How do I rotate log files?

I had a hectic time playing with the logs yesterday. I have a VirtualBox setup on Dell Lappy and was exploring more on Logging this time.In one of the interview I was asked on "Logging" and thought to give it a little more exploration.Here is what I really get to know about the logrotate.

Happy Reading !!

The rotation of log files can be done with the logrotate.

logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.

The /etc/logrotate.conf is the main configuration file for log rotation. This file is pretty self explanatory. Some important values to keep in mind:

# rotate log files weekly

# keep 4 weeks worth of backlogs
rotate 4

Normally, logrotate is run as a daily cron job. It will not modify a log multiple times in one day unless the criteria for a log is based on the logs size and logrotate is being run multiple times each day.

For example, to change the log setting for CUPS, follow the steps below:

Edit /etc/logrotate.d/cups file and add the following lines:

# Log files are rotated times before being removed If count is 0, old versions are
removed rather then rotated.
# Log files are rotated when they grow bigger then size bytes. If size is followed by M,
the size if assumed to be in megabytes. If the k is used, the size is in kilobytes. So size
100, size 100k, and size 100M are all valid.
# Old versions of log files are compressed with gzip by default.

The file should look similar to the following:

/var/log/cups/*_log {
size 100k # log files will be rotated when they grow bigger that 100k.
rotate 5 # will keep the logs for 5 weeks.
compress # log files will be compressed.
/etc/init.d/cups condrestart >/dev/null 2>1 || true

Save the file and exit.

No comments:

Post a Comment