Tag Archives: Cronolog

Rotating Apache log files using Cronolog

I must confess that I am a stats freak. If you are a long time reader of my blog, then you would have known that by now yourself. 😉 This explains the reason why I want to preserve my Apache log files in spite of using a variety of stat services like Google Analytics, WordPress stats, statscounter, performancing metrics (before it was closed).

The default Apache configuration preserves the log files only for the last 10 days, but I wanted to permanently archive this files. After some searches in Google I came across an excellent program called Cronolog. Cronolog is a simple filter program which writes each log entry to a separate log file named after the filename format specified. You can use a variety of parameters like current date, time etc to define the filename template.

First we have to install cronolog, either by using aptitude or by downloading it from its download page. Then you have to change the log file name path in the virtual host file. (In Ubuntu Gusty, the virtual host files are situated in the path /etc/apache2/sites-enabled). I am using the following file format for this blog
# Custom log file locations
LogLevel warn
ErrorLog "|/usr/sbin/cronolog /path/to/logs/%Y/%m/%Y-%m-%d-sudarmuthu.com-error.log"
CustomLog "|/usr/sbin/cronolog /path/to/logs/%Y/%m/%Y-%m-%d-sudarmuthu.com-access.log" combined

which will store my log files in separate folders for each year and for each month, like the below hierarchy
/2007/12/2007-11-01-sudarmuthu.com-access.log
/2007/12/2007-11-02-sudarmuthu.com-access.log
......
/2008/01/2008-01-01-sudarmuthu.com-access.log
/2008/01/2008-01-02-sudarmuthu.com-access.log
......

You can use a variety of modifiers for the filename and I have documented some of them in the below table. You can get more information from its documentation.

Specifier Description
Time fields
%H hour (00..23)
%I hour (01..12)
%p the locale’s AM or PM indicator
%M minute (00..59)
%S second (00..61, which allows for leap seconds)
%X the locale’s time representation (e.g.: “15:12:47”)
%Z time zone (e.g. GMT), or nothing if the time zone cannot be determined
Date fields
%a the locale’s abbreviated weekday name (e.g.: Sun..Sat)
%A the locale’s full weekday name (e.g.: Sunday .. Saturday)
%b the locale’s abbreviated month name (e.g.: Jan .. Dec)
%B the locale’s full month name, (e.g.: January .. December)
%c the locale’s date and time (e.g.: "Sun Dec 15 14:12:47 GMT 1996")
%d day of month (01 .. 31)
%j day of year (001 .. 366)
%m month (01 .. 12)
%U week of the year with Sunday as first day of week (00..53, where week 1 is the week containing the first Sunday of the year)
%W week of the year with Monday as first day of week (00..53, where week 1 is the week containing the first Monday of the year)
%w day of week (0 .. 6, where 0 corresponds to Sunday)
%x locale’s date representation (e.g. today in Britain: “15/12/96”)
%y year without the century (00 .. 99)
%Y year with the century (1970 .. 2038)

Posted in Unix/Server Stuff | Tagged , , , , | 1 Comment