Giter Club home page Giter Club logo

Comments (10)

jidian avatar jidian commented on May 18, 2024 2

很赞

from transmittable-thread-local.

zhangshity avatar zhangshity commented on May 18, 2024 1

from transmittable-thread-local.

oldratlee avatar oldratlee commented on May 18, 2024

LOG4J(2)的集成 想法不错,我理解和思考一下你说的实现过程。

另外,在MDC的需求说明中,关于日志场景的说明还是比较模糊的,你能给一下更明确实际些的场景说明不?这样做起来更有目标感 😸 @bwzhang2011

from transmittable-thread-local.

bwzhang2011 avatar bwzhang2011 commented on May 18, 2024

@oldratlee 感谢回复。

使用MDC是用来记录跟应用上下文环境中需要保存的一些数据,比如请求类提取的、跟执行有关的跟踪信息还有当异常发生的时候提取的,这些可以在appender中的layout来指定这些信息(比如%X{key1}: %X{key2}等)。

一般场景可以是:

  1. filter类中写入parent thread的内容(MDC.put)——带有一traceId或其余标记信息(用户)
  2. 业务类中写入业务类的信息(MDC.put)——可提取跟traceId或标记有关的信息(提取),并保存其余的消息(跟本线程有关的)并进行保存保存
  3. 清空filter的线程变量,finally { ThreadContext.clearAll() }

from transmittable-thread-local.

oldratlee avatar oldratlee commented on May 18, 2024

MtContextThreadLocal新加上callback方法:

    protected void beforeExecute() {
    }

    protected void afterExecute() {
    }

MtContextThreadLocal.java

可以覆盖这个方法 来完成 log4j2的设置,达到filter的效果:

    static MtContextThreadLocal<Map<String, String>> mtc = new MtContextThreadLocal<Map<String, String>>() {
        @Override
        protected void beforeExecute() {
            final Map<String, String> log4j2Context = get();
            for (Map.Entry<String, String> entry : log4j2Context.entrySet()) {
                ThreadContext.put(entry.getKey(), entry.getValue());
            }
        }

        @Override
        protected void afterExecute() {
            ThreadContext.clearAll();
        }

        @Override
        protected Map<String, String> initialValue() {
            return new HashMap<String, String>();
        }
    };

Log的代码:

    public static void main(String[] args) throws Exception {
        // Init Log Context, set MTC
        // More KV if needed
        final String TRACE_ID = "trace-id";
        final String TRACE_ID_VALUE = "XXX-YYY-ZZZ";
        ThreadContext.put(TRACE_ID, TRACE_ID_VALUE);
        mtc.get().put(TRACE_ID, TRACE_ID_VALUE);

        // Log in Main Thread
        logger.info("Log in main!");

        // Run task in thread pool
        final ExecutorService executorService = Executors.newFixedThreadPool(1);
        final Runnable task = new Runnable() {
            @Override
            public void run() {
                // Log in thread pool
                logger.info("Log in Runnable!");
            }
        };
        final Future<?> submit = executorService.submit(MtContextRunnable.get(task));
        submit.get();

        executorService.shutdown();
    }

完整示例:Log4j2ContextFilter.javalog4j2.xml

输出:

22:50:52.823 [main] INFO  log4j2.Log4j2ContextFilter XXX-YYY-ZZZ - Log in main!
22:50:52.826 [pool-2-thread-1] INFO  log4j2.Log4j2ContextFilter XXX-YYY-ZZZ - Log in Runnable!

@bwzhang2011 看看这样是不是可以解决你的问题了? 如果OK,我就发一个新版本,加上这个功能。

from transmittable-thread-local.

oldratlee avatar oldratlee commented on May 18, 2024

v1.2.0 已发布。 @bwzhang2011

from transmittable-thread-local.

bwzhang2011 avatar bwzhang2011 commented on May 18, 2024

@oldratlee, 我再仔细看一下,看是否能合到工程中。T.K.S

from transmittable-thread-local.

bwzhang2011 avatar bwzhang2011 commented on May 18, 2024

@oldratlee SL4JMDC类依赖于MDCAdapter,个人感觉如果集成则需要提供一个针对其的实现。这样我仍然只需要操作MDC即可。

from transmittable-thread-local.

oldratlee avatar oldratlee commented on May 18, 2024

相关的 SL4JMDCmultiple-thread-context的集成,涉及如果设置业务的Log Contexttrace-id,这个需要业务来完成, multi-thread-context提供能力。

个人感觉如果集成则需要提供一个针对其的实现

你说能一下 『针对其的实现』如何做,给一下示例代码?

如果你比较清楚了,能直接实现一下,给个PullRequest吗? 😸 @bwzhang2011

from transmittable-thread-local.

oldratlee avatar oldratlee commented on May 18, 2024

PS: 已提供集成库:

<dependency>
    <groupId>com.ofpay</groupId>
    <artifactId>logback-mdc-ttl</artifactId>
    <version>1.4.0</version>
</dependency>

说明参见:
https://github.com/alibaba/transmittable-thread-local/blob/master/docs/requirement-scenario.md#log4j2-mdc%E7%9A%84ttl%E9%9B%86%E6%88%90

from transmittable-thread-local.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.