gpt4 book ai didi

java - 停止 tomcat 写入 stdout 和 stderr

转载 作者:行者123 更新时间:2023-11-28 22:01:43 26 4
gpt4 key购买 nike

我们使用 docker。我们使用 fluentd 作为 docker 容器的日志记录驱动程序。我们使用 tomcat 来为 java 应用程序提供服务。当 java 应用程序中抛出异常时,我们使用 log4j 来处理该异常。为了控制日志记录,我根据说明在 tomcat 中安装了 log4j https://tomcat.apache.org/tomcat-6.0-doc/logging.html在那个页面上。这是我正在使用的配置。

log4j.rootLogger = INFO, CATALINA

# Define all the appenders
log4j.appender.CATALINA = org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File = ${catalina.base}/logs/catalina
log4j.appender.CATALINA.Append = true
log4j.appender.CATALINA.Encoding = UTF-8
# Roll-over the log once per day
log4j.appender.CATALINA.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern = stewart %d [%t] %-5p %c- %m%n

log4j.appender.LOCALHOST = org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOCALHOST.File = ${catalina.base}/logs/localhost
log4j.appender.LOCALHOST.Append = true
log4j.appender.LOCALHOST.Encoding = UTF-8
log4j.appender.LOCALHOST.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayout
log4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

log4j.appender.MANAGER = org.apache.log4j.DailyRollingFileAppender
log4j.appender.MANAGER.File = ${catalina.base}/logs/manager
log4j.appender.MANAGER.Append = true
log4j.appender.MANAGER.Encoding = UTF-8
log4j.appender.MANAGER.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

log4j.appender.HOST-MANAGER = org.apache.log4j.DailyRollingFileAppender
log4j.appender.HOST-MANAGER.File = ${catalina.base}/logs/host-manager
log4j.appender.HOST-MANAGER.Append = true
log4j.appender.HOST-MANAGER.Encoding = UTF-8
log4j.appender.HOST-MANAGER.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.HOST-MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

# log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
# log4j.appender.CONSOLE.Encoding = UTF-8
# log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
# log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

# Configure which loggers log to which appenders
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost] = INFO, LOCALHOST
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager] = INFO, MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager] = INFO, HOST-MANAGER

问题是对于每个被记录的错误,堆栈跟踪仍然被写到 stderr。这会导致我们的日志记录平台将日志消息写入两次。停止将所有堆栈跟踪写入 stderr 的最佳方法是什么?

最佳答案

一种方法是抑制错误输出。

 System.setErr(null);

我正在使用 spring,我通过使用 PostConstruct 注释让这段代码在启动时运行。

import javax.annotation.PostConstruct;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SurpressLogging {

@PostConstruct
public void err() {
System.setErr(null);
}
}

关于java - 停止 tomcat 写入 stdout 和 stderr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38137579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com