Log4j2 including library name in stacktrace

You need to set the alwaysWriteExceptions to false in the pattern layout.

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

Then add to your pattern the throwable options you would like instead.

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

Outputs the Throwable trace bound to the LoggingEvent, by default this will output the full trace as one would normally find with a call to Throwable.printStackTrace().

You can follow the throwable conversion word with an option in the form %throwable{option}.

%throwable{short} outputs the first line of the Throwable.

%throwable{short.className} outputs the name of the class where the exception occurred.

%throwable{short.methodName} outputs the method name where the exception occurred.

%throwable{short.fileName} outputs the name of the class where the exception occurred.

%throwable{short.lineNumber} outputs the line number where the exception occurred.

%throwable{short.message} outputs the message.

%throwable{short.localizedMessage} outputs the localized message.

%throwable{n} outputs the first n lines of the stack trace.

Specifying %throwable{none} or %throwable{0} suppresses output of the exception.

If you still can not satisfy what you want with those options you would need to write a custom converter as described here.

https://logging.apache.org/log4j/2.x/manual/extending.html#PatternConverters

An example that is a little more clear then theirs.

@Plugin(name="HostNameConverter", category ="Converter")
@ConverterKeys({"h","host","hostName"})
public class HostNameConverter extends LogEventPatternConverter {
    /**
     * Constructs an instance of LoggingEventPatternConverter.
     *
     * @param name  name of converter.
     * @param style CSS style for output.
     */
    protected HostNameConverter(String name, String style) {
        super(name, style);
    }

    @Override
    public void format(LogEvent event, StringBuilder toAppendTo) {
        toAppendTo.append(HostNameUtil.getLocalHostName());
    }

    public static HostNameConverter newInstance(String[] options){
        return new HostNameConverter(HostNameConverter.class.getSimpleName(),HostNameConverter.class.getSimpleName());
    }
}

Your configuration's PatternLayout pattern does not contain an explicit exception converter. Log4j will provide a default which is %xEx. This includes the jar file etc.

You can change this by explicitly specifying setting the simple %ex converter. So your pattern ends in ...%m%ex%n.


I think it's better: [...%m%n%ex]