How to configure log4j to only keep log files for

2019-01-10 18:47发布

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

I want log files to be rotated daily, like

log.2010-09-10
log.2010-09-09
log.2010-09-08
log.2010-09-07
log.2010-09-06
log.2010-09-05
log.2010-09-04

But for data security reasons we are not allowed to keep log files for longer than seven days at my company. So the generation of the next next log file log.2010-09-11 should trigger the deletion of log.2010-09-04. Is it possible to configure such a behaviour with log4j? If not, do you know another elegant solution for this kind of logging problem?

13条回答
Evening l夕情丶
2楼-- · 2019-01-10 19:02

If you are using Linux, you can configure a cron job using tmpwatch.

Most Linux systems have a tmpwatch cron job that cleans up the /tmp directory. You can add another that monitors your logging directory and deletes files over 7 days old.

If you are using a different system, there are probably equivalent utilities.

查看更多
仙女界的扛把子
3楼-- · 2019-01-10 19:07

I create this Methode and call it by closing the application:

  public void deleteFiles(){

    File f = new File("log");
    File[] fileArray = f.listFiles();
    double timenow = System.currentTimeMillis();

    double olderTenDays = timenow - 864000000;// MS for ten days

    for (int i = 0; i < fileArray.length; i++) {

        if(fileArray[i].lastModified()< olderTenDays )
           fileArray[i].delete();
    }
 }
查看更多
▲ chillily
4楼-- · 2019-01-10 19:09

Use the setting log4j.appender.FILE.RollingPolicy.FileNamePattern, e.g. log4j.appender.FILE.RollingPolicy.FileNamePattern=F:/logs/filename.log.%d{dd}.gz for keeping logs one month before rolling over.

I didn't wait for one month to check but I tried with mm (i.e. minutes) and confirmed it overwrites, so I am assuming it will work for all patterns.

查看更多
时光不老,我们不散
5楼-- · 2019-01-10 19:14

I had set:

log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.DatePattern='.'yyyy-MM-dd
# Archive log files (Keep one year of daily files)
log4j.appender.R.MaxBackupIndex=367

Like others before me, the DEBUG option showed me the error:

log4j:WARN No such property [maxBackupIndex] in org.apache.log4j.DailyRollingFileAppender.

Here is an idea I have not tried yet, suppose I set the DatePattern such that the files overwrite each other after the required time period. To retain a year's worth I could try setting:

log4j.appender.R.DatePattern='.'MM-dd

Would it work or would it cause an error ? Like that it will take a year to find out, I could try:

log4j.appender.R.DatePattern='.'dd

but it will still take a month to find out.

查看更多
爷、活的狠高调
6楼-- · 2019-01-10 19:17

There's also a DailyRollingFileAppender; http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html

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 realised before), then this looks like a better bet; http://logging.apache.org/log4j/companions/extras/apidocs/index.html

查看更多
Anthone
7楼-- · 2019-01-10 19:19

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 {} \;
查看更多
登录 后发表回答