一尘不染

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

tomcat

我创建了一个在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中的给定Web应用程序的日志记录?


阅读 202

收藏
2020-06-16

共1个答案

一尘不染

唯一的问题是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

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

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

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

2020-06-16