How to use logrotate with Apache's Tomcat (and any other Java process)

Logrotate is an excellent utility for managing log files that can start grow into an unmanageble size, Tomcat's catalina.out is one such file.

Most of the solutions (scroll down to the 'Rotate the logs' section) for managing catalina.out, or other log files generated by Tomcat or any Java process for that matter, don't mention the copytruncate option:

Truncate the original log file in place after creating a copy, instead of moving the old log file and optionally creating a new one, It can be used when some program can not be told to close its logfile and thus might continue writing (appending) to the previous log file forever. Note that there is a very small time slice between copying the file and truncating it, so some logging data might be lost. When this option is used, the create option will have no effect, as the old log file stays in place.
This basically means that you don't have to configure your logrotate script to restart Tomcat just so you can tidy up your log files - it trades off uptime against completeness of log records.

I found out about this from the Mission Data Blog in their post titled 'managing disk space with logrotate'. Is definitely worth taking a good look through the man page for logrotate to see if there are any other gems you can take advantage of for your setup.

Technorati Tags: , , ,

2 comments:

logrotate said...

Excellent article companion.

Logrotate is tremendously useful to be able to support of optimized form our files logs during a long period of time, since beside rotating them we can compress them automatically if we consider it opportune.

Logrotate is an indispensable tool.

A greeting,
Alejandro Arco

en said...

Thank you. Your post was very helpful.