Zilong Song created LOG4J2-1561:
-----------------------------------

             Summary: print expected mdc info to each line of the exception 
stacktrace
                 Key: LOG4J2-1561
                 URL: https://issues.apache.org/jira/browse/LOG4J2-1561
             Project: Log4j 2
          Issue Type: Improvement
          Components: Core
    Affects Versions: 2.6.2
         Environment: OS X EI Caption, Java 1.7
            Reporter: Zilong Song
            Priority: Minor
             Fix For: 2.6.2


In some applications we need to append certain MDC information to each line of 
logs for a certain process, so that we can easily distinguish them from other 
logs, and further analysis them. But currently this information cannot be 
appended to any line of the stack trace, so it's hard to get the lines of the 
stack trace from massive lines. This issue will append this information to each 
line of the stack trace. 

An already known usage for this feature is that we can easily get all lines of 
log for a certain process when we set an ID before the process starts.

The following is the code and configuration, and the effect of it.

code:
{code:java|title=Test.main()}
public static void main(String[] args) {
    final Logger logger = LogManager.getRootLogger();
    ThreadContext.put("proc_id", "1234567890");
    Exception exception = new Exception("sample exception");
    logger.info("an exception is logged", exception);
}
{code}

{code:xml|title=log4j2.xml|}
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="live-im">
    <Appenders>
        <Console name="console-log" target="SYSTEM_OUT">
            <PatternLayout pattern="%d %p [%t] %C{2} (%F:%L) - %m 
%X{proc_id}%n%cEx{proc_id}"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="all">
            <AppenderRef ref="console-log"/>
        </Root>
    </Loggers>
</Configuration>
{code}

{code:title=output}
2016-09-03 22:56:56,721 INFO [main] pattern.Test (Test.java:17) - an exception 
is logged 1234567890
java.lang.Exception: sample exception 1234567890
        at org.apache.logging.log4j.core.pattern.Test.main(Test.java:16) 
[classes/:?] 1234567890
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
~[?:1.8.0_91] 1234567890
        at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
~[?:1.8.0_91] 1234567890
        at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 ~[?:1.8.0_91] 1234567890
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_91] 
1234567890
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 
[idea_rt.jar:?] 1234567890
{code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to