This article explains Linux log rotation using logrotate. It details configuration via /etc/logrotate.d/, covering options like rotation frequency, file retention, compression, and troubleshooting. The main focus is effective log file management t
Logrotate is a powerful tool in Linux for managing log file sizes and automating their rotation. It prevents log files from growing indefinitely, consuming disk space and potentially impacting system performance. The primary configuration file is /etc/logrotate.conf
, which contains global settings, and individual log file configurations are typically placed in /etc/logrotate.d/
. You can create a new configuration file within /etc/logrotate.d/
for each log file or group of log files you want to manage. Let's create a simple configuration file for a hypothetical log file /var/log/my_app.log
:
<code>/var/log/my_app.log { daily rotate 7 compress copytruncate missingok notifempty }</code>
This configuration tells logrotate to:
To apply this configuration, run logrotate -d /etc/logrotate.d/my_app.log
(the -d
flag runs in dry-run/testing mode) to see what changes logrotate would make, and then run logrotate /etc/logrotate.d/my_app.log
to actually perform the rotation. You can also run logrotate
without any arguments to process all configurations in /etc/logrotate.d/
and /etc/logrotate.conf
. Remember to adjust the configuration options to fit your specific needs and log file size requirements.
Besides the options used in the example above, logrotate offers several other valuable options:
daily
).size 100M
).my_app.log.20241027
).These options provide flexibility in managing log rotation schedules, file retention, and post-rotation actions, allowing for tailored configurations to suit various applications and system requirements. Refer to the man logrotate
page for a comprehensive list of all available options and their detailed descriptions.
If log rotation isn't working as expected, several troubleshooting steps can help identify the problem:
/var/log/logrotate.log
(or a location specified by the log
directive in /etc/logrotate.conf
). Examine this log file for error messages or clues about why rotation failed.-d
(dry-run) option with logrotate
to test your configuration without actually performing rotations.systemctl status logrotate
on systemd systems) and ensuring it's enabled to start automatically on boot.By systematically investigating these aspects, you can pinpoint the cause of the log rotation issues and implement the necessary corrections.
Yes, logrotate can compress rotated log files using the compress
option in its configuration file. As shown in the first example, adding compress
to your configuration will automatically compress the rotated log files using gzip. The compressed files will typically have a .gz
extension. This helps reduce disk space usage, especially for applications generating large log files. Note that compression adds some overhead to the rotation process, so if performance is critical, you might consider using delaycompress
to defer compression until the next rotation.
The above is the detailed content of How do I configure log rotation in Linux using logrotate?. For more information, please follow other related articles on the PHP Chinese website!