Ever found yourself managing a system that generates logs at an alarming rate? Log rotation is a vital process to monitor, understand, and manage these logs effectively. Logrotate, a powerful tool in Linux, streamlines this process. Here, we'll dive into practical examples of how to configure logrotate for optimal performance.
Before we delve into specific examples, let's ensure we're on the same page. Logrotate isn't just about rotating logs; it also compresses, removes old logs, and mails log summaries, among other functionalities. By default, it processes logs located in the /var/log directory on a daily basis, automatically rotating them.
Logrotate Config File locations and Syntax
Understanding where logrotate stores its configuration files and how it interprets them is crucial. The main logrotate configuration files are stored under the /etc/logrotate.d/ directory. Each file in this directory corresponds to a log file - the name of the file is the log file name itself.
The syntax for logrotate's configuration is straightforward. Here's a simple breakdown: Each line of the config file can define:
- /path/to/file - The log file to be managed.
- options - Manipulation options, like daily, rotate 5, compress, or size 100K.
Config File Basics
Let's examine a basic logrotate configuration file. For instance, consider the log file /var/log/messages.
If we want to rotate this log file daily for the past seven days, and keep a week's worth of backups, we would configure logrotate like this:
/var/log/messages daily rotate 7Logrotate Examples
You can specify additional options to tailor logrotate to your needs. For example, you might decide to:
- Only rotate if the log file size is greater than 100KB: size 100K
- Compress the log files to save space: compress
- Mail log summaries to a specified address: mail [email protected]
- Delay rotation until the last log file is closed: delaycompress
Logrotate Master Configuration File
Sometimes, you might want to configure default behavior for all your logs. The master logrotate configuration file, located at /etc/logrotate.conf, allows you to do this. For instance,
To set the log directory to /var/log/mylogs and rotate logs weekly, you could add the following:
logdir /var/log/mylogs weekly