Logrotate

From Gentoo Linux Wiki
Jump to: navigation, search

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

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] Configuration syntax

File: /etc/logrotate.d/example
/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] Known bugs

Be careful when rotating and compressing /var/log/emerge.log files. If you are using genlop you will have to specify every single emerge.log file using option -f (compressed files will work, too), e.g.
genlop -f /var/log/emerge.log -f /var/log/emerge.log.1.gz apache

[edit] Troubleshooting

[edit] logrotate doesn't actually rotate logs

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.

If nothing seems to be wrong there, ensure that your cron is working properly as logrotate depends on this.

[edit] External links

Personal tools
In other languages