Configuring logrotate to Append a Daily Date to Log File Names, Set Retention Period, and Compress Old Files
Overview
The default settings in /etc/logrotate.conf have the following inconveniences:
- Log file names are given sequential numbers like error_log.1, which makes it hard to tell the actual date.
- By default, logs are kept for only 4 weeks, so there is only about a month’s worth of information. In some cases you simply cannot go back far enough to investigate.
- Files are not compressed, so they take up disk space.
These make investigation very inconvenient.
When httpd is installed via yum, the initial logrotate.conf settings look like this:
Append the date to log file names
Add the following line to /etc/logrotate.conf:
1 | dateext |
Set the log retention period to half a year (53/2 weeks ≒ 27)
1 | # keep 4 weeks worth of backlogs |
- Since disk capacity may be a concern, please consider how large the logs will grow per month before deciding on these settings.
Date
Compress old files
1 | # uncomment this if you want your log files compressed |
Rotating Apache logs by date
The day after applying the settings
You can see that the output looks like this:
1 | error_log-20150315.gz |
If full-width characters are present in logrotate.conf, rotation may not work correctly, so please be careful.
Configuring logrotate to Append a Daily Date to Log File Names, Set Retention Period, and Compress Old Files