0
votes

My name is Luis Ribeiro and I am trying to set log4j, so that it will delete the older rotate logs.

The solution that we have for now is to use cron with a script. For example like this: How to configure log4j to only keep log files for the last seven days?

But here are some major problems:

  1. Work with hundred of machines(n)
  2. Work with many crons in many machines(n * m)
  3. Work with different structures and OS(n * m * z)
  4. Cron will delete even when the application stop and there is a lost of information

The ideal is that when application runs, log4j will take care complete of the log rotation.

  • It will rotate once a day: RollingFile: Daily and filePattern="logs/${filename}.[ %d{yyyy-MM-dd} | -%i | any type of counter ].log.gz" with TimeBasedTriggeringPolicy
  • actual log and n rotation files are kept. Older are deleted: app.log, app.{-1 day}.log.gz,..., app.{-n days}.log.gz
  • The pattern name isn't important it can be a number in the file name
  • We cannot use size as trigger. We don't know how much the program will do during the day. Log size varies very, very much
  • It should be structure and os independent. We prefer to enhance the log4j Properties or XMl file, than adding scripts and cron triggers.

I try to use the DefaultRolloverStrategy with TimeBasedTriggeringPolicy. But the are problems:

  1. filePattern=${filename}.%d{yyyy-MM-dd}-%i.log.gz will result in: app.log, app.{-1 day}-1.log.gz, app.{-2 day}-1.log.gz,..., app.{-(n + 1) days}-1.log.gz,... => It will never be deleted
  2. filePattern=${filename}-%i.log.gz result in java.lang.IllegalStateException: Pattern does not contain a date

Is there any away to enhance the log4j so it will take care of all these tasks?

With best regards, Luis

1

1 Answers

1
votes

Because DailyRollingFileAppender does not have attribute MaxBackupIndex, so you have to remove log by yourself.

Or you can perform crontab for housecleaning like:

find /path/to/logs -type f -mtime +dayToKeep -exec rm -f {} \;