Logrotate Magento WordPress and more

  • SumoMe

This article took a lot longer to see the light, because of my self imposed hiatus.

How to use logrotate Magento WordPress

Logrotate is a very simple Linux command yet extremely powerful that you can use to “automate” the whole log cleaning endeavor.

By Definition Logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily,weekly, monthly, or when it grows too large.

Why would you want to use it with Magento? I know what you are thinking Production sites shouldn’t have any logging enabled, period! While I wholeheartedly agree with it, I also now that things happen and well it’s good to be prepared.

Fair enough, now what?

If you want to see an example in your terminal simply do (I am running a Debian based distro at the moment):

cat /etc/logrotate.d/apache2

It should output something like this:

var/log/apache2/*.log {
        weekly
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 root adm
        sharedscripts
        postrotate
                if /etc/init.d/apache2 status > /dev/null ; then \
                    /etc/init.d/apache2 reload > /dev/null; \
                fi;
        endscript
        prerotate
                if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                        run-parts /etc/logrotate.d/httpd-prerotate; \
                fi; \
        endscript
}

I read that output as follows:

Where my files located: var/log/apache2/*.log
What do you want me to do with them: Every week I want you to compress all of the logs and keep 52 saved copies. I want you to delay the compression, notify if the files are empty, ensure that proper permissions are set and well make sure to reload Apache2.

If we want to do the same or something similar for Magento:

vim /etc/logrotate.d/magento_us

Add something similar to this to the file:

var/www/magento_us/var/log/*.log {
        weekly
        missingok #Dont return error if the log file is missing
        rotate 4 #keep only 4 rotations
        compress
        copytruncate #creates a new copy of the file and resets the old one
        size 5m #only keep 5m worth of data
        postrotate #runs after the logrotate happens
               /home/user/notify.sh
        endscript
        maxage 100 # how long do I want the log to exists
        compresscmd /bin/bzip2 # I like bzip2 compression over gz
        compressext .bz2 #without this your extension would be .gz for a bzip2 file
}
var/www/magento_us/var/reports/* {
        daily
        missingok #Dont return error if the log file is missing
        rotate 5 #keep only 5 rotations
        compress
        copytruncate #creates a new copy of the file and resets the old one
        size 5m #only keep 5m worth of data
        postrotate #runs after the logrotate happens
               /home/user/notify.sh
        endscript
        maxage 100 # how long do I want the log to exists
        compresscmd /bin/bzip2 # I like bzip2 compression over gz
        compressext .bz2 #without this your extension would be .gz for a bzip2 file
        olddir reports.new
}

For WordPress:

vim /etc/logrotate.d/wordpress_us
var/www/wordpress/wp-content/*.log {
        weekly
        missingok #Dont return error if the log file is missing
        rotate 4 #keep only 4 rotations
        compress
        copytruncate #creates a new copy of the file and resets the old one
        size 5m #only keep 5m worth of data
        postrotate #runs after the logrotate happens
               /home/user/notify.sh
        endscript
        maxage 100 # how long do I want the log to exists
        compresscmd /bin/bzip2 # I like bzip2 compression over gz
        compressext .bz2 #without this your extension would be .gz for a bzip2 file
}

What I you wanted both of them in just one file? Hmmm not sure why you’d want that but here it is:

"var/www/wordpress/wp-content/*.log" var/www/magento_us/var/log/*.log {
        weekly
        missingok #Dont return error if the log file is missing
        rotate 4 #keep only 4 copies :-)
        compress
        copytruncate #creates a new copy of the file and resets the old one
        size 5m #only keep 5m worth of data
        postrotate #runs after the logrotate happens
               /home/user/notify.sh
        endscript
        maxage 100 # how long do I want the log to exists
        compresscmd /bin/bzip2 # I like bzip2 compression over gz
        compressext .bz2 #without this your extension would be .gz for a bzip2 file
}

Note, if you use the code above, remove all the lines after the “#” character, there are in there mostly for documentation and understanding! The /home/user/notify.sh is a simple script to notify when the logs are rotated. If you want to get the actual logs you could add this:

 mail email@domain.com

If you are in a shared hosting environment chances are you don’t have sudo or root permissions, to circumvent such problem you can modify your own crontab:

crontab -e

Add something like this:

@weekly /usr/sbin/logrotate -f /home/user/magento.conf

This cronjob will run weekly and use the instructions found in the magento.conf file to rotate the files.

You now have the same effect.

And this how you can logrotate Magento WordPress and probably any other type of log files under the sun.

Keep in mind that you should use something like graylog2 if you have multiple servers or just in general if you want to have a powerful visualization tool and analytics.

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)

Author: Luis Tineo

Husband, Father, performance improvement junkie, biker and video gamer, Linux user and in my day job I'm a Systems Architect at Blue Acorn.

Share This Post On