如何使用slf4j和java.util.logging登录tomcat

时间:2015-03-11 15:21:10

标签: tomcat slf4j java.util.logging

我创建了一个在Tomcat 8上运行的webapp。一如既往,我想使用slf4j,在这种情况下,由java.util.logging支持(因为它是Tomcat的默认设置)。相关的依赖是:

<!-- 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>

我在代码中添加了一些日志语句(信息和调试)但我找不到它们。既没有在控制台输出中(如果我从eclipse启动Tomcat)也没有在/ logs文件夹中创建的任何日志文件中(我的应用运行正常)。

那么这里缺少什么? 我是否必须向conf / logging.properties添加内容(我不想将任何日志配置打包到我的应用程序中)? 是否有一个示例如何配置在tomcat中部署的给定webapp的日志记录?

1 个答案:

答案 0 :(得分:3)

唯一的问题是Tomcat端的配置。由于Tomcat不使用普通的java.util.logging,因此配置略有不同。因此,在conf / logging.properties中添加一些行有助于:

...

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

所以我添加了一个aditional处理程序“5reportExport.org.apache.juli.AsyncFileHandler”来将我的东西放入单独的文件中。然后我像其他人一样配置了这个处理程序关键部分在最后:

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

这将为com.prodyna.reportExport包中的所有类使用已配置的处理程序。