Shirish Bari
Shirish Bari

Reputation: 2722

Rollover the logfile everyday irrespective if any messages is getting logged or not

I have below configuration in my log4j.properties

log4j.rootLogger=ERROR,FA
log4j.appender.FA=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.FA.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.FA.RollingPolicy.FileNamePattern=.\\logs\\app.log-%d{dd-MM-yyyy}
log4j.appender.FA.File=.\\logs\\app.log
log4j.appender.FA.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.FA.layout.ConversionPattern=%d %p %t %c: %m%n
log4j.appender.FA.Append=true

There are days when no error is reported and file is not getting rolled at the next consecutive days till next error is reported. As appender is set to ERROR level it will not roll log file till some error message is reported .

My question is Can we develop some mechanism so that the file will be rolled every midnight irrespective if appender have some error messages to be logged or not ?

I have a java application and we are using log4j api for logging so solution in Java or Log4j both would be appreciated .

thanks in advance

Note:- I cannot set the logger level to Info/Debug because of the size constraint

Upvotes: 0

Views: 1228

Answers (2)

Hein Blöd
Hein Blöd

Reputation: 1653

Using the new CronTriggeringPolicy in Log4j 2.5, I found a solution without dummy lines, which I posted here. Unfortunately, it still requires some custom plugin code because the behaviour of the DefaultRolloverStrategy is to simply delete empty files.

Upvotes: 1

Shirish Bari
Shirish Bari

Reputation: 2722

As it is a log4j behaviour not to rollover the empty file we have to use below approach

  1. Have a written a thread and scheduled it for 12 hours which logs the dummy statement "This is a dummy message"
  2. As the file is not empty now hence log4j takes care of rollover

and We send the log files every day we have decided to log a dummy statement after every 12 hours saying "This is a dummy message" , which has solved the problem of rollover.

Upvotes: 0

Related Questions