Rotate Log Files
Sometimes log files can become quite large. To prevent this, they should be "rotated" frequently.
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.
We cover the configuration of logrotate
for the log files of a Tomcat 8 instance in this article, which are located in /home/www-data/example.ch/logs/catalina.out
.
Configure Logrotate
You will need to add a configuration file (e.g. as a hidden file in the home directory): ~/.logrotate.conf
.
A simple configuration might look like this:
~/*/logs/catalina.out {
daily
rotate 14
notifempty
compress
copytruncate
compresscmd /usr/bin/zstdmt
compressoptions -18 -T0 --rm -qq
compressext .zst
uncompresscmd /usr/bin/unzstd
}
With this configuration, logrotate
:
- rotates all files matching
~/*/logs/catalina.out
once per day - keeps 14 log files
- does not rotate if the file is empty
- compresses the log files with ZSTD
- copies the log file and empties the log file afterwards (
copytruncate
)
This will end up in daily log files like this:
.../catalina.out-20161212.zst
.../catalina.out-20161213.zst
.../catalina.out-20161214.zst
More configuration options can be found in the logrotate
man page.
Execute Logrotate Daily
Logrotate can be executed like this:
/usr/sbin/logrotate -s ~/.logrotate.status.tmp ~/.logrotate.conf
We have to provide a custom state file with the -s
option, since the global state file is only writeable by the root
user.
To execute this command daily, we should add it to the crontab by executing crontab -e
. Simply add following line to the end of the file:
22 0 * * * /usr/sbin/logrotate -s ~/.logrotate.status.tmp ~/.logrotate.conf >/dev/null 2>&1
Now, the logrotate
is executed once a day at 00:22. Be aware that with >/dev/null 2>&1
at the and of the command all output of the command is suppressed.
For more information on configuring cron, see the dedicated Cron Jobs documentation page.