Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j2 including library name in stacktrace

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.

like image 573
Capril Aprilovich Avatar asked Jul 13 '15 13:07

Capril Aprilovich


People also ask

What is logger name Log4j2?

The name of log4j2 loggers are case sensitive. Except root logger, all loggers can be obtained through passing their name into LogManager. getLogger() . LoggerContext is a vocal point for Logging system as you may have multiple LoggerContexts inside your application.

How do you get all Appenders in Log4j2?

Enumeration appenders = logger. getAllAppenders(); . . . fileBackupIndex = rollingFileAppender. getMaxBackupIndex();

What is PatternLayout in Log4j2?

The PatternLayout class extends the abstract org. apache. log4j. Layout class and overrides the format() method to structure the logging information according to a supplied pattern.

What is status in Log4j2 properties file?

Configuration: the root element of a log4j2 configuration file; the status attribute represents the level at which internal log4j events should be logged. Appenders: this element contains a list of appenders; in our example, an appender corresponding to the System console is defined.


2 Answers

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

like image 175
Tryking Avatar answered Sep 28 '22 01:09

Tryking


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.

like image 45
Remko Popma Avatar answered Sep 28 '22 02:09

Remko Popma