【问题标题】:Log4j2 including library name in stacktraceLog4j2 包括堆栈跟踪中的库名称
【发布时间】:2015-10-01 19:26:37
【问题描述】:

我刚开始使用 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 的。

【问题讨论】:

标签: java spring spring-boot logging log4j2


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 感谢您的帮助!我刚刚在模式的末尾添加了 %ex,它对我有帮助!
    【解决方案3】:

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

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

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

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

    输出绑定到 LoggingEvent 的 Throwable 跟踪,默认情况下 this 将输出完整的跟踪,就像通常调用 Throwable.printStackTrace()。

    您可以在可抛出的转换词后面加上一个选项 形成 %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());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-01
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-15
      相关资源
      最近更新 更多