Sachin’s Weblog

August 19, 2007

Apache Log Rotation

Filed under: Apache, General, LAMP, Linux — sachin @ 7:47 am

The Apache access log file typically grows 1 MB or more per 10,000 requests. It will consequently be necessary to periodically rotate the log files by moving or deleting the existing logs. This cannot be done while the server is running, because Apache will continue writing to the old log file as long as it holds the file open. Instead, the server must be restared after the log files are moved or deleted so that it will open new log files. By using a graceful restart, the server can be instructed to open new log files without losing any existing or pending connections from clients. However, in order to accomplish this, the server must continue to write to the old log files while it finishes serving old requests. It is therefore necessary to wait for some time after the restart before doing any processing on the log files. A typical scenario that simply rotates the logs and compresses the old logs to save space is:

mv access_log access_log.old
mv error_log error_log.old
apachectl graceful
sleep 600
gzip access_log.old error_log.old

Another way to perform log rotation is using piped log.
Piped Logs
Apache httpd is capable of writing error and access log files through a pipe to another process, rather than directly to a file.In order to write logs to a pipe, simply replace the filename with the pipe character “|, followed by the name of the executable which should accept log entries on its standard input. Apache will start the piped-log process when the server starts, and will restart it if it crashes while the server is running.
Piped log processes are spawned by the parent Apache httpd process, and inherit the userid of that process. This means that piped log programs usually run as root.
One important use of piped logs is to allow log rotation without having to restart the server. The Apache HTTP Server includes a simple program called rotatelogs for this purpose. For example, to rotate the logs every 24 hours, you can use:
CustomLog "|/usr/local/apache/bin/rotatelogs /var/log/access_log 86400" common

rotatelogs – Piped logging program to rotate Apache logs
rotatelogs is a simple program for use in conjunction with Apache’s piped logfile feature. For example:

CustomLog “|bin/rotatelogs /usr/local/apache2/logs/my-site.com_access_log 86400″ common

This creates the files /usr/local/apache2/logs/my-site.com_access_log.nnnn, where nnnn is the system time at which the log nominally starts (this time will always be a multiple of the rotation time, so you can synchronize cron scripts with it). At the end of each rotation time (here after 24 hours) a new log is started.
CustomLog "|bin/rotatelogs
/usr/local/apache2/logs/my-site.com_error_log 5M” common

This configuration will rotate the logfile whenever it reaches a size of 5 megabytes.

ErrorLog "|bin/rotatelogs /usr/local/apache2/logs/my-site.com_error_log.%Y-%m-%d-%H_%M_%S 5M”

This configuration will rotate the error logfile whenever it reaches a size of 5 megabytes, and the suffix to the logfile name will be created of the form error_log.YYYY-mm-dd-HH_MM_SS.

Configure Log RotateSimply create a file called apache2 and copy this into logrotate.d folder.

/var/log/apache2/*_log {
sharedscripts
monthly
rotate 6
create
compress
postrotate
/bin/kill -HUP httpd `cat /var/run/httpd.pid 2> /dev/null` 2> /dev/null || true
endscript
}

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.

Blog at WordPress.com.