在Log4J中,为什么ConversionPattern中的%C打印“?" (问号)与AsyncAppender? [英] In Log4J, why %C in ConversionPattern prints '?' (question mark) with AsyncAppender?

查看:180
本文介绍了在Log4J中,为什么ConversionPattern中的%C打印“?" (问号)与AsyncAppender?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在带有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();
    }
}

测试代码的结果是:

2012/03/15 11:51:22,570? -你好,世界!

2012/03/15 11:51:22,570 ? - Hello, world!

没有AsynAppender,它可以正常工作:

Without AsynAppender, it works fine:

2012/03/15 11:51:06,002 Log4jTest-世界,您好!

2012/03/15 11:51:06,002 Log4jTest - Hello, world!

使用%c(类别),它也可以正常工作.

With %c (category), it works fine, too.

我想念什么?

请让我知道.

预先感谢:-)

参考:

http://logging.apache.org/log4j/1.2/apidocs /org/apache/log4j/PatternLayout.html

推荐答案

使用%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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆