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.
[tags]tomcat4,logfile rotation[/tags]
Oh yeah, if you’re a really anal sysadmin (and I wouldn’t want to work with any other kind), you’ll note that catalina.out is still growing–every time you restart tomcat, a new set of shutdown/startup messages is added.
To deal with this, I’d just modify the shell script that starts tomcat to rotate catalina.out during that short period of time where tomcat is not running.
However, in theory there’s no useful information in generic startup messages, so I might just delete the old catalina.out on restart, rather than rotate it.
hi,
I have a doubt in configuring in log4j….
i have seen your sample code of configuring using PropertyConfigurator..that works with log4j.properties…
i need to do the same with log4j.xml….
i have to make the filename dynamic using DOM Configurator…
do u have any ideas..
please send me a reply:jintothomasm@gmail.com
thanks
jinto
Hi Jinto,
I think the answer is to manipulate DOM object, just like I describe manipulating a Properties object here: http://www.mooreds.com/wordpress/archives/000238
doConfigure(String) might be a useful method too: http://logging.apache.org/log4j/docs/api/org/apache/log4j/xml/DOMConfigurator.html#doConfigure(java.lang.String,%20org.apache.log4j.spi.LoggerRepository)
You can also use logrotate on linux/unix machine to rotate catalina.out without restarting tomcat.
http://www.vineetmanohar.com/2010/03/24/howto-rotate-tomcat-catalina-out/
Hi Vineet,
Useful post. It’s worth calling out a part of your comment to your post from the man page: “Note that there is a very small time slice between copying the file and truncating it, so some logging data might be lost. ”
Sometimes (often) that is acceptable, sometimes it is not.
Thanks for the feedback – I will highlight it in the main post.
In general you can clobber a log file and regain its disk space (even if a process is holding it open) using some command that truncates it. Say, for example
# echo ” > catalina.out
This won’t help you if you *wanted* the data in the file, obviously, but if all that’s left there is startup and shutdown messages and for whatever reason you don’t want to restart tomcat at that moment, it might do the trick.
I have done it for rotating catalina.out to be cleaned once in a week.
that is logs directory will contain ;
1. catalina.Mon.out
2. catalina.Tue.out…etc..
7.upto Sunday..
8. catalina.out
Now the logic is;
a. Change bin/catalina.sh to create each 7 files as told above
b. Make catalina.out null
c. Change bin/catalina.sh to create a soft link to catalina.out per day
simply;
touch catalina.`date %a….`.out [Forget the format]
echo /dev/null > catalina.out
ln -sf catalina.`date %a….`.out catalina.out
sorry forget to add to my first comment
simply;
xfile=”catalina.`date %a….`.out”; [Forget the format]
[ -f $xfile ] && echo /dev/null > $xfile || touch $xfile
echo /dev/null > catalina.out
ln -sf catalina.`date %a….`.out catalina.out
In the tomcat log directory:
find . -mtime +1 -exec gzip -9 {} ;
Matías Colli
Perito Informático Forense
M.N A-120 COPITEC
find . -mtime +1 -exec gzip -9 {} ;
Agrego que el 1 es la cantidad de días, puede ser 2, 3, etc.
Matías Colli
Perito Informático Forense
M.N A-128 COPITEC
http://estudiopericialinformatico.com