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

1

8

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?

Spectroscope answered 11/3, 2015 at 15:21 Comment(0)
S
3

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.

Spectroscope answered 13/3, 2015 at 15:59 Comment(1)
I'm trying to achieve the same but the logs are not showing up in the configured file, they just show up in catalina.out. When I directly use java util logging, logs are visible in the file. Any suggestions on fixing this ? TIAEquiangular

© 2022 - 2024 — McMap. All rights reserved.