Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to log in tomcat using slf4j and java.util.logging

I created a webapp that runs on Tomcat 8. As always I want to use slf4j and in this case, backed by java.util.logging (because it is the default for Tomcat). The relevant dependencies are this:

<!-- logging -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.10</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-jdk14</artifactId>
    <version>1.7.10</version>
    <scope>runtime</scope>
</dependency>

I added some log statements in the code (info and debug) but I can't find them. Neither in the console output (if I start Tomcat from eclipse) nor in any of the log files created in /logs folder (My app is running fine).

So what is missing here? Do I have to add something to conf/logging.properties (I don't want to package any log configuration into my app)? Is there an example how to configure logging of a given webapp deployed in tomcat?

like image 945
dermoritz Avatar asked Mar 11 '15 15:03

dermoritz


People also ask

What is the name of Java logging method used in Tomcat?

As of Tomcat 5.5, Apache's Java Commons Logging (JCL) technology is used throughout Tomcat. JCL is a lightweight API for Java applications that allows hierarchical logging to be supported across all log levels, independent of logging implementation.

Does Apache Tomcat use log4j by default?

Apache Tomcat. Log4j may be used as the logging framework for Apache Tomcat. This support is implemented automatically by including the log4j-api, log4j-core, and log4j-appserver jars in the boot classpath.


1 Answers

The only problem was the configuration on Tomcat side. Since Tomcat doesn't use plain java.util.logging the config is little different. So adding some lines to conf/logging.properties helped:

...

handlers = 1catalina.org.apache.juli.AsyncFileHandler, 2localhost.org.apache.juli.AsyncFileHandler, 3manager.org.apache.juli.AsyncFileHandler, 4host-manager.org.apache.juli.AsyncFileHandler, 5reportExport.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler 

.handlers = 1catalina.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler

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

1catalina.org.apache.juli.AsyncFileHandler.level = FINE
1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina.

2localhost.org.apache.juli.AsyncFileHandler.level = FINE
2localhost.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost.

3manager.org.apache.juli.AsyncFileHandler.level = FINE
3manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
3manager.org.apache.juli.AsyncFileHandler.prefix = manager.

4host-manager.org.apache.juli.AsyncFileHandler.level = FINE
4host-manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
4host-manager.org.apache.juli.AsyncFileHandler.prefix = host-manager.

5reportExport.org.apache.juli.AsyncFileHandler.level = FINE
5reportExport.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
5reportExport.org.apache.juli.AsyncFileHandler.prefix = reportExport.

java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter


############################################################
# 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.AsyncFileHandler

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = 3manager.org.apache.juli.AsyncFileHandler

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].handlers = 4host-manager.org.apache.juli.AsyncFileHandler

com.prodyna.reportExport.level = FINE
com.prodyna.reportExport.handlers = 5reportExport.org.apache.juli.AsyncFileHandler

# 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

# To see debug messages in TldLocationsCache, uncomment the following line:
#org.apache.jasper.compiler.TldLocationsCache.level = FINE

So i added an aditional handler "5reportExport.org.apache.juli.AsyncFileHandler" to get my stuff into seperate file. Then i configured this handler like the others. The crucial part is at the end:

com.prodyna.reportExport.level = FINE
com.prodyna.reportExport.handlers = 5reportExport.org.apache.juli.AsyncFileHandler

This uses the configured handler for all classes in package com.prodyna.reportExport.

like image 177
dermoritz Avatar answered Oct 07 '22 01:10

dermoritz