[
https://issues.apache.org/jira/browse/LOG4J2-1561?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15463122#comment-15463122
]
Zilong Song edited comment on LOG4J2-1561 at 9/4/16 4:22 PM:
-------------------------------------------------------------
Hi, thank you for your question. But I didn't quite understand your point. I
have 2 kinds of comprehension:
# The existing option already support my requirement, I can do it by just
configuring the pattern layout in {{log4j2.xml}}.
# I can extend the option in {{%ex}} (or {{%xEx}}) conversion to implement this
feature, instead of providing a new converter.
If the first one is what you mean, would you please show me how to configure
the pattern layout? As far as I learned, I still don't know how to reach this
goal. I will appreciate it very much if you show it to me.
If the second is what you mean, then I think it may be better to provide a new
converter. The existing {{ThrowablePatternConverter}} s are not very friendly
for extension because the format of the options are very fixed and hard to make
compatible modification. and if we are lucky to successfully make such a
modification, the future modifications will be more limited than this time.
Another reason why I not to do it in this way is that the
{{ThrowablePatternConverter}}s are complex enough, and I do not wish to
increase its complexity.
Another way I have considered to reach this goal is to provide a converter like
{{%replace}}, so that I can insert the MDC information before the new line
character. But I give it up because it will cost a lot of memory resources. The
{{%replace}} converter will buffer the contents in a {{StringBuilder}} and then
do replacement on it when append to the final log event text builder. As we
know the {{stacktrace}} s are always very large (hundreds or even thousands of
characters), a copy of it will cost a lot memory and time.
So I finally choose to use a new converter to do it.
was (Author: xnslong):
Hi, thank you for your question. But I didn't quite understand your point. I
have 2 kinds of comprehension:
# The existing option already support my requirement, I can do it by just
configuring the pattern layout in {{log4j2.xml}}.
# I can extend the option in {{%ex}} (or {{%xEx}}) conversion to implement this
feature, instead of providing a new converter.
If the first one is what you mean, would you please show me how to configure
the pattern layout? As far as I learned, I still don't know how to reach this
goal. I will appreciate it very much if you show it to me.
If the second is what you mean, then I think it may be better to provide a new
converter. The existing {{ThrowablePatternConverter}}s are not very friendly
for extension because the format of the options are very fixed and hard to make
compatible modification. and if we are lucky to successfully make such a
modification, the future modifications will be more limited than this time.
Another reason why I not to do it in this way is that the
{{ThrowablePatternConverter}}s are complex enough, and I do not wish to
increase its complexity.
Another way I have considered to reach this goal is to provide a converter like
{{%replace}}, so that I can insert the MDC information before the new line
character. But I give it up because it will cost a lot of memory resources. The
{{%replace}} converter will buffer the contents in a {{StringBuilder}} and then
do replacement on it when append to the final log event text builder. As we
know the {{stacktrace}}s are always very large (hundreds or even thousands of
characters), a copy of it will cost a lot memory and time.
So I finally choose to use a new converter to do it.
> 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
> Labels: features
> Fix For: 2.6.2
>
> Original Estimate: 12h
> Remaining Estimate: 12h
>
> 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.
> {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]