perf4J 与 MDC

发布于 2024-10-25 18:36:14 字数 598 浏览 6 评论 0原文

有谁知道 perf4J 是否支持 log4j MDC。我的所有日​​志语句都附加了 MDC 值,但是 perf4J 日志语句不显示 MDC 值。

请参阅下文,我希望 MDCMappedValue 也显示在 [TimingLogger] 日志语句的末尾。

18:35:48,038 INFO [LoginAction] 将用户 kermit 登录到应用程序 - MDCMappedValue 18:35:48,749 INFO [PostAuthenticationHandler] doPostAuthenticate() 已启动 - MDCMappedValue 18:36:03,653 INFO [PostAuthenticationHandler] 为 kermit 加载配置文件 - MDCMappedValue 18:36 :08,224 INFO [TimingLogger] start[1300905347914] time[20310] tag[HTTP.Success] message[/csa/login.seam] -
18:36:09,142 INFO [TimingLogger] 开始[1300905368240] 时间[902] 标签[HTTP.Success] 消息[/csa/home.seam] -

Does anyone know if perf4J has support for log4j MDC. All my log statements are appended with MDC values, however the perf4J log statements don't show the MDC value.

Please see below, I expect MDCMappedValue to be shown at the end of [TimingLogger] log statements as well.

18:35:48,038 INFO [LoginAction] Logging in user kermit into application - MDCMappedValue 18:35:48,749 INFO [PostAuthenticationHandler] doPostAuthenticate() started - MDCMappedValue 18:36:03,653 INFO [PostAuthenticationHandler] Profile Loaded for kermit - MDCMappedValue 18:36:08,224 INFO [TimingLogger] start[1300905347914] time[20310] tag[HTTP.Success] message[/csa/login.seam] -
18:36:09,142 INFO [TimingLogger] start[1300905368240] time[902] tag[HTTP.Success] message[/csa/home.seam] -

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2024-11-01 18:36:14

我的测试似乎产生了预期的结果。请注意,我使用 Log4JStopWatch,而不是 LoggingStopWatch

package test;

import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import org.perf4j.StopWatch;
import org.perf4j.log4j.Log4JStopWatch;

public class Perf4jMdcTest {
    private Logger _ = Logger.getLogger(Perf4jMdcTest.class);

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            new Thread() {
                @Override
                public void run() {
                    MDC.put("id", getName());
                    Perf4jMdcTest perf4jMdcTest = new Perf4jMdcTest();
                    perf4jMdcTest.test1();
                    perf4jMdcTest.test2();
                    MDC.clear();
                }
            }.start();
        }
    }

    private void test1() {
        _.info("test1");
        StopWatch stopWatch = new Log4JStopWatch();
        stopWatch.start("a");
        try { Thread.sleep(300); } 
        catch (InterruptedException e) { }
        stopWatch.stop();
    }

    private void test2() {
        _.info("test2");
        StopWatch stopWatch = new Log4JStopWatch();
        stopWatch.start("b");
        try { Thread.sleep(600); } 
        catch (InterruptedException e) { }
        stopWatch.stop();

    }
}

我的 log4j.properties 如下:

log4j.rootLogger=debug, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%d [%-5p] MDC:%X{id} - %m%n

输出为:

2012-03-26 20:37:43,049 [INFO ] MDC:Thread-1 - test1
2012-03-26 20:37:43,050 [INFO ] MDC:Thread-3 - test1
2012-03-26 20:37:43,049 [INFO ] MDC:Thread-2 - test1
2012-03-26 20:37:43,353 [INFO ] MDC:Thread-2 - start[1332787063053] time[300] tag[a]
2012-03-26 20:37:43,353 [INFO ] MDC:Thread-2 - test2
2012-03-26 20:37:43,353 [INFO ] MDC:Thread-1 - start[1332787063053] time[300] tag[a]
2012-03-26 20:37:43,354 [INFO ] MDC:Thread-1 - test2
2012-03-26 20:37:43,353 [INFO ] MDC:Thread-3 - start[1332787063053] time[300] tag[a]
2012-03-26 20:37:43,354 [INFO ] MDC:Thread-3 - test2
2012-03-26 20:37:43,955 [INFO ] MDC:Thread-2 - start[1332787063354] time[600] tag[b]
2012-03-26 20:37:43,955 [INFO ] MDC:Thread-1 - start[1332787063354] time[601] tag[b]
2012-03-26 20:37:43,955 [INFO ] MDC:Thread-3 - start[1332787063354] time[601] tag[b]

My test seems to produce the expected results. Notice that I use the Log4JStopWatch, not the LoggingStopWatch:

package test;

import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import org.perf4j.StopWatch;
import org.perf4j.log4j.Log4JStopWatch;

public class Perf4jMdcTest {
    private Logger _ = Logger.getLogger(Perf4jMdcTest.class);

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            new Thread() {
                @Override
                public void run() {
                    MDC.put("id", getName());
                    Perf4jMdcTest perf4jMdcTest = new Perf4jMdcTest();
                    perf4jMdcTest.test1();
                    perf4jMdcTest.test2();
                    MDC.clear();
                }
            }.start();
        }
    }

    private void test1() {
        _.info("test1");
        StopWatch stopWatch = new Log4JStopWatch();
        stopWatch.start("a");
        try { Thread.sleep(300); } 
        catch (InterruptedException e) { }
        stopWatch.stop();
    }

    private void test2() {
        _.info("test2");
        StopWatch stopWatch = new Log4JStopWatch();
        stopWatch.start("b");
        try { Thread.sleep(600); } 
        catch (InterruptedException e) { }
        stopWatch.stop();

    }
}

My log4j.properties is as follows:

log4j.rootLogger=debug, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%d [%-5p] MDC:%X{id} - %m%n

And the output is:

2012-03-26 20:37:43,049 [INFO ] MDC:Thread-1 - test1
2012-03-26 20:37:43,050 [INFO ] MDC:Thread-3 - test1
2012-03-26 20:37:43,049 [INFO ] MDC:Thread-2 - test1
2012-03-26 20:37:43,353 [INFO ] MDC:Thread-2 - start[1332787063053] time[300] tag[a]
2012-03-26 20:37:43,353 [INFO ] MDC:Thread-2 - test2
2012-03-26 20:37:43,353 [INFO ] MDC:Thread-1 - start[1332787063053] time[300] tag[a]
2012-03-26 20:37:43,354 [INFO ] MDC:Thread-1 - test2
2012-03-26 20:37:43,353 [INFO ] MDC:Thread-3 - start[1332787063053] time[300] tag[a]
2012-03-26 20:37:43,354 [INFO ] MDC:Thread-3 - test2
2012-03-26 20:37:43,955 [INFO ] MDC:Thread-2 - start[1332787063354] time[600] tag[b]
2012-03-26 20:37:43,955 [INFO ] MDC:Thread-1 - start[1332787063354] time[601] tag[b]
2012-03-26 20:37:43,955 [INFO ] MDC:Thread-3 - start[1332787063354] time[601] tag[b]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文