Rolling Logfiles


Messages get written to logfiles with no consideration for the files' size. If you didn't take some action to keep the size of the logfiles down, the files would grow and grow, eventually eating up all of your free disk space. To help solve this problem, JBoss provides the ability to periodically roll active logging over to a new logfile, leaving the old files around to be archived, or even deleted. There are two basic rotation policies: size-based and time-based. We've seen both of them in passing, but now we'll examine them in more detail.

How do I do that?

When specifying an appender, it is necessary to provide the name of the class that provides the actual implementation of the appender. We've already used DailyRollingFileAppender for todo.log:

     <appender name="TODO"               docEmphStrong">org.jboss.logging.appender.DailyRollingFileAppender">         <errorHandler />         <param name="File" value="${jboss.server.home.dir}/log/todo.log"/>         <param name="Append" value="false"/>         <!-- Rollover at midnight each day -->         <param name="DatePattern" value="'.'yyyy-MM-dd"/>         <layout >            <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>         </layout>     </appender> 


Note: Logfiles are rolled only when there is activity. Don't expect to see logfiles rolling when your application is sitting idle.

DailyRollingFileAppender supports date-based rollover at these intervals: monthly, weekly, daily, twice a day at midnight and noon, and at the top of every hour and every minute. The DatePattern parameter controls both the frequency of the rollover and the name of the saved logfile. Because the date format uses the day as the lowest unit of time, a new logfile will be created nightly, with the old one archived to be renamed according to the date pattern: todo.log-2005-04-04, for example.

The other popular rolling strategy is to roll a new logfile only when the current one gets large. RollingFileAppender does this job:

     <appender name="TODO"               docEmphStrong">org.jboss.logging.appender.RollingFileAppender">         <errorHandler />         <param name="File"       value="${jboss.server.home.dir}/log/todo.log"/>         <param name="Append"         value="false"/>         <param name="MaxFileSize"    value="100KB"/>         <param name="MaxBackupIndex" value="10"/>         <layout >             <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>         </layout>     </appender> 

RollingFileAppender will create a new logfile when the size of the current log exceeds MaxFileSize. In this example, the maximum size is 100KB. The old logfiles will have an incremental index number added to the filename: todo.log.1, todo.log.2, etc. The older logs will have a higher index number. Log4j will keep MaxBackupIndex of old logfiles and will delete logfiles that are too old.



JBoss. A Developer's Notebook
JBoss: A Developers Notebook
ISBN: 0596100078
EAN: 2147483647
Year: 2003
Pages: 106

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net