Rotate Apache logs using Awstats

Whatever web log analyzer you would use, at one time or the other you will end up with one problem: how to deal with weblogs rotation, to not have a gap in your statistics? This has various solutions starting with running the weblog analyzer at the prerotate step of logrotate, or maybe using some manual script. This post will show how you can rotate the apache logs using awstats right after it has processed the logs. This can be beneficial for situations where you have quite big logs and using this method will keep them small all the time, and also where restarting apache just for logrotating is not such a good idea. Obviously for this to make sense, you need to be already using awstats for your log processing ;-) .

The awstats config option PurgeLogFile will purge the log file, after analyzing it. The default is 0 and this means no purge is done, and will assume some external tool is used for log rotation (like logrotate, or even apache internal mechanisms like rotatelogs). If this is set to 1, awstats will purge of the log file everytime it is run:

PurgeLogFile=1

By default the ‘archive’ file will be saved under: DirData/awstats_archive.configname.log , meaning this will become in time a huge file that needs to be also rotated, compressed, archived, etc. Another nice option of awstats is the possibility to rotate this file. ArchiveLogRecords can be used for this, and again this is also set by default on 0.

  • for ex. if we would want this file rotated daily you would use: %YYYY%MM%DD

    ArchiveLogRecords=%YYYY%MM%DD
    
  • or monthly: %YYYY%MM

    ArchiveLogRecords=%YYYY%MM
    

For example using:

PurgeLogFile=1
ArchiveLogRecords=%YYYY%MM

will create the following monthly archive files:

awstats_archive.configname.200804.log
awstats_archive.configname.200805.log
...

and from here you can compress them, archive them, or even pipe them to another analyzer software if you need that ;-) .

Depending on your needs, this might be a great (and very simple) solution to rotate your apache logs, right after processing and this without even restarting apache. For more details, check the awstats docs.

comments powered by Disqus