Best way to rotate Apache log files [closed]
Asked Answered
S

7

70

I've got an Apache server that has one access log file that is topping 600MB. This makes it really hard to search the file or parse it.
What software or modules for Apache are available that will make a daily copy of my access file to make it more manageable?

Shirleyshirlie answered 4/9, 2008 at 15:51 Comment(6)
Lucky you - the one I'm facing is 2.4 GIGABYTES :(Blowfly
You could indicate whether you are really looking for an Apache module, or something that depends on the employed OS, such as logrotate or cron (already mentioned in the answers). That could help in replying to this - in my opinion - simple question.Emmerie
14GB here! Just over one year of entries.Burton
I don't care about the logs and only want them to not grow unlimited: on WIN i used this to have max 7x15 mb logs. CustomLog "|bin/rotatelogs.exe -l logs/access.%A.log 15M" commonShanaeshanahan
scratch that, according to Bug 51081 issues.apache.org/bugzilla/show_bug.cgi?id=51081 rotatelogs will not overwriteShanaeshanahan
23 GB here, on an old server... this should be a built-in featureDelorsedelos
L
50

Have you looked at logrotate - this is probably the simplest, most widely available and well understood method of achieving this. It is highly configurable and will probably do 90% of what you need.

Larimore answered 4/9, 2008 at 15:54 Comment(2)
A usage example like Brian's answer would be really helpful. Thanks.Neaten
The downside to logrotate is that the logfile will continue to get written to the old filename until the process restarts.Biernat
H
28

I'm a big fan of Cronolog. Just install and pipe your logs through it. For daily log rotation, something like this would work:

ErrorLog  "|/usr/bin/cronolog /path/to/logs/%Y-%m-%d/error.log"
CustomLog "|/usr/bin/cronolog /path/to/logs/%Y-%m-%d/access.log" combined

Pretty handy, and once installed, easier (in my experience) than logrotate.

Huoh answered 4/9, 2008 at 16:12 Comment(1)
when I used cronolog i encountered some issues when the log file reached 2GB of data. At that point there was no more logging and apache stalled until i removed/renamed the file. Since then I'm using logrotate and all is fine :)Cuddle
G
13

The actual command for Windows, which is quite difficult to find online is:

CustomLog '|" "*Apache-Path/bin/rotatelogs.exe" 
    "**Apache-Path*/logs/backup/internet_access_%d-%m-%y.log" 86400' combined

Where the "internet_access" bit is the name you choose for your files, the 86400 is the number of seconds in one day. You need to change the Apache-Path to the relevant directory you've installed Apache to.

Gabrielegabriell answered 10/12, 2008 at 11:15 Comment(7)
Instead of dealing with trying to wrap quotes around the paths, I ended up shortening them, such as "c:/Progra~1/..." Also, it took awhile, but I eventually realized that I needed forward slashes in the paths.Selfconsistent
Don't use backslashes in the path, it won't work! Alternatively, you can use mod_log_rotate. Using this mod prevents also cmd.exe issues under Windows.Magenta
I did had some issues in Apache2.4 (Windows 8) for implementing log rotation. But fixed as below. In httpd.conf file add the following line ServerRoot "c:/Apache24" # Apache installation path #CustomLog "|bin/rotatelogs logs/access.log.%Y-%m-%d 86400" common # This above does not work, but when added .exe, it worked. CustomLog "|bin/rotatelogs.exe logs/access.log.%Y-%m-%d 86400" commonDifferential
Note that the -n 4 "circular logs" option might not work as expected unless you add the -L basename option.Biernat
@Gabrielegabriell Can you give exact command for configuration rotatelog because am getting unable to open log error while i trying to start apache server.Bureaucratize
Unable to open file sounds like permissions - create a blank file with the correct name and check the write permissions.Gabrielegabriell
This is good enough to me ErrorLog '| "bin/rotatelogs.exe" "logs/error_%Y%m%d-%H%M%S.log" 10M'Semimonthly
P
11

logrotate

logrotate is probably the best solution. Use the file /etc/logrotate.conf to change the settings for all your logs. You van change weekly to daily so the logs are rotated every day. Also, you might want to add compress so the archives are compressed. If you don't care about the old logs, you can set rotate rotate 4 to something lower.

Paymar answered 4/9, 2008 at 16:0 Comment(2)
I'm intrigued - what are the differences between this and the apache-supplied 'rotatelogs'? And why doesn't the apache manual mention logrotate (yet it does refer to cronolog)?Blowfly
Just an assumption: this could be because logrotate is not available on all system Apache is running. I have only used it on Linux installations, where it is used to rollover all of the system logs, and of course Apache's log files.Emmerie
E
3

CustomLog "|bin/rotatelogs /var/logs/logfile 5M" common

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

ErrorLog "|bin/rotatelogs /var/logs/errorlog.%Y-%m-%d-%H_%M_%S 5M" 

This Would Be Best Way to Redirect Apache logs. No need to compile mod with httpd.

Expansible answered 21/6, 2011 at 0:20 Comment(4)
Is there some rotatelogs option to delete old files or to overwrite them?Carrol
@Carrol did you find a solution to delete old files ?Casandra
Not with date log names, I had to switch to indexed log names (rotatelogs.exe -n 3 filepath 1M)Carrol
Using rotatelogs -t ... 100M, I find that the logfile is overwritten each time instead of appended to. Is there any way to make rotatelogs append to an existing log file?Epoxy
S
1

rotatelog.exe or cronolog.exe on windows os. They are used in pipe command in http.conf Mod_log_rotate additional module for apache ONLY for access log rotation Logrotate ONLY for unix os.

Surname answered 4/8, 2010 at 9:22 Comment(0)
R
0

I have a module that does this for you without the need for external pipes etc :

http://www.poptart.org/bin/view/Poptart/ModAutorotate

I've tried to add it to the Apache modules collection but that seems to have been broken for a while now.

Ripple answered 27/11, 2010 at 9:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.