Setup log rotation for Magento logs

in Tools

Most production shops, wil have no logging configured. In some cases you want the Magento development logging to remain enabled.

A continuous growing log file in /data/web/public/var/log will reduce speed of your shop, so it’s important to avoid a log file that is bigger than a few MB’s.

To make sure the log files don’t grow exhaustively, we can use logrotate, the linux utility that rotates logfiles.

Configuring log rotate

To make use of Logrotate, first create a config file /data/web/logrotate.conf:

# Rotate logs for production
/data/web/public/var/log/*.log {
    # Rotate daily
    daily
    # Rotate when the size is bigger than 59MB
    size 50M
    # Compress old logfiles
    compress
    # Always keep 4 weeks of log files
    rotate 4
    # Create a new log file with mode 0600
    create 0600
    # Don't rotate file if not found
    missingok
    # Don't rotate log if file is empty
    notifempty
}

# Rotate logs for staging
/data/web/staging/var/log/*.log {
    daily
    compress
    size 50M
    rotate 4
    create 0600
    missingok
    notifempty
}

Now test if your config file is working:

logrotate -v /data/web/logrotate.conf --state /data/web/.logrot_state ; echo $?

If the returned exit code on the end of the output is 0, all is well and we’re ready to configure a cronjob that triggers logrotate.

Running logrotate periodically

When Logrotate is working, we’ll have to make sure it is run nighly to make sure all log files are rotated frequently

To do this add the following cron to your crontab:

30 * * * *  /usr/sbin/logrotate /data/web/logrotate.conf --state /data/web/.logrot_state

Now every hour the Logrotate cronjob will verify your logfiles and rotate when they’re getting bigger then 50MB or when the day is over.

Additional info

Additional info can be found using the man command: man logrotate
Or in this howto with some great additional examples.

 

0