Recipe 3.9 Rotating Logs on the First of the Month

Problem

You want to close the previous month's logs and open new ones on the first of each month.

Solution

The Apache distribution doesn't include a canned script to perform this duty, but you can find one on this book's companion site at http://Apache-Cookbook.Com/sources/Chapter04/rotate-log-monthly.pl.

You use it by piping the desired activity log through the script, such as with:

CustomLog "| rotate-log-monthly.pl logs/access_log-%Y-%M" CLF

The argument to the script is the name of the logfile; the special sequences beginning with % are passed to strftime(3) to form the new logfile name.

Discussion

As with the other logging solutions in this chapter, the solution given here addresses a single specific need. If you want to combine functions, such as splitting logfiles according to virtual host and rotating them on the first of each month, you are going to need to use custom scripts.

The rotate-log-monthly.pl script is a rather brute-force approach and may not be appropriate for extremely high traffic servers due to its sampling of the system time at each log entry. However, it illustrates the technique.

See Also

  • http://httpd.apache.org/docs/logs.html#piped



Apache Cookbook
Apache Cookbook: Solutions and Examples for Apache Administrators
ISBN: 0596529945
EAN: 2147483647
Year: 2006
Pages: 215

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net