英文:
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:
<?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>
This is all I have in my dependencies:
<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>
When I run this (asdf doesn't exist):
public class Main {
public static void main(String[] args) {
Logger logger = LogManager.getLogger();
try {
new FileInputStream("asdf");
} 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.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论