我想在使用这个基本的 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>
这就是我的依赖项中的所有内容:
<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>
当我运行它时(asdf 不存在):
public class Main {
public static void main(String[] args) {
Logger logger = LogManager.getLogger();
try {
new FileInputStream("asdf");
} catch(Exception e) {
logger.error(e);
}
}
}
我的输出是
throwable:
我想要这样的东西:
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)
PatternLayout 的文档位于:https ://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout default (%throwable) 应该记录整个堆栈跟踪
任何帮助都会很棒!
编辑:我正在使用 Java 8