How can we help you today?

How to Setup Log Rotation for the Magento Logs

Created by: Jesper Verkade

Modified on: Fri, 29 May, 2020 at 1:59 PM


Most production shops will have little to no logging configured. In some cases you want the Magento development logging to remain enabled. However, 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.


Configure the Log Rotation

When using logrotate the first step is to create a config file (/data/web/logrotate.conf) with the instructions on when to rotate the logs

# 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 log files
    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.

Please take into account that logrotation can be used for any eCommerce platform. Just make sure that you set the correct path to the location of the logs.

Configure Cronjob to Run Logrotate Periodically

When Logrotate is working, we’ll have to make sure it runs nightly to make sure all log files are rotated frequently. To do this add the following cronjob to your crontab:

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

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

Additional Information

Additional information about how to use logrotate can be found by running the command man logrotate via the CLI on your Hypernode. Furthermore, some great additional examples can be found here

J
Jesper is the author of this solution article.

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.