Knowledgebase:
Rotating stdout logfiles with Tomcat
Posted by Greg Pettit, Last modified by on 22 November 2007 08:33 PM

Note: This article assumes that you are using Apache Software Foundation's Tomcat 6.0 to deploy a server application (Unlimited FTP Servlet, FileCatalyst Web, Files2U, Doctera). It is likely that the information and files provided will also work with Tomcat 5.5 or 6.x+, but only 6.0 has been tested. Always make backups before replacing or modifying files.

---

Tomcat's default logging characteristics are more than up to the task of recording a huge number of messages; the daily rotation is sufficient for most needs. However, in certain environments or under certain conditions you may have noticed your "stdout.log" file (in your Tomcat/logs/ directory) is growing to unmanageable sizes during the course of a day. We have created a drop-in-place solution that will provide size-based rather than daily rotation. The files are rotated based on:

- Maximum File Size for an individual logfile (before a new one is started)
- Maximum number of logfiles to keep before rotating

Follow these steps to implement custom size-based log rotation:

1. Download and unzip this archive: http://www.utechsoft.com/support/documentation/tomcat_log_rotation.zip
2. Stop Tomcat
3. Copy tomcat-juli.jar to /Tomcat/bin
4. Copy tomcat-juli-adapters.jar to /Tomcat/lib
5. Copy log4j-1.2.15.jar to /Tomcat/lib
6. Copy log4j.properties to /Tomcat/lib
7. Copy tomcat_log_test.jsp to /Tomcat/webapps/ROOT
8. Edit log4j.properties.  MaxFileSize sets the threshold when the log file gets rotated.  MaxBackupIndex sets the number of rotated log files that are kept before the oldest gets deleted.
9. Edit /Tomcat/conf/context.xml to add swallowOutput="true" to the Context tag.  This must be set inside the “greater than/less than” braces.
 
Example:
 
<!-- The contents of this file will be loaded for each web application -->
<Context swallowOutput="true">

 
This will redirect the stdout to the a custom log file called "tomcat.log". Note that the stdout logs will still be created, but will contain no messages.
 
10. Start Tomcat
11. Check if the configuration was successful:
  a) go to your /Tomcat/logs directory to observe creation of the logs
  b) if tomcat.log exists, you have implemented the new log rotation correctly
  c) test rotation by pointing a browser at: http://localhost:8080/tomcat_log_test.jsp (substitute with appropriate host and port if applicable), which will simply create a harmless set of errors to dump to the log. Refresh this page as many times as necessary.

Once the tomcat.log file reaches the threshold size (MaxFileSize), new incremental files will appear, named: tomcat.log.1, tomcat.log.2, tomcat.log.3 etc. (up to the MaxBackupIndex number).  Once the MaxBackupIndex is reached, the oldest current tomcat.log.x file will be deleted and the rest will be rotated through.


Comments (0)