Java – How to configure a log4j file appender which rolls the log file every 15 minutes

javalog4j

I understand that i can use a DailyRollingFileAppender to roll the log file every month, day, half-day, hour or minute. But how can i configure log4j to roll the log file every 15 minutes.

If this is not possible by configuration, please suggest/direct me on how to extend log4j's file appender to achieve this.

Thanks and Regards.

Best Answer

The Javadoc for DailyRollingFileAppender in Log4J indicates that the time-based rolling only occurs on unit-based rollovers (day, week, month, etc.). That would mean the closest you could get with that pattern is '.'yyyy-MM-dd-HH-mm, which would roll over every minute.

My recommendations would be to do one of the following:

  • Since you're running on a fixed interval, write a custom FileAppender that uses logic borrowed from DailyRollingFileAppender to make the computation
  • If you have some flexibility, switch from Log4J to LOGBack, and write a custom RollingPolicy that uses logic borrowed from the LOGBack time calculations (which will be very similar to the ones in Log4J)

By the way, if you choose the latter, I'd recommend that you consider coding to the SLF4J API, and use LOGBack (or Log4J) as the underlying implementation.

Related Topic