Log4j formatting: Is it possible to truncate stacktraces?

28,186

Solution 1

You can use a EnhancedPatternLayout in log4j to format your stacktraces.

See http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/EnhancedPatternLayout.html, specifically the section about the "throwable" pattern in the pattern table.

Note that support for the %throwable{n} support is rather new and requires at least log4j 1.2.16 (which is the latest at time of writing)

For tracking purposes, this is the ticket that dealt with its implementation: https://issues.apache.org/bugzilla/show_bug.cgi?id=48902

Solution 2

Yup... EnhancedPatternLayout provides this functionality. (Since Log4J-1.2.16, was in extra companions before).

For a log4j config of

<appender name="Console" class="org.apache.log4j.ConsoleAppender">
    <param name="Threshold" value="debug"/>
    <layout class="org.apache.log4j.EnhancedPatternLayout">
        <param name="ConversionPattern" value="%d %-5p [%t] %c.%M - %m%n %throwable{short}"/>
    </layout>
</appender>

and for a piece of Java code like

throw new Exception(new Exception("Inner Exception"));

You get the following in the log file...

java.lang.Exception: java.lang.Exception: Inner Exception

If we remove the '%throwable{short}' from our log4j config file we would get the full stack trace

Solution 3

In log4j2.xml simply add %throwable{short} at end. No need to add param name.

<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %m%n %throwable{short} %n"/>

ref : https://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout

Solution 4

Yes, it is part of log4j since log4j 1.2.16

Here is the original proposal in the Apache issue tracker that describes in detail how each of the applications of the %throwable pattern looks like: https://issues.apache.org/bugzilla/show_bug.cgi?id=48902#c0

Solution 5

I don't know of such an option. But you can extend your current appender (for example RollingFileAppender) and provide an append / doAppend / subAppend method (depending on which class you are extending) to handle this.

The throwable information is contained in LoggingEvent.throwableInformation

That said, I'm not sure you should do this - you can lose important information that way.

Share:
28,186
rompetroll
Author by

rompetroll

java developer in oslo

Updated on July 15, 2020

Comments

  • rompetroll
    rompetroll almost 4 years

    I want to log only the first few lines of Exceptions in my program. I know, I can do something like this to print only the first 5 lines of a stacktrace:

    Throwable e = ...;
    StackTraceElement[] stack = e.getStackTrace();
    int maxLines = (stack.length > 4) ? 5 : stack.length;
    for (int n = 0; n < maxLines; n++) {
        System.err.println(stack[n].toString());
    }
    

    But I would rather use log4j (or slf4j over log4j to be more precise) for logging. Is there a way to tell log4j that it should only print the first 5 lines of a stacktrace?

  • tkruse
    tkruse over 7 years
    Note that in case of nested Exceptions, this will mercylessly cut off the nested messages, so for many real-world use-cases, this is useless. What developers would need would be to limit the number of stack elements per exception in the trace.
  • Gerold Broser
    Gerold Broser over 5 years
    Is this %throwable{...} pattern available with slf4j, too, if slf4j-log4j12 or log4j-slf4j-impl is used?
  • Gaurav
    Gaurav almost 5 years
    is it possible to hide the message "Inner Exception" in the exception?
  • galgal
    galgal over 2 years
    Yes it works, need to add the log4j-over-slf4j bridge dependency. @GeroldBroser
  • BabyishTank
    BabyishTank almost 2 years
    is there a solution for JsonTemplateLayout?