[英]Log4j2: Logger is configured but not usable
I'm working on a JEE REST API and am converting to log4j2.我正在研究 JEE REST API 并且正在转换为 log4j2。 Some of my logging is working, but not others.我的一些日志记录在工作,但其他的没有。
I created an appender, monitorAppender.我创建了一个 appender,monitorAppender。 Here's its configuration:这是它的配置:
<RollingFile name="monitorAppender" append="true" fileName="${baseDir}/ZDA_monitor.log" filePattern="${baseDir}/ZDA_monitor.log.%i">
<PatternLayout pattern="%d{yyMMddHHmmssSSS}|%m%x%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
And logger configuration:和记录器配置:
<Logger name="ZDAMonitor" level="info" additivity="false">
<AppenderRef ref="monitorAppender"/>
</Logger>
In my code when I try to use the logger:在我尝试使用记录器时的代码中:
LogManager.getLogger("ZDAMonitor").fatal("Test123");
I see this in the console, instead of the file:我在控制台而不是文件中看到了这个:
SystemOut O 14:56:50.922 [WebContainer: 1] FATAL ZDAMonitor - Test123
I can't tell what's wrong with the config.我不知道配置有什么问题。 I turned on debug logging for log4j and see in the startup logs it's being correctly loaded:我打开了 log4j 的调试日志记录,并在启动日志中看到它被正确加载:
SystemOut O 2023-01-25 14:56:43,490 WebContainer: 1 DEBUG Registering MBean org.apache.logging.log4j2:type=5745d2fa,component=Loggers,name=ZDAMonitor
I can't post the source as this is a company project, but I'm using gradle, log4j-core and api 2.19.0, and WebSphere 8.5.5.22 with Java 1.8.我无法发布源代码,因为这是一个公司项目,但我使用的是 gradle、log4j-core 和 api 2.19.0,以及 WebSphere 8.5.5.22 和 Java 1.8。
I've checked and double checked the configuration and code, and cannot find anything wrong with it.我检查并仔细检查了配置和代码,没有发现任何问题。
WebSphere has a very peculiar thread context classloader. WebSphere 有一个非常特殊的线程上下文类加载器。
The log4j-web
module configures a logger context associated to the context classloader. log4j-web
模块配置与上下文类加载器关联的记录器上下文。 However the context classloader in WebSphere is not the classloader that loaded the classes in your application, so your application's loggers use a different logger context (and a different config).然而,WebSphere 中的上下文类加载器并不是在您的应用程序中加载类的类加载器,因此您的应用程序的记录器使用不同的记录器上下文(和不同的配置)。 See LOG4J2-3577 for details.有关详细信息,请参阅LOG4J2-3577 。
A simple solution would be to use a BasicContextSelector
.一个简单的解决方案是使用BasicContextSelector
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.