Details
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.
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); }
<?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>
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
- Is contained by
-
LOG4J2-1838 Add support for appending common suffix to each line of throwable stack trace
- Closed
- links to