Must have Time and Size Log4J appender for your application

TimeAndSizeRollingAppender, a very handy Log4J appender that supports rolling log files by time and size. I haven’t found one that meet my requirements in the built-in/extra appenders provided by log4j project. DailyRollingFileAppender is the closest appender I could live with, but there is no way to control size of the file when rolling, which is very useful when triaging application issues at debug level trace.

TimeAndSizeRollingAppender appender is pretty cool, loving it so far. Compression is built-in and super easy to configure. It has many features and this comparison covers those in detail. Here is a sample log4j.properties file that I ended up using:


log4j.rootLogger=DEBUG,rollingByDate
# logfile is set to be a TimeAndSizeRollingAppender. Keep 100 backup files before rotating and rolling daily, compression set to GZ
log4j.appender.rollingByDate=uk.org.simonsite.log4j.appender.TimeAndSizeRollingAppender
log4j.appender.rollingByDate.File=/logs/app.log
log4j.appender.rollingByDate.RollOnStartup=true
log4j.appender.rollingByDate.DateRollEnforced=true
log4j.appender.rollingByDate.Threshold=DEBUG
log4j.appender.rollingByDate.DatePattern=.yyyy-MM-dd
log4j.appender.rollingByDate.MaxFileSize=10MB
log4j.appender.rollingByDate.MaxRollFileCount=100
log4j.appender.rollingByDate.ScavengeInterval=30000
log4j.appender.rollingByDate.BufferedIO=false
log4j.appender.rollingByDate.CompressionAlgorithm=GZ
log4j.appender.rollingByDate.CompressionMinQueueSize=5
log4j.appender.rollingByDate.layout=org.apache.log4j.PatternLayout
log4j.appender.rollingByDate.layout.ConversionPattern=%-5p %-23d{ISO8601} [%t] %x: %c{1} – %m%n

2 thoughts on “Must have Time and Size Log4J appender for your application

  1. I have an issue with TimeAndSizeRollingAppender. When the rollover happens for the second time. The first log line gets duplicated. Has anyone faced this issue before? If yes, what was the solution?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s