How to configure log4j to only keep log files for the last seven days?

前端 未结 14 1471
北海茫月
北海茫月 2020-12-02 14:01

I have the following logging problem with several Java applications using log4j for logging:

I want log files to be rotated daily, like

         


        
14条回答
  •  醉梦人生
    2020-12-02 14:54

    There is another option DailyRollingFileAppender. but it lacks the auto delete (keep 7 days log) feature you looking for

    sample

    log4j.appender.DRF=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.DRF.File=example.log
    log4j.appender.DRF.DatePattern='.'yyyy-MM-dd
    

    I do come across something call org.apache.log4j.CompositeRollingAppender, which is combine both the features of the RollingFileAppender (maxSizeRollBackups, no. of backup file) and DailyRollingFileAppender (roll by day).

    But have not tried that out, seems is not the standard 1.2 branch log4j feature.

提交回复
热议问题