Log4j2包括stacktrace中的库名

时间:2015-07-13 13:38:40

标签: java spring spring-boot logging log4j2

我刚开始使用log4j2。 但我发现log4j2包含stacktrace中的库名。 我怎么能禁用它?

以下是一个例子:

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

3 个答案:

答案 0 :(得分:7)

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

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

答案 1 :(得分:2)

您需要在模式布局中将alwaysWriteExceptions设置为false。

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

然后在您的模式中添加您想要的throwable选项。

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

  

默认输出绑定到LoggingEvent的Throwable跟踪   将输出通常通过调用找到的完整跟踪   Throwable.printStackTrace()。

     

您可以使用中的选项跟随throwable转换字   form%throwable {option}。

     

%throwable {short}输出Throwable的第一行。

     

%throwable {short.className}输出所在类的名称   发生了异常。

     

%throwable {short.methodName}输出方法名称所在的位置   发生了异常。

     

%throwable {short.fileName}输出所在类的名称   发生了异常。

     

%throwable {short.lineNumber}输出所在的行号   发生了异常。

     

%throwable {short.message}输出消息。

     

%throwable {short.localizedMessage}输出本地化消息。

     

%throwable {n}输出堆栈跟踪的前n行。

     

指定%throwable {none}或%throwable {0}会抑制输出   异常。

如果仍然无法满足您的需求,您需要编写自定义转换器,如此处所述。

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

答案 2 :(得分:0)

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