Rotating Linux Log Files - Part 1: syslog

Syslog is the default logging application installed in most Linux distributions. It can be replaced with syslog-ng for better functionality, but about this in a future article. As I explained in the introduction, the log files that are managed by syslog are not rotated with logrotate, but by syslog itself. In the second part I will cover the log files that are handled by logrotate.

What files are handled by syslog? We can find out what are those files simply by inspecting the syslog configuration file (/etc/syslog.conf) that defines each log file, and also what kind of information is saved to each particular file. Let’s see how the configuration file looks on a fresh Debian system (I have removed most of the comments and kept only the relevant log definitions):


#  /etc/syslog.conf     Configuration file for syslogd.
#
#                       For more information see syslog.conf(5)
#                       manpage.

auth,authpriv.*                 /var/log/auth.log
*.*;auth,authpriv.none          -/var/log/syslog
#cron.*                         /var/log/cron.log
daemon.*                        -/var/log/daemon.log
kern.*                          -/var/log/kern.log
lpr.*                           -/var/log/lpr.log
mail.*                          -/var/log/mail.log
user.*                          -/var/log/user.log
uucp.*                          /var/log/uucp.log
...
mail.info                       -/var/log/mail.info
mail.warn                       -/var/log/mail.warn
mail.err                        /var/log/mail.err
news.crit                       /var/log/news/news.crit
news.err                        /var/log/news/news.err
news.notice                     -/var/log/news/news.notice
*.=debug;
auth,authpriv.none;
news.none;mail.none     -/var/log/debug
*.=info;*.=notice;*.=warn;
auth,authpriv.none;
cron,daemon.none;
mail,news.none          -/var/log/messages

Normally, I would change this and configure based on my preferences, but his is not the point now. As you can see there are various log files that will contain the information specified by the configured facility (authpriv, cron, daemon, ftp, kern, lpr, mail, mark, news, security, syslog, user, uucp and local0 through local7).

How are these files rotated? As I previously said this is handled by syslog itself, and it is done using 2 cron scripts: daily and weekly.

daily rotation: (handled by /etc/cron.daily/sysklogd)

So we can see here that by default my debian system will keep 7 archives of previous logs (7 days). If I would want to change this, then all I have to do is to replace the -c 7 parameter with what I need.
When does this rotation occur? Since it is launched from /etc/cron.daily/ it is defined in the system wide crontab:

# /etc/crontab: system-wide crontab
...
25 6 * * * root test -x /usr/sbin/anacron || run-parts --report /etc/cron.daily
47 6 * * 7 root test -x /usr/sbin/anacron || run-parts --report /etc/cron.weekly
so based on the default cron job, this will be done daily at 6:25AM.

weekly rotation: (handled by /etc/cron.weekly/sysklogd)

So by default it will keep 4 archives of old logs (without counting the current log); the archives will have the extension: *.0-*.3 (with the first archive not compressed by default). If I would want to change this, I would need to modify accordingly the -c 4 parameter based on my needs.
As seen above in the system crontab, this rotation will take place at 6:47AM each Sunday (the weekly cronjob).

For example, the rotated log files for the messages log file, will look like this:

/var/log/messages
/var/log/messages.0
/var/log/messages.1.gz
/var/log/messages.2.gz
/var/log/messages.3.gz 

Note: On RedHat based systems (RHEL, Centos, Fedora, etc.) the functionality covered above doesn’t exist by default (even though I don’t see why it could not be implemented if someone wants it). On these operating systems, this is handled also by logrotate as shown in the next part.
This covers the basics on how system logs are rotated. In part 2 we will be looking at how application logs are rotated.

Go to:
Rotating Linux Log Files - Part 2: logrotate
Rotating Linux Log Files - Introduction

Tags: , , ,

Share This

Related Posts:

2 Responses to “Rotating Linux Log Files - Part 1: syslog

Leave a Reply

Recommended Sites