The immediate responsibility of every network administrator or engineer is to manage the subnetwork entrusted to him. And in order to understand the current sta...
3v-Hosting Blog
7 min read
Managing logs is a vital part of system administration. Without a structured approach, logs can quickly become unmanageable, consuming valuable disk space and hindering the ability to extract meaningful insights. The logrotate utility is a powerful tool that automates the process of managing logs, ensuring they are rotated, compressed, and removed as needed. This article provides a comprehensive guide to configuring log rotation with logrotate, addressing key features and best practices.
Logrotate is a standard utility in Linux systems designed to manage log file growth. It automates tasks such as:
Rotating logs: Renaming and creating new log files.
Compressing old logs: Reducing disk space usage by archiving logs.
Removing outdated logs: Deleting logs beyond a defined retention period.
Customizing schedules: Adjusting rotation intervals to suit application needs.
By default, logrotate runs periodically through the cron scheduler, ensuring system logs remain manageable and organized.
Logrotate configurations are defined in two primary locations:
Global Configuration File (/etc/logrotate.conf): Contains default settings that apply system-wide unless overridden by individual configurations.
Per-Service Configuration Files (/etc/logrotate.d/): Service-specific settings stored as individual files for fine-grained control.
Key parameters in a logrotate configuration include:
rotate: Specifies the number of log files to retain.
compress: Enables compression of rotated logs.
dateext: Adds a date suffix to rotated log files.
notifempty: Prevents rotation of empty log files.
size: Triggers rotation based on file size instead of time.
To illustrate logrotate's capabilities, let’s configure it for a custom application logging to /var/log/myapp.log. Begin by creating a configuration file:
sudo nano /etc/logrotate.d/myapp
Add the following directives to the file:
/var/log/myapp.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 0640 root root
postrotate
systemctl reload myapp.service
endscript
}
Explanation of Directives:
daily: Rotates the log file daily.
rotate 7: Retains the last 7 logs.
compress: Compresses old log files using gzip.
delaycompress: Delays compression until the next rotation cycle.
missingok: Skips errors if the log file is missing.
notifempty: Avoids rotating empty log files.
create 0640 root root: Creates a new log file with specified permissions.
postrotate/endscript: Executes a command after log rotation, such as reloading the application.
Save the file and exit the editor.
It is critical to test your logrotate configuration to avoid unexpected issues. Use the logrotate command in debug mode:
sudo logrotate -d /etc/logrotate.conf
This command simulates the log rotation process without making changes. It identifies configuration errors and verifies directive behavior.
For manual rotation testing, use:
sudo logrotate -f /etc/logrotate.conf
The -f option forces immediate log rotation.
Logrotate offers advanced features to meet specific needs:
Log Rotation Based on Size: If logs grow unpredictably, rotation can be triggered by size:
/var/log/myapp.log {
size 100M
rotate 5
compress
}
Custom Compression Algorithms: By default, logrotate uses gzip, but other tools like bzip2 or xz can be specified:
compresscmd /usr/bin/bzip2
uncompresscmd /usr/bin/bunzip2
Log Rotation Notifications: Combine logrotate with email alerts to notify administrators of log events:
/var/log/myapp.log {
mail user@example.com
mailfirst
}
For applications managed by systemd, ensure logrotate and systemd are synchronized. The postrotate directive can be used to reload or restart services after rotation. However, avoid unnecessary reloads to reduce system load.
Example:
/var/log/myapp.log {
weekly
rotate 4
postrotate
systemctl reload myapp.service
endscript
}
Plan Retention Policies: Define rotation and retention schedules based on application and compliance requirements.
Use Compression Wisely: Compress large log files to save space but balance the CPU load introduced by compression.
Monitor Disk Usage: Periodically review disk usage to ensure that logrotate policies are effective.
Centralize Logging: Combine logrotate with centralized logging systems like Logstash or Fluentd for efficient monitoring and analysis.
The logrotate utility is indispensable for managing log files in Linux environments. It ensures logs remain manageable, reduces disk space usage, and supports customization for diverse application needs. By understanding its features and applying best practices, administrators can streamline log management and enhance system reliability.
Accelerating WordPress at the Nginx level: correct PHP-FPM settings, try_files, static files, caching, Brotli, wp-login protection, and secure headers for stabl...
Effective backup strategies for Docker applications: how to protect volumes, data, and configurations while avoiding common mistakes, and quickly restore servic...
A clear guide to what VPS is used for: real cases, examples for developers, business setups, VPN, CI/CD and more. Learn how to choose the right VPS plan.