我使用logback
来记录java应用程序。如何使用logback
我的测试配置
<appender name="exceptions" class="ch.qos.logback.core.FileAppender">
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator>
<expression>java.lang.Exception.class.isInstance(throwable)</expression>
</evaluator>
<onMatch>ACCEPT</onMatch>
</filter>
<file>exceptions.log</file>
<append>true</append>
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</pattern>
<immediateFlush>false</immediateFlush>
</encoder>
</appender>
答案 0 :(得分:4)
假设您的意思是捕获所有异常,即使是那些未被捕获的异常:您可以尽早在代码中定义未捕获的异常处理程序:
private static void setDefaultUncaughtExceptionHandler() {
try {
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
LOGGER.error("Uncaught Exception detected in thread " + t, e);
}
});
} catch (SecurityException e) {
LOGGER.error("Could not set the Default Uncaught Exception Handler", e);
}
}