【问题标题】:Prevent Tomcat logging from going to stderr?防止Tomcat日志记录进入stderr?
【发布时间】:2016-12-29 11:09:46
【问题描述】:

我目前在 Tomcat 中部署了多个 Web 应用程序,这些应用程序作为 Windows 服务运行。我创建/修改了大部分 logging.properties 文件,以便(希望)为每个 Web 应用程序创建一个单独的日志文件。

我的 conf\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 information 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.AsyncFileHandler, 2localhost.org.apache.juli.AsyncFileHandler, 3manager.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.

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

# 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

# To see debug messages for HTTP/2 handling, uncomment the following line:
#org.apache.coyote.http2.level = FINE

# To see debug messages for WebSocket handling, uncomment the following line:
#org.apache.tomcat.websocket.level = FINE

我的每个网络应用程序在 WEB-INF\classes 文件夹中都有以下 logging.properties 文件:

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

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

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

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

我目前在 INFO 级别记录我的 Web 应用程序中的所有内容,如下所示:

Logger.getLogger("LOG").log(Level.INFO, "Stuff to log");

事情似乎与我希望的比较接近。 Stdout 似乎正确地进入了单个 Web 应用程序日志,但一切似乎也进入了 tomcat8-stderr 日志。

我尝试从所有 config.properties 中注释掉 ConsoleHandler,但我仍然得到相同的行为。如何防止所有内容也记录在 stderr 日志中?

我将它添加到我的 Context.xml 中,但这似乎并没有改变任何东西:

<Context swallowOutput="true">

【问题讨论】:

    标签: java tomcat logging stderr java.util.logging


    【解决方案1】:

    我可以通过将 Web 应用程序记录器的 Level 设置为 FINE 并将 logging.properties 中的 Web 应用程序 ConsoleHandler 级别更改为 INFO 来解决此问题。

    【讨论】:

      【解决方案2】:

      正如Official Tomcat site 中所述,Apache Commons Logging 的私有包重命名实现,即 JULI 确实有例外。 .handlers 指令定义了 ROOT 记录器,而 ROOT 记录器又是直接来自常见 jar 的 ClassLoader(例如 StandardEngine 或 java.sql 驱动程序)的调用的唯一可能输出。除非通过类名分配特定的处理程序,否则 java.util.logging.Logger.getLogger() 之类的方法调用可能会得到控制。

      要使用每个引擎、每个主机和每个上下文日志的所谓“设施特定属性”tomcat-juli 功能,有效的 logging.properties 是在引擎启动时最初加载的全局 tomcat 日志配置:

      调用 javax.servlet.ServletContext.log(...) 以写入日志 消息由内部 Tomcat 日志记录处理。此类消息是 登录到名为

      的类别
      org.apache.catalina.core.ContainerBase.[${engine}].[${host}].[${context}]
      

      此日志记录是根据Tomcat日志记录执行的 配置。 您不能在网络应用程序中覆盖它

      然而,

      JULI [...] 除了常规的全局 java.util.logging 配置外,还支持每个类加载器配置。这意味着可以在以下层配置日志记录:

      1. 全球。这通常在${catalina.base}/conf/logging.properties 文件中完成。该文件由启动脚本设置的 java.util.logging.config.file 系统属性指定。如果不可读或未配置,则默认使用 JRE 中的 ${java.home}/lib/logging.properties 文件。
      2. 在 Web 应用程序中。该文件将是WEB-INF/classes/logging.properties

      所以在进行了一些不同的测试之后,我意识到 我无法重现您的问题,因为我成功地从应用程序完全地登录到前缀。 yyyy-mm-dd.log 文件在每个应用程序WEB-INF/classes/logging.properties 中定义。

      我说“完全”是考虑到如果 Web 应用程序使用 System.out.print(),打印的内容只会显示在“前缀”中。如果 swallowOutput 在 context.xml 中设置为 true(无论是来自应用程序的 META-INF/context.xml 还是来自 ${CATALINA_BASE}/conf/context.xml 文件。

      调整日志记录级别以避免在我看来不是一个优雅的绕过的情况,我希望你还没有解决这个问题,并且会分享你的最终解决方案,除非问题从此消失!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-08
        • 2018-01-07
        • 2014-06-08
        • 2011-03-10
        相关资源
        最近更新 更多