You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by "Robert Schaft (JIRA)" <ji...@apache.org> on 2015/04/29 18:57:06 UTC

[jira] [Created] (LOG4J2-1002) PatternLayout, newlines and Exceptions

Robert Schaft created LOG4J2-1002:
-------------------------------------

             Summary: PatternLayout, newlines and Exceptions
                 Key: LOG4J2-1002
                 URL: https://issues.apache.org/jira/browse/LOG4J2-1002
             Project: Log4j 2
          Issue Type: Bug
          Components: Layouts, Pattern Converters
    Affects Versions: 2.2
         Environment: Windows, eclipse
            Reporter: Robert Schaft


I am struggeling to get the PatternLayout right when using %throwable, %ex or similar.
The first problem is, that if the exception is limited by the number of lines (e.g. with the option {{short}} or providing a number), the converter {{ExtendedThrowablePatternConverter}} doesn't attach a newline to the end of the stack.
On the other hand it does attach a newline at the end of the full stack.

That is why
{quote}
{{<PatternLayout pattern="%msg%n%ex" />}}
{quote}
produces the expected result for message with and without throwables: there are no empty lines in the log file and every log message starts in a new line. 

What about {{%ex\{short\}}}?
{quote}
{{<PatternLayout pattern="%msg%n%ex\{short\}" />}}
{quote}
This has the problem that messages with throwables do not end with a new line. This produces all kinds of problems.

Ok, let's add a newline
{quote}
{{<PatternLayout alwaysWriteExceptions="false" pattern="%msg%n%ex\{short\}%n" />}}
{quote}
This has the problem that messages _without_ throwables are followed by an empty line. This is not acceptable on the console.

So we need something more complicated:
{quote}
{{<PatternLayout alwaysWriteExceptions="false" pattern="%msg%replace\{%n%ex\{short\}%n\}\{\[\r\n]+$\}\{\}%n" />}}
{quote}
Yeah! It works (at least on Windows, Unix, Linux, Mac) and if the undocumented throwable {{separator}} option is not used. But it's ugly and requires the alwaysWriteExceptions because the throwable pattern detection does not work any more.

Short Term solution: Always add a newline to the exception.

Long Term Solution:

Add a conversion pattern {{%onThrowable\{pattern1\}\[\{pattern2\}]}} where pattern1 is appended when there is a throwable attached to the log message and the optional pattern2 is appended when there is no throwable appended.
The {{alwaysWriteExceptions="false"}} parameter could be replaced by {{pattern="%onThrowable\{\}"}} 



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

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org