Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

java - How to configure a log4j file appender which rolls the log file every 15 minutes

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...