Tomcat log cutting (logrotate) is described in detail

  • 2020-05-30 21:42:15
  • OfStack

Tomcat log cutting

logrotate is a powerful system software, it has a 1 set of complete operating mode on the log file, such as: dump, mail and compression, etc., and the default logrotate added to cron (. / etc cron daily/logrotate) as a daily task execution. With logrotate automatically, I don't want to write my own log cutting scripts anymore.

Tomcat log catalina.out log cut as follows

# ls -lh /usr/local/tomcat/logs/catalina.out
-rw-r--r-- 1 www www 14M Aug 28 15:55 /usr/local/tomcat/logs/catalina.out

Configure logrotate to catalina.out log cutting

# cat /etc/logrotate.d/tomcat
/usr/local/tomcat/logs/catalina.out {
rotate 5

Details of parameters:

daily specifies a daily dump cycle rotate 5 specifies the number of times the log file was dumped before it was deleted, 0 for no backups and 5 for keeping 5 backups missingok ignores this warning if the log does not exist dateext file suffix is the date format, that is, the cut file is: xxx.log-20150828.gz compress compressed the logs after the dump through gzip (gzip-d xxx.gz) notifempty is not dumped if it is empty copytruncate is used for log files that are still open to back up and truncate the current log

Immediately truncate the log:

# logrotate --force /etc/logrotate.d/tomcat

The effect is as follows:

# ls -lh /usr/local/tomcat/logs/catalina.out* 
-rw-r--r-- 1 www www  0 Aug 28 16:00 /usr/local/tomcat/logs/catalina.out 
-rw-r--r-- 1 www www 1.1M Aug 28 16:00 /usr/local/tomcat/logs/catalina.out-20150828.gz 

Thank you for reading, I hope to help you, thank you for your support of this site!

Related articles: