问题描述
在带有AsyncAppender的ConversionPattern中使用%C时遇到麻烦.
I have a trouble when using %C in ConversionPattern with AsyncAppender.
我的Lo4J配置是:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy/MM/dd HH:mm:ss,SSS} %C{1} - %m%n" />
</layout>
</appender>
<appender name="async_console" class="org.apache.log4j.AsyncAppender">
<param name="BufferSize" value="1000" />
<appender-ref ref="console" />
</appender>
<root>
<level value="debug" />
<!--
<appender-ref ref="console" />
-->
<appender-ref ref="async_console" />
</root>
</log4j:configuration>
我的测试代码是:
@Test
public void testAsync() {
DOMConfigurator
.configure("src/test/resources/learningtest/log4j/log4j_test_async.xml");
Logger log = Logger.getLogger(getClass());
log.debug("Hello, world!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
测试代码的结果是:
没有AsynAppender,它可以正常工作:
Without AsynAppender, it works fine:
使用%c(类别),它也可以正常工作.
With %c (category), it works fine, too.
我想念什么?
请让我知道.
预先感谢:-)
参考:
推荐答案
使用%C"或%M"时,log4J使用Throwable.getStackTrace获取stackTrace并使用此信息获取调用方类和方法.问题在于,当使用AsyncAppender时,Throwable在另一个线程中创建,并且stackTrace不包含调用者方法.
When using "%C" or "%M", log4J uses Throwable.getStackTrace to get the stackTrace and use this information to get the caller class and method. The problem is that when using an AsyncAppender, the Throwable is created in another thread and the stackTrace does not contain the caller method.
这篇关于在Log4J中,为什么ConversionPattern中的%C打印“?" (问号)与AsyncAppender?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!