How can I get log4j to delete old rotating log files?

前端 未结 4 1957
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 23:56

How can I get log4j to delete old rotating log files? I know I can set up automated jobs (cron for UNIX and scheduled task for Windows), but I want it cross platform, and I

4条回答
  •  自闭症患者
    2020-12-01 00:24

    Logs rotate for a reason, so that you only keep so many log files around. In log4j.xml you can add this to your node:

    
    

    The value tells log4j.xml to only keep 20 rotated log files around. You can limit this to 5 if you want or even 1. If your application isn't logging that much data, and you have 20 log files spanning the last 8 months, but you only need a weeks worth of logs, then I think you need to tweak your log4j.xml "MaxBackupIndex" and "MaxFileSize" params.

    Alternatively, if you are using a properties file (instead of the xml) and wish to save 15 files (for example)

    log4j.appender.[appenderName].MaxBackupIndex = 15
    

提交回复
热议问题