Logging in Tomcat
This logging is performed according to the Tomcat logging configuration. You cannot overwrite it in a web application.
The Servlets logging API predates the java.util.logging API that is now provided by Java. As such, it does not offer you much options. E.g., you cannot control the log levels. It can be noted, though, that in Apache Tomcat implementation the calls to ServletContext.log(String) or GenericServlet.log(String) are logged at the INFO level. The calls to ServletContext.log(String, Throwable) or GenericServlet.log(String, Throwable) are logged at the SEVERE level.
- Uncaught exceptions printed by java.lang.ThreadGroup.uncaughtException(..)
- Thread dumps, if you requested them via a system signal
Access logging is a related but different feature, which is implemented as a Valve . It uses self-contained logic to write its log files. The essential requirement for access logging is to handle a large continuous stream of data with low overhead, so it only uses Apache Commons Logging for its own debug messages. This implementation approach avoids additional overhead and potentially complex configuration. Please refer to the Valves documentation for more details on its configuration, including the various report formats.
- Globally. That is usually done in the $/conf/logging.properties file. The file is specified by the java.util.logging.config.file System property which is set by the startup scripts. If it is not readable or is not configured, the default is to use the $/lib/logging.properties file in the JRE.
- In the web application. The file will be WEB-INF/classes/logging.properties
org.apache.catalina.level=FINEST
- A prefix may be added to handler names, so that multiple handlers of a single class may be instantiated. A prefix is a String which starts with a digit, and ends with ‘.’. For example, 22foobar. is a valid prefix.
- System property replacement is performed for property values which contain $.
- As in Java 6, loggers can define a list of handlers using the loggerName.handlers property.
- By default, loggers will not delegate to their parent if they have associated handlers. This may be changed per logger using the loggerName.useParentHandlers property, which accepts a boolean value.
- The root logger can define its set of handlers using the .handlers property.
- By default the log files will be kept on the file system forever. This may be changed per handler using the handlerName.maxDays property. If the specified value for the property is
handlers = 1catalina.org.apache.juli.FileHandler, \ 2localhost.org.apache.juli.FileHandler, \ 3manager.org.apache.juli.FileHandler, \ java.util.logging.ConsoleHandler .handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler ############################################################ # Handler specific properties. # Describes specific configuration info for Handlers. ############################################################ 1catalina.org.apache.juli.FileHandler.level = FINE 1catalina.org.apache.juli.FileHandler.directory = $/logs 1catalina.org.apache.juli.FileHandler.prefix = catalina. 1catalina.org.apache.juli.FileHandler.maxDays = 90 1catalina.org.apache.juli.FileHandler.encoding = UTF-8 2localhost.org.apache.juli.FileHandler.level = FINE 2localhost.org.apache.juli.FileHandler.directory = $/logs 2localhost.org.apache.juli.FileHandler.prefix = localhost. 2localhost.org.apache.juli.FileHandler.maxDays = 90 2localhost.org.apache.juli.FileHandler.encoding = UTF-8 3manager.org.apache.juli.FileHandler.level = FINE 3manager.org.apache.juli.FileHandler.directory = $/logs 3manager.org.apache.juli.FileHandler.prefix = manager. 3manager.org.apache.juli.FileHandler.bufferSize = 16384 3manager.org.apache.juli.FileHandler.maxDays = 90 3manager.org.apache.juli.FileHandler.encoding = UTF-8 java.util.logging.ConsoleHandler.level = FINE java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter java.util.logging.ConsoleHandler.encoding = UTF-8 ############################################################ # Facility specific properties. # Provides extra control for each logger. ############################################################ org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = \ 2localhost.org.apache.juli.FileHandler org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = \ 3manager.org.apache.juli.FileHandler # For example, set the org.apache.catalina.util.LifecycleBase logger to log # each component that extends LifecycleBase changing state: #org.apache.catalina.util.LifecycleBase.level = FINE
Example logging.properties for the servlet-examples web application to be placed in WEB-INF/classes inside the web application:
handlers = org.apache.juli.FileHandler, java.util.logging.ConsoleHandler ############################################################ # Handler specific properties. # Describes specific configuration info for Handlers. ############################################################ org.apache.juli.FileHandler.level = FINE org.apache.juli.FileHandler.directory = $/logs org.apache.juli.FileHandler.prefix = servlet-examples. java.util.logging.ConsoleHandler.level = FINE java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
- Consider removing ConsoleHandler from configuration. By default (thanks to the .handlers setting) logging goes both to a FileHandler and to a ConsoleHandler . The output of the latter one is usually captured into a file, such as catalina.out . Thus you end up with two copies of the same messages.
- Consider removing FileHandler s for the applications that you do not use. E.g., the one for host-manager .
- The handlers by default use the system default encoding to write the log files. It can be configured with encoding property. See Javadoc for details.
- Consider configuring an Access log.
- Create a file called log4j.properties with the following content and save it into $CATALINA_BASE/lib
log4j.rootLogger = INFO, CATALINA # Define all the appenders log4j.appender.CATALINA = org.apache.log4j.DailyRollingFileAppender log4j.appender.CATALINA.File = $/logs/catalina log4j.appender.CATALINA.Append = true log4j.appender.CATALINA.Encoding = UTF-8 # Roll-over the log once per day log4j.appender.CATALINA.DatePattern = '.'yyyy-MM-dd'.log' log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n log4j.appender.LOCALHOST = org.apache.log4j.DailyRollingFileAppender log4j.appender.LOCALHOST.File = $/logs/localhost log4j.appender.LOCALHOST.Append = true log4j.appender.LOCALHOST.Encoding = UTF-8 log4j.appender.LOCALHOST.DatePattern = '.'yyyy-MM-dd'.log' log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayout log4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c- %m%n log4j.appender.MANAGER = org.apache.log4j.DailyRollingFileAppender log4j.appender.MANAGER.File = $/logs/manager log4j.appender.MANAGER.Append = true log4j.appender.MANAGER.Encoding = UTF-8 log4j.appender.MANAGER.DatePattern = '.'yyyy-MM-dd'.log' log4j.appender.MANAGER.layout = org.apache.log4j.PatternLayout log4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n log4j.appender.HOST-MANAGER = org.apache.log4j.DailyRollingFileAppender log4j.appender.HOST-MANAGER.File = $/logs/host-manager log4j.appender.HOST-MANAGER.Append = true log4j.appender.HOST-MANAGER.Encoding = UTF-8 log4j.appender.HOST-MANAGER.DatePattern = '.'yyyy-MM-dd'.log' log4j.appender.HOST-MANAGER.layout = org.apache.log4j.PatternLayout log4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Encoding = UTF-8 log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c- %m%n # Configure which loggers log to which appenders log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost] = INFO, LOCALHOST log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager] =\ INFO, MANAGER log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager] =\ INFO, HOST-MANAGER
- Download Log4J (Tomcat requires v1.2.x).
- Download or build tomcat-juli.jar and tomcat-juli-adapters.jar that are available as an «extras» component for Tomcat. See Additional Components documentation for details. This tomcat-juli.jar differs from the default one. It contains the full Apache Commons Logging implementation and thus is able to discover the presence of log4j and configure itself.
- If you want to configure Tomcat to use log4j globally:
- Put log4j.jar and tomcat-juli-adapters.jar from «extras» into $CATALINA_HOME/lib .
- Replace $CATALINA_HOME/bin/tomcat-juli.jar with tomcat-juli.jar from «extras».
- If you are running Tomcat with separate $CATALINA_HOME and $CATALINA_BASE and want to configure to use log4j in a single $CATALINA_BASE only:
- Create $CATALINA_BASE/bin and $CATALINA_BASE/lib directories if they do not exist.
- Put log4j.jar and tomcat-juli-adapters.jar from «extras» into $CATALINA_BASE/lib
- Put tomcat-juli.jar from «extras» as $CATALINA_BASE/bin/tomcat-juli.jar
- If you are running with a security manager, you would need to edit the $CATALINA_BASE/conf/catalina.policy file to adjust it to using a different copy of tomcat-juli.jar.
- Delete $CATALINA_BASE/conf/logging.properties to prevent java.util.logging generating zero length log files.
- Start Tomcat
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=DEBUG log4j.logger.org.apache.catalina.core=DEBUG log4j.logger.org.apache.catalina.session=DEBUG
- This exposes log4j libraries to the web applications through the Common classloader. See class loading documentation for details. Because of that, the web applications and libraries using Apache Commons Logging library are likely to automatically choose log4j as the underlying logging implementation.
- The java.util.logging API is still available for those web applications that use it directly. The $/conf/logging.properties file is still referenced by Tomcat startup scripts. For more information, see the subsections of the Introduction to this page. Removal of $/conf/logging.properties file, mentioned as one of the steps above, causes java.util.logging to fallback to the default configuration for the JRE, which is to use a ConsoleHandler and therefore not create any standard log files. You should confirm that all your log files are being created by log4j before disabling the standard mechanism.
- The Access Log Valve and ExtendedAccessLogValve use their own self-contained logging implementation, so they cannot be configured to use log4j. Refer to Valves for specific configuration details.