The log files for Apache can typically grow very large, and it sometimes necessary to periodically rotate the log files by moving or deleting the existing logs. Apache is capable of writing log files through a pipe to another process, rather than directly to a file. This allows log to be rotated without restarting Apache.
rotatelogs
The Apache HTTP Server includes a simple program called rotatelogs for log rotation. In Ubuntu this program can be found in /usr/sbin/rotatelogs.
The following is a simple configuration to setup log rotation in Apache:
CustomLog "|/usr/sbin/rotatelogs /var/log/apache2/access.log 86400" common
This configuration rotates the access log file every 24 hours.
CustomLog "|/usr/sbin/rotatelogs /var/log/apache2/access.log 5M" common
This configuration Rotates the access log file whenever it reaches 5MB.
ErrorLog "|/usr/sbin/rotatelogs /var/log/apache2/error.log.%Y-%m-%d-%H_%M_%S 5M"
This configuration will rotate the error log file whenever it reaches 5MB, and add the suffix to the log file name in the form of error.log.YYYY-mm-dd-HH_MM_SS.
For more information on rotatelogs refer to http://httpd.apache.org/docs/2.2/programs/rotatelogs.html
cronolog
Cronolog is a similar more flexible log rotation program. It rotates log files specify by the filename template and the current date and time.
To install cronolog:
$ apt-get install cronolog
The configuration below rotates the log files on a daily basis:
CustomLog "|/usr/bin/cronolog /var/log/apache2/access.log.%Y-%m-%d" ErrorLog "|/usr/bin/cronolog /var/log/apache2/errors.log.%Y-%m-%d"
To rotate the log file on a monthly basis the configuration would be:
CustomLog "|/usr/bin/cronolog /var/log/apache2/access.log.%Y-%m"
For more information on cronolog refer to http://cronolog.org/
Related posts: