Log4j2 including library name in stacktrace
Asked Answered
U

3

14

I just started using of log4j2. But i found that log4j2 including library name in stacktrace. How can i disable that?

Here is an example:

java.lang.NullPointerException
    at com.sev.controllers.UserController.login(UserController.java:35) ~[UserController.class:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_31]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_31]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_31]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[?:1.8.0_31]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) ~[spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]

Im talking about that name in [] braces.

Here is my log4j config

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="org.apache.log4j.xml" level="INFO"/>
        <Logger name="org.hibernate" level="INFO"/>
        <Logger name="org.springframework" level="INFO"/>
        <Root level="debug">
            <AppenderRef ref="STDOUT"/>
        </Root>
    </Loggers>
</Configuration>

And here is my versions:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
    <version>1.2.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.3</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.3</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.3</version>
</dependency>

Forget to mention that my app is spring-boot based.

Uroscopy answered 13/7, 2015 at 13:38 Comment(2)
possible duplicate of Log4j formatting: Is it possible to truncate stacktraces?Outhouse
It is not since log4j2 is largely differentBushranger
B
8

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.

Beeves answered 14/7, 2015 at 9:16 Comment(1)
Thanks for help! I just added %ex to the end of my pattern and it helped me!Uroscopy
B
3

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());
    }
}
Bushranger answered 13/7, 2015 at 21:33 Comment(0)
W
1

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

Whin answered 30/8, 2019 at 7:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.