Linux Tutorial
Linux File/Directory Management
Linux Packaging And Compression
Vim Text Editor
Linux Text Processing
Linux Software Installation
Linux User/User Group Management
Linux Permission Management
Linux Filesystem Management
Linux Advanced Filesystem Management
Linux System Management
Linux Backup and Recovery
Linux System Service Management
Linux System Log Management
Linux Boot Management
LAMP/LNMP Environment
SELinux Management
In Linux, log rotation is crucial to prevent log files from consuming too much disk space and to maintain a manageable archive of log entries. Log rotation, also known as log dumping, involves periodically archiving, compressing, and deleting log files. The logrotate
utility is commonly used for this purpose in Linux. In this tutorial, we will explain the basics of log rotation and provide a step-by-step guide to set up log rotation for your applications.
1. Understanding Log Rotation
Log rotation typically includes the following steps:
These steps help to manage log files efficiently and ensure that the system does not run out of disk space.
2. Using logrotate
As explained in the previous tutorial, logrotate
is a Linux utility that manages log rotation. It is typically configured through its main configuration file, /etc/logrotate.conf
, and additional configuration files in the /etc/logrotate.d/
directory.
3. Set Up Log Rotation for a Custom Application
To set up log rotation for a custom application, follow these steps:
a. Create a new logrotate configuration file for your application in the /etc/logrotate.d/
directory. Replace myapp
with your application's name:
sudo nano /etc/logrotate.d/myapp
b. Add the following content to the file, adjusting the parameters to fit your needs:
/var/log/myapp/*.log { daily rotate 7 compress missingok notifempty create 0640 myapp myapp }
In this example:
/var/log/myapp/*.log
specifies the log files to rotate.daily
rotates the log files daily.rotate 7
keeps the last seven rotated log files.compress
compresses the rotated log files using gzip
.missingok
ignores the absence of log files without raising an error.notifempty
skips rotation if the log file is empty.create 0640 myapp myapp
creates a new log file with the specified permissions (0640), owner (myapp), and group (myapp) after rotation.c. Save and close the file.
d. Test your configuration with the -d
option:
sudo logrotate -d /etc/logrotate.d/myapp
e. If there are no issues in the output, run logrotate
manually with your custom configuration:
sudo logrotate /etc/logrotate.d/myapp
4. Automatic Rotation
logrotate
is usually run automatically via a scheduled job defined in /etc/cron.daily/logrotate
(or /etc/cron.weekly/logrotate
). The system automatically processes the log files according to the configuration files found in /etc/logrotate.d/
.
By following these steps, you can set up log rotation for your custom applications and manage log files effectively. Log rotation helps to maintain a clean log file archive and prevent log files from consuming excessive disk space.
How to configure log rotation in Linux:
Configuration is typically done in the /etc/logrotate.conf
file and additional configurations in the /etc/logrotate.d/
directory. Edit these files to define log rotation settings.
Example /etc/logrotate.conf
configuration:
rotate 4 weekly compress
Setting up log rotation for system logs in Linux:
System logs are usually configured in the /etc/logrotate.conf
file. Customize settings based on your preferences, such as rotation frequency and compression.
Example system logs configuration:
/var/log/syslog { rotate 7 daily compress }
Customizing log rotation frequency in Linux:
Customize rotation frequency using directives like daily
, weekly
, or monthly
to control how often log rotation occurs.
Example daily rotation configuration:
/var/log/mylog { rotate 5 daily compress }
Rotate logs on size in Linux:
Specify the log file size at which rotation should occur using the size
directive.
Example size-based rotation configuration:
/var/log/mylog { size 100M rotate 5 compress }
Log rotation and compression in Linux:
Compress log files to save disk space by including the compress
directive in the logrotate configuration.
Example compression configuration:
/var/log/mylog { rotate 5 daily compress }
Managing log files with logrotate on Linux:
logrotate
simplifies log file management. It rotates, compresses, and optionally deletes old log files, ensuring efficient use of disk space.
Example log file management configuration:
/var/log/mylog { rotate 5 daily compress missingok notifempty }