19

I use Log4j with the RollingFileAppender to create a log rotation based on size.

How can I configure it to log to each file for a certain amount of time before rotating?

For example, so that each log file contains one hour of logs, rotating at the top of each hour?

I configure Log4j programatically in Java using a Properties object (as opposed to a log4j.properties file)

Thiago Negri
  • 5,221
  • 2
  • 28
  • 39
DivideByHero
  • 19,715
  • 24
  • 57
  • 64

5 Answers5

30

You probably want to use a DailyRollingFileAppender. To roll them hourly, for example, you'd use a DatePattern of '.'yyyy-MM-dd-HH. For a log4j.properties file:

log4j.appender.myAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.myAppender.DatePattern='.'yyyy-MM-dd-HH
...

Or for your programmatic configuration:

DailyRollingFileAppender appender = new DailyRollingFileAppender();
appender.setDatePattern("'.'yyyy-MM-dd-HH");

Logger root = Logger.getRootLogger();
root.addAppender(appender);

Unfortunately, using a DailyRollingFileAppender means that you can't limit the file size - this could be problematic if you have tons of logs in the given rolled period.

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
  • But aren't there potential threading issues? I've heard of cases where it fails to roll over if other threads are logging. – sproketboy May 20 '15 at 11:43
  • 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. – petertc Mar 14 '16 at 08:30
4

Additionally,

log4j.appender.myAppender=org.apache.log4j.DailyRollingFileAppender
**log4j.appender.myAppender.DatePattern='.'yyyy-MM-dd-HH**

The following list shows all the date patterns which have defined by log4j,

Minutely  '.'yyyy-MM-dd-HH-mm application.log.2013-02-28-13-54
Hourly '.'yyyy-MM-dd-HH application.log.2013-02-28-13
Half-daily '.'yyyy-MM-dd-a application.log.2013-02-28-AM app.log.2013-02-28-PM
Daily '.'yyyy-MM-dd application.log.2013-02-28
Weekly '.'yyyy-ww application.log.2013-07 app.log.2013-08 
Monthly '.'yyyy-MM application.log.2013-01 app.log.2013-02
3

The other thing to be careful of with any rolling file appender is to make sure only one JVM access a particular log file at a time. This is because log4j caches the log file size for performance reasons, and your 'rolling' will get wonky if multiple JVMs access the same files.

Jeremy Visser
  • 6,237
  • 1
  • 25
  • 30
Nathan Feger
  • 19,122
  • 11
  • 62
  • 71
2

Use a DailyRollingFileAppender.

In particular, setting its 'datePattern' property to '.'yyyy-MM-dd-HH would cause file to rotate every hour.

ChssPly76
  • 99,456
  • 24
  • 206
  • 195
0

You need to use the DailyRollingFileAppender. Despite its misleading name it can be configured in a to run at configurable time periods up to minutes.

Tendayi Mawushe
  • 25,562
  • 6
  • 51
  • 57