我在一个 Spring Boot 项目中使用 logback-spring.xml。我想把所有未捕获的异常记录到一个文件中,基本上只是将控制台输出重定向到文件中。我尝试了几种变化的 logback-spring.xml,但都没有成功。
我尝试了这个:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<logger name="com.mine" level="WARN" additivity="false">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</logger>
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE">
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>
</appender-ref>
</root>
</configuration>
在我的主方法中加入以下代码:
SLF4JBridgeHandler.install();
。我已经尝试了Spring文档中的方法。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
这是我的application.yml文件
logging:
file: C:\dev\assessment-tool\logs\application.log
config: classpath:logback-spring.xml
对于这些问题,它将记录所有启动日志信息,但当抛出未被捕获的异常时,它会转到控制台并不会显示在日志文件中。我该如何让未被捕获的异常进入日志文件?