如何在 Tomcat 中将级别日志记录设置为 DEBUG?

Posted

技术标签:

【中文标题】如何在 Tomcat 中将级别日志记录设置为 DEBUG?【英文标题】:How to set level logging to DEBUG in Tomcat? 【发布时间】:2011-05-06 08:59:54 【问题描述】:

我想在 tomcat 中将级别日志记录设置为 DEBUG,但在控制台中仍然只有 INFO 和 WARN 输出。 谁能告诉我怎么了?

我的 C:\tomcat\logging.properties:

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional DEBUGrmation regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

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

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

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

1catalina.org.apache.juli.FileHandler.level = DEBUG
1catalina.org.apache.juli.FileHandler.directory = $catalina.base/logs
1catalina.org.apache.juli.FileHandler.prefix = catalina.

2localhost.org.apache.juli.FileHandler.level = DEBUG
2localhost.org.apache.juli.FileHandler.directory = $catalina.base/logs
2localhost.org.apache.juli.FileHandler.prefix = localhost.

3manager.org.apache.juli.FileHandler.level = DEBUG
3manager.org.apache.juli.FileHandler.directory = $catalina.base/logs
3manager.org.apache.juli.FileHandler.prefix = manager.

4host-manager.org.apache.juli.FileHandler.level = DEBUG
4host-manager.org.apache.juli.FileHandler.directory = $catalina.base/logs
4host-manager.org.apache.juli.FileHandler.prefix = host-manager.

java.util.logging.ConsoleHandler.level = DEBUG
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter


############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = DEBUG
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.FileHandler

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

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

# For example, set the com.xyz.foo logger to only log SEVERE
# messages:
#org.apache.catalina.startup.ContextConfig.level = DEBUG
#org.apache.catalina.startup.HostConfig.level = DEBUG
#org.apache.catalina.session.ManagerBase.level = DEBUG
#org.apache.catalina.core.AprLifecycleListener.level=DEBUG

我的日志示例:

INFO: Deploying configuration descriptor manager.xml
08.11.2010 1:06:42 org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive spring-mvc-trial.war
08.11.2010 1:06:46 org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory docs
08.11.2010 1:06:46 org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory examples
08.11.2010 1:06:46 org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory ROOT
08.11.2010 1:06:46 org.apache.coyote.http11.Http11AprProtocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
08.11.2010 1:06:46 org.apache.coyote.ajp.AjpAprProtocol start
INFO: Starting Coyote AJP/1.3 on ajp-8009
08.11.2010 1:06:46 org.apache.catalina.startup.Catalina start
INFO: Server startup in 3777 ms
08.11.2010 1:09:36 org.apache.coyote.http11.Http11AprProtocol pause
INFO: Pausing Coyote HTTP/1.1 on http-8080
08.11.2010 1:09:36 org.apache.coyote.ajp.AjpAprProtocol pause
INFO: Pausing Coyote AJP/1.3 on ajp-8009
08.11.2010 1:09:37 org.apache.catalina.core.StandardService stop
INFO: Stopping service Catalina
08.11.2010 1:09:37 org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
SEVERE: The web application [/spring-mvc-trial] registered the JBDC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
08.11.2010 1:09:37 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/spring-mvc-trial] appears to have started a thread named [MySQL Statement Cancellation Timer] but has failed to stop it. This is very likely to create a memory leak.
08.11.2010 1:09:38 org.apache.coyote.http11.Http11AprProtocol destroy
INFO: Stopping Coyote HTTP/1.1 on http-8080
08.11.2010 1:09:38 org.apache.coyote.ajp.AjpAprProtocol destroy
INFO: Stopping Coyote AJP/1.3 on ajp-8009

【问题讨论】:

对于 Tomcat 8,请参阅:Tomcat 8 enable debug logging to list unneeded jars 【参考方案1】:

首先,要使用的关卡名称是FINE,而不是DEBUG。让我们假设 DEBUG 实际上是有效的,因为它使下面的解释更有意义......

Handler specific properties 部分,您将这些处理程序的日志记录级别设置为DEBUG。这意味着处理程序将处理具有DEBUG 或更高级别的任何日志消息。这并不一定意味着任何DEBUG 消息实际上都会传递给处理程序。

Facility specific properties 部分,您将一些明确命名的记录器的记录级别设置为DEBUG。对于这些记录器,DEBUG 或更高级别的任何内容传递给处理程序。

默认的日志记录级别是INFO,除了Facility specific properties部分提到的记录器,所有的记录器都会有这个级别。

如果您想查看所有FINE 消息,请添加:

.level = FINE

但是,这会产生大量的日志消息。为您的代码设置日志记录级别可能更有用:

your.package.level = FINE

有关详细信息,请参阅Tomcat 6/Tomcat 7 日志记录文档。此处显示的示例 logging.properties 文件使用 FINE 而不是 DEBUG

...
1catalina.org.apache.juli.FileHandler.level = FINE
...

还提供了设置其他日志记录级别的示例:

# For example, set the com.xyz.foo logger to only log SEVERE
# messages:
#org.apache.catalina.startup.ContextConfig.level = FINE
#org.apache.catalina.startup.HostConfig.level = FINE
#org.apache.catalina.session.ManagerBase.level = FINE

【讨论】:

【参考方案2】:

Tomcat 的 JULI 日志记录级别

严重 - 严重失败

警告 - 潜在问题

INFO - 信息性消息

CONFIG - 静态配置消息

FINE - 跟踪消息

FINER - 详细的跟踪消息

FINEST - 非常详细的跟踪消息

你可以在这里找到更多 https://documentation.progress.com/output/ua/OpenEdge_latest/index.html#page/pasoe-admin/tomcat-logging.html

【讨论】:

以上是关于如何在 Tomcat 中将级别日志记录设置为 DEBUG?的主要内容,如果未能解决你的问题,请参考以下文章

tomcat源码阅读之日志记录器(Logger)

如何设置 HikariCP 的日志记录级别?

如何处理Tomcat日志catalina.out日志文件过大的问题

Django:如何将日志级别设置为 INFO 或 DEBUG

配置 log4cpp 日志级别优先级

Tomcat 自动清理日志和缓存