Logrotate
From Gentoo Linux Wiki
logrotate is a tool for log maintenance. Log files tend to grow, and from time to time you have to rotate them (delete them and create new ones). Instead of deleting you may move them to some other place, compress, mail etc. logrotate can automate this process.
Contents |
[edit] Installation
Just emerge it: emerge -av logrotate. A cronjob should be added automatically. Logrotate is called once per day by default but it doesn't mean your logs will be rotated daily. If you want to change the parameters logrotate is being called with, you can edit them in /etc/cron.daily/logrotate.cron.
[edit] Configuration
Main configuration file is /etc/logrotate.conf, other configuration files are included from /etc/logrotate.d/ directory.
[edit] Syntax
/var/log/wtmp {
monthly
create 0664 root utmp
rotate 1
}
The basic element of the configuration file is a section. It consists of a log file to rotate (/var/log/wtmp in this example), and options in curly braces. When you specify an option outside of any section, it is interpreted as a default setting.
[edit] Examples
| File: /etc/logrotate.d/apache2 |
/var/log/apache2/*.log {
weekly
missingok
rotate 2
compress
delaycompress
notifempty
sharedscripts
postrotate
if [ -f /var/run/apache.pid ]; then
/etc/init.d/apache2 restart > /dev/null
fi
endscript
}
|
| File: /etc/logrotate.d/syslog-ng |
/var/log/messages {
rotate 7
weekly
compress
size 50M
notifempty
delaycompress
sharedscripts
postrotate
/etc/init.d/syslog-ng reload > /dev/null 2>&1 || true
endscript
}
|
[edit] Common options reference
- compress - compress rotated logs using gzip
- create mode owner group - specify permissions (as in chmod) and ownership for created logs
- daily, weekly, monthly - how often should logs be rotated
- size size[G|M|k] - rotate, if log-file size exceeds size bytes / kilobytes / megabyte / gigabytes
- mail address - mail rotated logs to specified address
- olddir directory - move rotated logs to the specified directory
- rotate count - keep count rotated log files
for more options see man logrotate
[edit] Bugs
Do not select to rotate and compress /var/log/emerge.log files. Doing so will break Genlop trying to read emerge times. For some reason, Genlop cannot read anything else but the default "/var/log/emerge.log" named file.
[edit] Troubleshooting
If you're having problems with logrotate not rotating your logs, try running it manually with the -d option to force a debug output: /usr/sbin/logrotate -dv /etc/logrotate.conf.
Note with the -d option, logrotate will NOT actually rotate your logs, it must be run without the -d option to rotate.
