There seems to be some confusion regarding rotating tomcat logs, in particular rotating
catalina.out without restarting Tomcat. With Tomcat4,
catalina.out contains everything the application developers logged using
System.out.println. Because of the way that Tomcat holds onto the file descriptor on Unix, simply moving the file doesn’t work: Tomcat merrily writes to a file that you can no longer access.
This is important to do for the same reasons you should rotate any other system log; disk space is cheap, but logs can fill it quickly, and the information value of logs decreases rapidly with time. Rotating logs typically means you remove them from the filesystem, but you can also archive them off line with tape, or to a SAN, etc. I’ve run into issues with JSVC and large log files too.
There’s an entry in the FAQ and a pointer to a maillist discussion. But you have to dig a bit and I thought I’d just document what I did. (This works for Tomcat4–Tomcat 5.5 did away with the Logger element and I’m guessing you’d just use some of the log4j configuration and RollingFileAppender.)
First off, you want to make sure that you’re rotating your other logs. This is typically done with a Logger component, configured either at the host or context level. Set the timestamp attribute to be ‘true’.
Now Tomcat is handily rotating all your host and context level logs. You may want to gzip or delete old ones via find:
/usr/bin/find ./logs -mtime +60 -name "mylog_log*"|xargs rm -f
The next step is to set the
swallowOutput attribute to ‘true’ on all contexts. According to the docs, this attribute causes
the bytes output to System.out and System.err by the web application [to] be redirected to the web application logger.
Now you have to restart tomcat. But from here on out, Tomcat will rotate all configured logs, and catalina.out will only contain start and stop messages.
Oh yeah, and just as that email thread does, I heartily condemn using System.out.println because log4j is so easy to setup and use. But I’ve written code like that (admittedly, usually with a sheepish grin on my face), so I can’t cast the first stone.
Technorati Tags: tomcat4, logfile rotation