Log files are an integral part of Linux systems, providing valuable insights for monitoring and troubleshooting. However, over time, log files can grow exponentially, occupying excessive disk space. To address this issue, Linux offers the logrotate command, a powerful utility that automates log file management. We will explore the logrotate command, including its functionality, usage, and how to configure it to automatically rotate larger Apache HTTPD logs.

Understanding Logrotate:

Logrotate is a versatile command-line tool designed to automate the management of log files. It rotates, compresses, and purges log files based on predefined criteria, ensuring efficient disk space utilization. Logrotate operates through a set of configuration files, typically stored in the /etc/logrotate.d/ directory, which specify the log files to rotate and the desired rotation parameters.

Basic Syntax:

The logrotate command follows the following syntax:

logrotate [options] <config_file>

Now, let’s delve into the functionality of logrotate by exploring a few examples and explaining them in detail.

Example 1: Rotating a Log File Based on Size:

Suppose we have a log file named /var/log/myapp.log, and we want to rotate it when it reaches a size of 10MB. To achieve this, we can create a logrotate configuration file, say /etc/logrotate.d/myapp, with the following contents:

/var/log/myapp.log {
    size 10M
    rotate 5
    compress
    delaycompress
    notifempty
    missingok
}

Explanation:

  • size 10M: Specifies the maximum size at which the log file should be rotated.
  • rotate 5: Retains up to 5 rotated log files, removing older ones as new rotations occur.
  • compress: Compresses the rotated log files using gzip, saving disk space.
  • delaycompress: Delays compression of the most recent rotated log file until the next rotation cycle.
  • notifempty: Skips rotation if the log file is empty.
  • missingok: Ignores errors if the log file is missing.

Example 2: Rotating Log Files Based on Time Interval:

Let’s consider another scenario where we want to rotate log files on a daily basis. We can create a logrotate configuration file, e.g., /etc/logrotate.d/syslogs, with the following contents:

/var/log/syslog
{
    daily
    rotate 7
    compress
    delaycompress
    missingok
    notifempty
    create 644 root adm
    sharedscripts
    postrotate
        /etc/init.d/rsyslog restart >/dev/null 2>&1 || true
    endscript
}

Explanation:

  • daily: Specifies a daily rotation interval.
  • rotate 7: Keeps up to 7 rotated log files, removing older ones as new rotations occur.
  • compress and delaycompress: Compresses the rotated log files, deferring compression of the most recent log file until the next rotation cycle.
  • missingok: Ignores errors if the log file is missing.
  • notifempty: Skips rotation if the log file is empty.
  • create 644 root adm: Creates a new log file with the specified permissions and ownership if it doesn’t exist.
  • sharedscripts: Executes the postrotate script only once after all log files have been rotated.
  • postrotate and endscript: Encloses the post-rotation script section, restarting the rsyslog service in this example.

Configuring Automatic Log Rotation for Apache HTTPD Logs using logrotate:

Apache HTTPD generates log files that can rapidly grow in size, requiring regular rotation. To configure automatic log rotation for larger Apache HTTPD logs using logrotate, follow these steps:

Step 1: Create a Logrotate Configuration File:

  1. Open a terminal and navigate to the /etc/logrotate.d/ directory.
  2. Create a new file for Apache log rotation using a text editor. For example:
   sudo nano /etc/logrotate.d/apache2

Step 2: Specify Log Rotation Settings:
Inside the logrotate configuration file, add the following code to define the rotation settings for Apache logs:

/var/log/apache2/*.log {
    rotate 7
    weekly
    missingok
    notifempty
    compress
    delaycompress
    sharedscripts
    postrotate
        /etc/init.d/apache2 reload > /dev/null
    endscript
}

Explanation of the Configuration:

  • /var/log/apache2/*.log: Specifies the log files to rotate. Adjust the path and file pattern according to your Apache log file location.
  • rotate 7: Keeps 7 rotated log files. You can modify the number to retain a different number of log files.
  • weekly: Rotates the logs on a weekly basis. You can use other time-based options such as daily, monthly, or yearly.
  • missingok: Ignores log files that are missing.
  • notifempty: Does not rotate an empty log file.
  • compress: Compresses rotated log files using gzip.
  • delaycompress: Delays compression of the most recent rotated log file until the next rotation cycle.
  • sharedscripts: Executes the postrotate script only once after all log files have been rotated.
  • postrotate and endscript: Encloses the post-rotation script section.
  • /etc/init.d/apache2 reload > /dev/null: Reloads the Apache service to ensure it uses the new log file. Modify the command if you are using a different method to reload Apache.

Conclusion:

Logrotate is an essential tool for managing log files in Linux systems efficiently. By automating log rotation, compression, and purging, logrotate ensures optimal disk space utilization and simplifies log analysis and troubleshooting tasks. Through examples and explanations, we have explored the logrotate command’s functionality and how to configure it to automatically rotate larger Apache HTTPD logs. By implementing log rotation effectively, you can maintain a well-organized log system and ensure the smooth operation of your Linux environment. Make sure to read the man page.