Rotating Magento Log Files with Linux logrotate
If you’re running a Magento website on a Linux server, then there is a handy Linux utility which you can use to split your Magento log files located at /var/log
depending on a variety of criteria. Over time, providing you have logging enabled, you may find that these log files can get quite large in size and each time Magento writes to these, it can become quite a slow process (and of course difficult to debug/find related errors to issues).
Navigate to /etc/logrotate.d
and create a new file with a name that will easily distinguish which site it is for, for example ‘magento-domain.com’ (obviously replace domain.com with your domain.
There are lots of configurable options for logrotate so I would recommend reading the manual but for the sake of this example, we will set ours up to rotate the logs on a monthly basis so the contents of our file, we’ve used (obviously replace the path as necessary):-
1 2 3 4 5 6 7 8 |
/var/www/vhosts/domain.com/httpdocs/var/log/*.log { monthly rotate 12 missingok copytruncate notifempty dateext } |
The purpose of this is so that we can use a separate log file for each month of the year and only keep the last year’s worth of log files (12 months – rotate 12).
We then need to create a cron job to run at the very start of every month:-
0 0 1 * * /usr/sbin/logrotate -f /etc/logrotate.d/magento.domain.com.
What you’ll end up with at the start of every month then is a new log file created labelled like so:-
nameoflogfile.log-yyyy/mm/dd
For example:-
system.log-20150101
system.log-20150201
system.log-20150301
system.log-20150401
There are of course many options for logrotate so you can tailor your log rotation specific to your requirements.