Shami's Blog

Sysadmin, Because Even Developers Need Heroes

Rotating Tomcat Logs


Something that had puzzled me for a while, when I first stared working for Integrant Inc. I noticed that the catalina.out file generated by tomcat was growing so big it sometimes reached 3 or even 4GB.

Being a Linux administrator, I directly set up an entry in logrotate.conf to make my life easier, only to find that tomcat stops writing to that file :S

Doing the regular googling I find out that tomcat just directs it’s stdout to catalina.out, and it doesn’t use syslog so moving the file like logrotate does will stop tomcat from logging. The only way after doing that was to restart tomcat, which is not something I would want to do.

catalina.out was actually designed to only display errors from tomcat itself, so it shouldn’t grow big, but if developers tend to log their stuff to it it will grow very big very quickly.

How can be done you ask, well, we have multiple options:

  1. Use log4j, to be honest, Since I don’t know Java I found this one very complicated to set up (Maybe it’s just me), and I think it requires some code change (Honestly I am not sure)
  2. Use the copytruncate options with logrotate, this will only copy the file, and then redirect /dev/null to it so it gets cleared, easy to set up, and does the job, but a race condition will come up while logrotate is copying the file and tomcat tries to log something, the stuff you might need might come at that point in time (Everybody has his day, you know how it goes)
  3. I just noticed this while playing around with Debian on an old test server I’m phasing out, they have a package for tomcat, and it does daily log rotation, I was a little interested in figuring out how, I did an ls -l on the log folder only to find that catalina.out is actually a named pipe, when tomcat directs stuff to catalina.out, a program reads this stuff and puts it in a file with the date appended at the end, which does the job cleanly and easily, the guy who thought of this knows his stuff :)

Just thought I should share this info in case someone was searching.

Have a great week.


About Me

Dev gone Ops gone DevOps. Any views expressed on this blog are mine alone and do not necessarily reflect the views of my employer.

twitter linkedin