Setting up your application log with Logrotate

First story

Recently, our server was down, the main reason because the log files burned out our storage. We must find a way to restart the server, the temporary solution at that time was zip the logs and create new logs and then restart the server.

After that, I spend the time to research about Logrotate and init the Logrotate configurations for our servers. By the way, Logrotate is a system utility that manages the automatic rotation and compression of log files. This means by using the Logrotate, your log files can be compressed after a specific time and Logrotate will init an empty log file for you and you don’t need to get your server to switch over to the new log file.

Getting Started

Logrotate is installed by default on Ubuntu 16.04, and is set up to handle the log rotation needs of all installed packages, including rsyslog, the default system log processor.

Step 1: Enable the compress feature

Open the /etc/logrotate.conf file and remove the comment sign for compress feature

compress: gzip the log rotation. This uses gzip by default and results in files ending in .gz.

delaycompress: When delaycompress is active, an archived log is compressed the next time that the log is rotated. This can be important when you have a program that might still write to its old log file for a time after a fresh one is rotated in. Note that delaycompress works only if you have compress in your configuration.

copytruncate: copy the log files and then empties it. This makes sure the log files always exists so you don’t need to reload the server.