[ https://issues.apache.org/jira/browse/LOG4J2-180?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13749366#comment-13749366 ]
Seyguai edited comment on LOG4J2-180 at 8/24/13 11:48 AM: ---------------------------------------------------------- Yes, this behaviour is related to a misconfiguration, but the code I quoted is supposed to fix this issue in case the user did it on purpose. Please consider those 4 cases: {code:java} #1: <PatternLayout pattern="%m %ex{1}%n" /> #2: <PatternLayout pattern="%m%ex{1}%n" /> #3: <PatternLayout pattern="%m %highlight{%ex{1}}%n" /> #4: <PatternLayout pattern="%m%highlight{%ex{1}}%n" /> {code} With the cases #1 and #2, the previous code will correctly look into 'toAppendTo' to add a whitespace if needed, since toAppendTo already contains the message. With the others cases, the toAppendTo StringBuilder is empty when it reachs the ThrowablePatternConverter class, thus the code will never have the expected behaviour. {code:java} org.apache.logging.log4j.core.pattern.HighlightConverter.java: public void format(final LogEvent event, final StringBuilder toAppendTo) { final StringBuilder buf = new StringBuilder(); for (final PatternFormatter formatter : formatters) { formatter.format(event, buf); // Here buf is empty, thus the ThrowablePattern formatter won't be able to fix the spacing problem as it should. } if (buf.length() > 0) { toAppendTo.append(levelStyles.get(event.getLevel())).append(buf.toString()). append(AnsiEscape.getDefaultStyle()); } } {code} A fix would probably looks like that: {code:java} if (buf.length() > 0) { if (!Character.isWhitespace(toAppendTo.charAt(len - 1))) { toAppendTo.append(" "); } toAppendTo.append(levelStyles.get(event.getLevel())).append(buf.toString()). append(AnsiEscape.getDefaultStyle()); } {code} (for all converters) was (Author: seyguai): Yes, this behaviour is related to a misconfiguration, but the code I quoted is here to fix this issue in case the user did it on purpose. Please consider those 4 cases: {code:java} #1: <PatternLayout pattern="%m %ex{1}%n" /> #2: <PatternLayout pattern="%m%ex{1}%n" /> #3: <PatternLayout pattern="%m %highlight{%ex{1}}%n" /> #4: <PatternLayout pattern="%m%highlight{%ex{1}}%n" /> {code} With the cases #1 and #2, the previous code will correctly look into 'toAppendTo' to add a whitespace if needed, since toAppendTo already contains the message. With the others cases, the toAppendTo StringBuilder is empty when it reachs the ThrowablePatternConverter class, thus the code will never have the expected behaviour. {code:java} org.apache.logging.log4j.core.pattern.HighlightConverter.java: public void format(final LogEvent event, final StringBuilder toAppendTo) { final StringBuilder buf = new StringBuilder(); for (final PatternFormatter formatter : formatters) { formatter.format(event, buf); // Here buf is empty, thus the ThrowablePattern formatter won't be able to fix the spacing problem as it should. } if (buf.length() > 0) { toAppendTo.append(levelStyles.get(event.getLevel())).append(buf.toString()). append(AnsiEscape.getDefaultStyle()); } } {code} A fix would probably looks like that: {code:java} if (buf.length() > 0) { if (!Character.isWhitespace(toAppendTo.charAt(len - 1))) { toAppendTo.append(" "); } toAppendTo.append(levelStyles.get(event.getLevel())).append(buf.toString()). append(AnsiEscape.getDefaultStyle()); } {code} (for all converters) > style & highlight patterns altering content behaviour > ----------------------------------------------------- > > Key: LOG4J2-180 > URL: https://issues.apache.org/jira/browse/LOG4J2-180 > Project: Log4j 2 > Issue Type: Bug > Components: Layouts > Affects Versions: 2.0-beta4 > Reporter: Seyguai > Priority: Minor > > - Using style or highlight patterns over a Throwable pattern makes this code > useless if the exception is the only content (i.e. "%highlight{%ex}") > {code:java} > org.apache.logging.log4j.core.pattern.ThrowablePatternConverter:format(final > LogEvent event, final StringBuilder toAppendTo) { > ... > final int len = toAppendTo.length(); > if (len > 0 && !Character.isWhitespace(toAppendTo.charAt(len - > 1))) { > toAppendTo.append(" "); // Add a space before printing stack > trace > } > ... > } > {code} > (With style or highlight patterns, the format is called with a new empty > StringBuilder) > Example: > {noformat} > 17:26:54.395 ERROR # Server.doOperation catchingjava.lang.NullPointerException > {noformat} > (Syntax affected, no space between the message 'catching' and the exception > 'java.lang.NullPointerException') > - Using style or highlight patterns over a Throwable pattern makes it to be > duplicated (the pattern is interpreted as if there was nothing dealing with > throwables) > Examples: > {noformat} > Simple pattern: "%m %ex{short}" > Output: catching java.lang.NullPointerException > at net.wm.core.prototype.Server.doOperation(Server.java:27) > {noformat} > {noformat} > Pattern with highlight: "%m %highlight{%ex{short}}" > Output: catching java.lang.NullPointerException > at net.wm.core.prototype.Server.doOperation(Server.java:27) > java.lang.NullPointerException > at net.wm.core.prototype.Server.doOperation(Server.java:27) [bin/:?] > at net.wm.core.AbstractOperation.run(AbstractOperation.java:99) > [bin/:?] > at java.lang.Thread.run(Unknown Source) [?:1.7.0_07] > {noformat} -- This message is automatically generated by JIRA. If you think it was sent incorrectly, please contact your JIRA administrators For more information on JIRA, see: http://www.atlassian.com/software/jira --------------------------------------------------------------------- To unsubscribe, e-mail: log4j-dev-unsubscr...@logging.apache.org For additional commands, e-mail: log4j-dev-h...@logging.apache.org