Thursday, November 1, 2012

JBoss – Daily rolling logs (log4j)

Our standard log4j JBoss application configuration uses with size based, index rolling file appenders, and when having to investigate customers issues that happened a few days back requires quite a bit of navigation to find the particular date and time, or worse if a recurrent problem fills up the log parts, it might loose the information that could’ve otherwise helped. So recently I’ve been looking to implement daily rolling file appenders in a customer site.

Looking at Log4j DailyRollingFileAppender they are advising to use the log4j extras companion classes:

http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html
DailyRollingFileAppender extends FileAppender so that the underlying file is rolled over at a user chosen frequency. 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.
You can download the Apache Extras Companion for Apache Log4j from project website, or otherwise from Maven.org - place that in your JBoss / application CLASSPATH and restart the application.

The RollingFileAppender uses a TimeBasedRollingPolicy, controlling when the file is rolled through a SimpleDateFormat pattern (daily, hourly etc). Optionally it can compress the archive log by adding the .gz/.zip extension to the filename. You can also move the archive logs into a separate location.
   <!-- SERVER.LOG -->
   <appender name="FILE" class="org.apache.log4j.rolling.RollingFileAppender">
     <!-- Active log file name -->
     <param name="File"     value="${jboss.server.home.dir}/log/server.log" />
     <param name="Append"   value="true" />
     <param name="Encoding" value="UTF-8" />

     <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
       <!-- Archive file to roll to @ midnight (yyyy-MM-dd) with compression -->
       <param name="FileNamePattern" value="${jboss.server.home.dir}/log/archive/server.%d{yyyy-MM-dd}.log.zip" />
     </rollingPolicy>

     <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="%d %-5p [%t] [%c{1}:%L] %m%n"/>
     </layout>
   </appender>

1 comment :