Log4j2 在堆栈跟踪中包含库名称

Log4j2 including library name in stacktrace

我刚开始使用 log4j2。 但我发现 log4j2 在堆栈跟踪中包含库名称。 我怎样才能禁用它?

这是一个例子:

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]

我说的是 [] 大括号中的那个名字。

这是我的 log4j 配置

<?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>

这是我的版本:

<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>

忘记提及我的应用程序是 spring-boot based。

您需要在图案布局中将alwaysWriteExceptions设置为false。

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

然后将您想要的可抛出选项添加到您的模式中。

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.

如果您仍然无法通过这些选项满足您的需求,则需要按照此处所述编写自定义转换器。

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

一个比他们的更清楚的例子。

@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());
    }
}

您配置的 PatternLayout 模式不包含显式异常转换器。 Log4j 将提供默认值 %xEx。这包括 jar 文件等。

您可以通过明确指定设置简单的 %ex 转换器来更改此设置。所以你的模式以 ...%m%ex%n 结尾。

我认为更好:[...%m%n%ex]