Magento Log Optimisation

Time to Read: 10 Difficulty Level: Intermediate
Tools Needed: Plesk Access Last Updated: 25/11/2024

This guide will provide steps on how to set up log rotation for your magento logs

With hosting a Magento site, actions, errors and requests will all be logged to Magento’s log’s. These can grow to be very large. This guide is here to show you how to set up log rotation for Magento’s logs and keep the logs at a manageable level, whilst also allowing you to choose how many days worth of data is logged on the server.

Why is Log Rotation needed?
Where to Setup Log Rotation?
Log Rotation Configuration
Triggering the Log Rotation

Why is Log Rotation needed?

Magento’s log’s can grow to be very large, very quickly. When that happens, this can contribute towards using your server’s disk space. This can cause some side effects of backups not running, the server filling up, and can cause the server to crash if it reaches 100% full. Whilst it is possible to flush Magento’s logs emptying the log or deleting the logs, adding some form of log rotation would be the best option here. This will allow you to manage your logs to keep 4 days worth of logs, compress the files and stop these from growing and growing.

Where to Setup Log Rotation?

Log rotation is set up by creating a configuration file and a cron to run the config file. We would typically recommend setting this up outside of your document root (typically httpdocs - /var/www/vhosts/<your-domain.tld>/httpdocs), for example, in /var/www/vhosts/<your-domain.tld>/private.

In your Plesk Control Panel, you can navigate to

  • Click your Subscription
  • Click Files
  • On the left hand-side menu, Click Home Directory
  • Click the + icon
  • Create Directory
  • Name the directory, in this example, we are calling the directory Private

Log Rotation Configuration

Navigate to the newly created private folder, within here we want to create a new file called magento_logrotate.conf. Within this file, we want to include the following code below. This file’s aim is to copy the content onto another file and have four days of logs stored, compressing the files.

~/httpdocs/var/log/*log {
copytruncate
rotate 4
daily
compress
missingok
notifempty
}

Triggering the Log Rotation

To make the Log Rotation work, we need to set up a cron to trigger the request. This can be setup within Plesk:

  • In your Plesk Control Panel, Click Subscriptions
  • Click on your Domain
  • Click Scheduled Tasks on the right hand-side pane
  • Click Add Task
  • Within this page you can create a command, paste the command below into the command field. Set this to run Daily, we recommend around 6AM, and Press OK to confirm the cron.

This will run the cron on a daily basis, managing your logs going forwards.

/sbin/logrotate -s ~/private/magento_logrotate.status ~/private/magento_logrotate.conf