Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

One of the things that I am investigating is using the dateext option for logrotate. However if I understood things correctly apache will keep writing to the files access.log and error.log.

I was wondering if there was a way to have apache log to a file access.log.YYYYMMDD and error.log.YYYYMMDD instead? Where YYYYMMDD would be the timestamp when the file was created.

share|improve this question

Apache comes with rotatelogs which does exactly what you're looking for.

CustomLog "|/usr/bin/rotatelogs -l /var/logs/apache2/access.log.%Y%m%d 86400" combined
ErrorLog "|/usr/bin/rotatelogs -l /var/logs/apache2/error.log.%Y%m%d 86400"

Or you can write your own small script that does this. Apache than pipes it's log to that script's STDIN again using the following syntax

CustomLog |/path/to/ combined

Your script reads the log lines on STDIN and writes them to a logfile which name you can choose and change freely.

The script could look something like this

use warnings;
use strict;
use Time::Local;
use IO::Handle;

my $logfilePrefix = '/var/log/apache2/access.log.';
my $rotateAt;


while(my $l = <STDIN>)
  reopenLog() if(time() >= $rotateAt);
  print LOG $l;

exit 0;

sub openLog
  my ($day, $month, $year) = (localtime)[3,4,5];
  my $logfile = $logfilePrefix.sprintf("%04d%02d%02d", $year+1900, $month+1, $day);
  $rotateAt = timelocal(0, 0, 0, (localtime(time() + 86400))[3,4,5]);
  open(LOG, ">> $logfile") || die "couldn't write to $logfile: $!\n";

sub reopenLog

Further reading:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.