Log Rotation for Application Logs in Linux

Log rotation is a crucial task for managing application logs efficiently and preventing them from consuming excessive disk space. If your application logs are stored in a different location, you can still perform log rotation using the logrotate tool. In this guide, we’ll walk through the steps of setting up log rotation for application logs in a specific directory.

1. Identify Log Location:

Determine the directory where your application logs are stored. For this example, let’s assume the logs are in /path/to/app/logs/.

2. Create a Logrotate Configuration File:

Create a dedicated logrotate configuration file for your application logs in the /etc/logrotate.d/ directory.

Example (/etc/logrotate.d/my_app_logs):

/path/to/app/logs/*.log {
    weekly
    rotate 4
    create
    compress
    delaycompress
    notifempty
    missingok
}

Adjust the path and log file extension based on your actual log file locations and names.

3. Configure Log Rotation Parameters:

  • weekly: Rotate logs weekly. Customize to daily, monthly, etc.
  • rotate 4: Keep four rotated log files.
  • create: Create a new empty log file after rotation.
  • compress: Compress rotated log files using gzip.
  • delaycompress: Postpone compression until the next rotation cycle.
  • notifempty: Do not rotate an empty log file.
  • missingok: Ignore missing log files.

4. Test Manually:

Manually test the log rotation process before relying on the automatic cron job.

sudo logrotate -v /etc/logrotate.d/my_app_logs

5. Verify Log Rotation:

After log rotation, verify that the log files have been rotated correctly.

ls -l /path/to/app/logs/

6. Automation with Cron:

Configure logrotate to run automatically through cron. Ensure the cron job for logrotate is set up to run at the desired frequency.

7. Adjust Logrotate Parameters:

Customize logrotate parameters based on specific requirements. Add post-rotation scripts, adjust rotation frequency, or tailor other parameters to fit your needs.

8. Monitor and Troubleshoot:

Monitor the log rotation process and, if issues arise, check logrotate logs and configuration for errors.

cat /var/log/logrotate.log

9. Example with Size-Based Rotation:

/path/to/app/logs/*.log {
    size 100M
    rotate 4
    create
    compress
    delaycompress
    notifempty
    missingok
}

This example rotates logs based on a size limit of 100 megabytes.

By following these steps, you can set up log rotation for application logs located in a different directory. Customize the configuration based on your specific application and logging requirements.