Simple Log User Guide

Simple Log - Log Rolling

Rolling Along

This page explains log rolling, a new feature in Simple Log 2.0 that allows you to maintain an archive of your logs without having to make use of one huge, massive, ridiculously gargantuan log file.

Sounds good, doesn't it?

What Is Rollover?

Log rolling, or log rollover, allows you to have your logging distributed across multiple files over time, rather than continuing to accumulate it all in a single huge file.

How Simple Log's Rollover Works

Log rolling in Simle Log uses the concept of an active log file and rolled log files. Simple Log will always write new output to the active log file. When it is time to roll the active log file, its entire contents are copied to a new "rolled" log file, the active log file is emptied and Simple Log begins writing again at the start of the (now empty) active log file.

As for how Simple Log decides when it is time to roll the active log file, this depends on the Rollover Strategy being used.

Explanation of the Strategies

Simple Log comes with two Rollover Strategies, although you can also write your own if necessary. The 'File Size' strategy will roll the active log file whenever it reaches a particular size, while the 'Time of Day' strategy rolls the active log file at a particular time of day.

The size at which the File Size strategy will roll the log file is of course configurable and defaults to 100 MB.

The time at which the Time of Day strategy will roll the log file is configuragble and defaults to 12:00 Midnight. The Timezone in which the rolling time is specified is also configurable and by default uses the system's default Timezone. If you're thinking "What if my application isn't running at that time?", don't worry - we handle that.

How to Enable Log Rolling

To turn on log rolling in Simple Log you simply need to specify the strategy to use. You can specify either 'fileSize' or 'timeOfDay', or specify your own custom strategy. This will enable log rolling, with the rolled logs being placed in the same directory as the active log file and the rolled log file names being the same as the active log file name but with a unique, incrementing number prepended (e.g. 02-application.log).

Both the directory where the logs are placed and the format of the rolled log file names is configurable. If you're really anal, you can also configure how often Simple Log will check whether it needs to roll the logs. By default this is done every 60 seconds.

Write and Use Your Own Strategy

If you have some other requirement for determining when your logs are rolled, for example "once a week", "when the moonlight is at just the right angle", or using the "Schimke Random Rolling" algorithm, you can easily write your own implementation of RolloverStrategy. It's as simple as creating a zero-argument constructor and implementing the

configure(Map properties)
method and the all-important
boolean rolloverNow(Date fileCreated, long fileLength)
function. The documentation is pretty straight-forward, and there's of course the source code of the other two strategies for you to look at.

Your strategy will be able to read properties out of the Simple Log properties file, so you can make it as configurable as you like. If you're planning on giving your strategy to other people, just remember to have sensible defaults for all the properties so that they don't have to configure it if they don't want to.

To make use of your own strategy, you simply need to enter its fully-qualified class name as the value for the simplelog.rollover property and make sure that your class is available in the same classloader that is loading Simple Log.

Simple Log User Guide