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

前端 未结 14 1474
北海茫月
北海茫月 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:51

    According to the following post, you can't do this with log4j: Use MaxBackupIndex in DailyRollingFileAppender -log4j

    As far as I know, this functionality was supposed to make it into log4j 2.0 but that effort got sidetracked. According to the logback website, logback is the intended successor to log4j so you might consider using that.

    There's an API called SLF4J which provides a common API to logging. It will load up the actual logging implementation at runtime so depending on the configuration that you have provided, it might use java.util.log or log4j or logback or any other library capable of providing logging facilities. There'll be a bit of up-front work to go from using log4j directly to using SLF4J but they provide some tools to automate this process. Once you've converted your code to use SLF4J, switching logging backends should simply be a case of changing the config file.

提交回复
热议问题