Apache daily logrotate

14,894

Solution 1

Following on to Brian's answer, I'm a big fan of cronolog, which does pretty much exactly what you're going for:

CustomLog "|/usr/sbin/cronolog /var/log/httpd/%Y/%m/%Y-%m-%d-access.log" combined
ErrorLog "|/usr/sbin/cronolog /var/log/httpd/%Y/%m/%Y-%m-%d-error.log"

yum install cronolog will get you cronolog on Cent6.

Solution 2

Try running logrotate manually to look for errors: logrotate -d /etc/logrotate.d/httpd. The manual says "-d Turns on debug mode and implies -v. In debug mode, no changes will be made to the logs or to the logrotate state file."

This is what we're using successfully:

/var/log/httpd/*log {
  daily
  dateext
  dateformate -%d-%m-%Y
  missingok
  nocompress
  rotate 30
  postrotate
    /sbin/service httpd reload > /dev/null 2>/dev/null || true
  endscript
}

Solution 3

Apache lets you pipe log files to another program which can then handle rotation without having to reload/restart Apache. Apache even provides a program to do this.

ErrorLog "|bin/rotatelogs -l -f /var/log/apache2/errlogfile.%Y.%m.%d.log 86400" common
CustomLog "|bin/rotatelogs -l -f /var/log/apache2/logfile.%Y.%m.%d.log 86400" common
Share:
14,894

Related videos on Youtube

salep
Author by

salep

Updated on September 18, 2022

Comments

  • salep
    salep almost 2 years

    I want to create daily logs, but there's a small problem. Logs aren't being created for each day, rather they contain the previous log files. Here's my current setup, how can I change it so it only create a log file for each day?

    I edit the following file : /etc/logrotate.d/httpd

    I'm using a control panel called Zadmin so I included its log path as a second dir.

    I'm using CentOS 6.5 64 bit.

    /var/log/httpd/*log /var/sentora/logs/domains/zadmin/*.log {
        missingok
        rotate 4000000
        daily
        notifempty
        sharedscripts
        postrotate
        /sbin/service httpd reload > /dev/null 2>/dev/null || true
        endscript
    }
    
    • c4urself
      c4urself almost 9 years
      What do you mean by > they contain the previous log files
    • salep
      salep almost 9 years
      For instance, when I look at the access.log file, it includes all the logs that has been created so far. I don't want it, I want it to create logs daily and name it like the following : access.log-21-08-2015
    • Brian
      Brian almost 9 years
      There is also a rotate logs program that comes with Apache, rotatelogs.
    • ojs
      ojs almost 9 years
      Is this happening only to Apache logs? If its happening to all logs the 'create' option might be missing from /etc/logrotate.conf (please add the content of that file to your post). Also the /var/lib/logrotate.status (I think Centos has that one, at least Arch has it) might be corrupted, you could try and move it out of harms way and try running logrotate -f /etc/logrotate.d/httpd manually as root. Also adding the -d switch for debugging purposes wont hurt.
    • Navern
      Navern almost 9 years
      Do you have cron task which runs logrotate? what happens when you run logrotate /etc/logrotate.conf manually?
    • MastaJeet
      MastaJeet almost 9 years
      WAG: There is an error in the apache configuration so the reload isn't working. Try apachectl configtest
  • Brian
    Brian almost 9 years
    It does look nicer than the Apache program, would just add it is in the EPEL repository which is not enabled be default on CentOS (yum install epel-release to install/enable it).
  • salep
    salep almost 9 years
    Where I should put the text that starts with "CustomLog" and "ErrorLog", /etc/logrotate.d/httpd?
  • salep
    salep almost 9 years
    Additionally, should I remove everything in the /etc/logrotate.d/httpd and just put these?
  • Brian
    Brian almost 9 years
    They go in the Apache web server's configuration file.
  • Hardoman
    Hardoman over 3 years
    ErrorLog has an issue in the format. ErrorLog takes one argument, The filename of the error log - no "common" is expected in the end of the line. Apache fails to start with such config.
  • Hardoman
    Hardoman over 3 years
    Moreover, you need to specify the real path to rotatelogs. Normally it's not like above but can be /usr/local/apache/bin/rotatelogs
  • Brian
    Brian over 3 years
    @Hardoman The examples are from the documentation: rotatelogs - Piped logging program to rotate Apache logs
  • Hardoman
    Hardoman over 3 years
    And you copied it incorrectly. ErrorLog doesn't have "common" in the examples!