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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
But unable to get TimeAndSizeRollingAppender from maven, any other possiblity to achieve this
?
LikeLike
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?
LikeLike