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

前端 未结 14 1433
北海茫月
北海茫月 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 15:00

    There's also a DailyRollingFileAppender

    Edit: after reading this worrying statement:

    DailyRollingFileAppender has been observed to exhibit synchronization issues and data loss. The log4j extras companion includes alternatives which should be considered for new deployments and which are discussed in the documentation for org.apache.log4j.rolling.RollingFileAppender.

    from the above URL (which I never realized before), then the log4j-extras looks to be a better option.

    0 讨论(0)
  • 2020-12-02 15:01

    You can perform your housekeeping in a separate script which can be cronned to run daily. Something like this:

    find /path/to/logs -type f -mtime +7 -exec rm -f {} \;
    
    0 讨论(0)
提交回复
热议问题