首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用MDC的perf4J

使用MDC的perf4J
EN

Stack Overflow用户
提问于 2011-03-24 03:36:14
回答 1查看 539关注 0票数 1

有人知道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 start1300905347914 time20310 tagHTTP.Success message/csa/login.seam -

18:36:09,142 INFO TimingLogger start1300905368240 time902 tagHTTP.Success message/csa/home.seam -

EN

回答 1

Stack Overflow用户

发布于 2012-03-27 02:43:19

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

代码语言:javascript
复制
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如下:

代码语言:javascript
复制
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

输出结果为:

代码语言:javascript
复制
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]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5410651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档