Thoughts by Techxplorer

Thoughts on my experiences with technology

Creating a log file in Apache Tomcat 6

By: Tattooed JJ

The main issue I have with developing web based application with Java and Apache Tomcat is that there always seems to be a bazillion ways of doing something and none of them documented very well. Or at least not with documentation I can find on the Interweb.

Recently with my work for AusStage I needed to create a log that included some additional information that isn’t included in the standard access logs. From what I have been able to learn from the Interweb there are a number of different ways to do logging in a Tomcat based environment. One of the main sources of information I found was the Logging in Tomcat page in the Tomcat Documentation.

After a great deal of searching this is the technique that I’ve developed for logging in my application to date.

The first step is to have a file called logging.properties in the WEB-INF/classes directory of your web application. For me this meant putting it into the root of my source tree. The text inside the file is as follows:


handlers = org.apache.juli.FileHandler

############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################

org.apache.juli.FileHandler.level = FINE
org.apache.juli.FileHandler.directory = ${catalina.base}/logs
org.apache.juli.FileHandler.prefix = file-name-prefix

This file is based on the second example on the Logging in Tomcat page in the Tomcat Documentation. Essentially it means to create a log file in the standard logs directory, as specified by ${catalina.base}/logs, and with the file prefix file-name-prefix. All of the remaining settings are left at their defaults.

To add an entry to the line I use a call to the log() method of the ServletContext object that is available to my servlet via the getServletContext() method of the ServletConfig object. The ServeltConfig object is passed into the init() method of my Servlet, an object which is a child of the HttpServlet class which is in turn a child of the GenericServlet and I store a reference to it for when I need it as a private variable.

The “Logs” photo was uploaded to Flickr by Tattooed JJ and used under the terms of a Creative Commons license.

Category: Thoughts
Tag: