Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to put traceId in MDC #600

Open
prefersimple opened this issue Oct 27, 2020 · 4 comments
Open

How to put traceId in MDC #600

prefersimple opened this issue Oct 27, 2020 · 4 comments

Comments

@prefersimple
Copy link

I want to print traceId in my log. I try to put traceId in MDC, then write %X{traceId} in logback.xml! How can I do it?

@lichenglin
Copy link

I think we can add new rules for "logback" ,"log4j","log4j2"

@Duncan-tree-zhou
Copy link
Contributor

if you want to get the traceid in a running thread, maybe you can use the following code to get the trace id.

        if(!GlobalTracer.isRegistered() || null == GlobalTracer.get().scopeManager().activeSpan()) {
            // tracing is not activated
            log.debug(String.format("tracing is not activated here."));
        } else {
            // tracing is activated
            String traceId = GlobalTracer.get().scopeManager().activeSpan().context().toTraceId();
            log.debug(String.format("trace id is %s", traceId));
        }

@prefersimple
Copy link
Author

if you want to get the traceid in a running thread, maybe you can use the following code to get the trace id.

        if(!GlobalTracer.isRegistered() || null == GlobalTracer.get().scopeManager().activeSpan()) {
            // tracing is not activated
            log.debug(String.format("tracing is not activated here."));
        } else {
            // tracing is activated
            String traceId = GlobalTracer.get().scopeManager().activeSpan().context().toTraceId();
            log.debug(String.format("trace id is %s", traceId));
        }

Thanks for your reply! I want to place this code in some rules without writing extra code.

@prefersimple
Copy link
Author

I think we can add new rules for "logback" ,"log4j","log4j2"

It is a good idea for my requirements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants