Log4j 2不会记录%throwable的日志。

huangapple go评论63阅读模式
英文:

Log4j 2 not logging out %throwable

问题

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="throwable: %throwable"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="error">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>
<dependencies>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.13.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.13.3</version>
    </dependency>
</dependencies>
public class Main {
    public static void main(String[] args) {
        Logger logger = LogManager.getLogger();
        try {
            new FileInputStream("asdf");
        } catch(Exception e) {
            logger.error(e);
        }
    }
}

Output:

throwable: java.io.FileNotFoundException: asdf (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at Main.main(Main.java:10)

Documentation for PatternLayout: https://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout
Default (%throwable) should log the entire stack trace

Edit: I am using Java 8

英文:

I want to print the stack trace when logging errors using this basic Log4j log4j2.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;Configuration status=&quot;WARN&quot;&gt;
    &lt;Appenders&gt;
        &lt;Console name=&quot;Console&quot; target=&quot;SYSTEM_OUT&quot;&gt;
            &lt;PatternLayout pattern=&quot;throwable: %throwable&quot;/&gt;
        &lt;/Console&gt;
    &lt;/Appenders&gt;
    &lt;Loggers&gt;
        &lt;Root level=&quot;error&quot;&gt;
            &lt;AppenderRef ref=&quot;Console&quot;/&gt;
        &lt;/Root&gt;
    &lt;/Loggers&gt;
&lt;/Configuration&gt;

This is all I have in my dependencies:

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
            &lt;artifactId&gt;log4j-api&lt;/artifactId&gt;
            &lt;version&gt;2.13.3&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
            &lt;artifactId&gt;log4j-core&lt;/artifactId&gt;
            &lt;version&gt;2.13.3&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;

When I run this (asdf doesn't exist):

public class Main {
    public static void main(String[] args) {
        Logger logger = LogManager.getLogger();
        try {
            new FileInputStream(&quot;asdf&quot;);
        } catch(Exception e) {
            logger.error(e);
        }
    }
}

my output is

throwable: 

and I want something like this:

throwable: java.io.FileNotFoundException: asdf (No such file or directory)
	at java.io.FileInputStream.open0(Native Method)
	at java.io.FileInputStream.open(FileInputStream.java:195)
	at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:138)
	at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:93)
	at Main.main(Main.java:10)

documentation from PatternLayout at: https://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout
default (%throwable) should log the entire stack trace

Any help would be great!

Edit: I am using Java 8

答案1

得分: 1

你正在使用 Logger.error(Object)。所有仅带有单个对象参数的日志记录方法只会记录该对象的 toString() 值,即使它是一个 Throwable(可抛出的)。在你的情况下,追加模式不包含 %m/%msg/%message,因此你在控制台输出中只能看到 "throwable:"。
如果我们将消息添加到模式中,输出将会是:

throwable: java.io.FileNotFoundException: asdf(系统找不到指定的文件)

在使用 Log4j2 时,这是一个相当常见的陷阱,不幸的是,看起来这个问题在将来不会改变。

要正确记录异常及其堆栈跟踪,你可以使用带有单独 Throwable 参数的其中一种日志记录方法,例如 Logger.error(String, Throwable),或者你可以使用 Logger.catching(Level, Throwable)。然而,最好使用带有消息参数的日志记录方法,因为它们允许你描述上下文。否则,你可能很难找出日志消息实际创建的位置。

英文:

You are using Logger.error(Object). All logging methods with a single object parameter only log the toString() value of that object, even if it is a Throwable. In your case the appender pattern does not contain %m/%msg/%message so you are only seeing "throwable:" in the console output.
If we add the message to the pattern the output is:

throwable: java.io.FileNotFoundException: asdf (The system cannot find the file specified) 

This is a pretty common pitfall when using Log4j2 and sadly it appears this won't change in the future.

To properly log the exception and its stack trace you can either use one of the logging methods with separate Throwable parameter, e.g. Logger.error(String, Throwable), or you can use Logger.catching(Level, Throwable). However, the logging methods with message parameter should be preferred since they allow you to describe the context. Otherwise you might have a hard time finding out where the log message was actually created.

huangapple
  • 本文由 发表于 2020年9月4日 02:33:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63729726.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定