Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-1561

print expected mdc info to each line of the exception stacktrace

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Duplicate
    • 2.6.2
    • 2.6.2
    • Core
    • OS X EI Caption, Java 1.7

    Description

      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 do further analysis. 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 a textual log file with massive lines of logs. 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, including all lines of the stack trace.

      Following is the code and configuration, and the output of it.

      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);
      }
      
      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>
      
      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
      

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              xnslong Zilong Song
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - 12h
                  12h
                  Remaining:
                  Remaining Estimate - 12h
                  12h
                  Logged:
                  Time Spent - Not Specified
                  Not Specified