2

我用于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>
4

1 回答 1

5

假设您的意思是捕获所有异常,即使是未捕获的异常:您可以尽早在代码中定义未捕获的异常处理程序:

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);
    }
}
于 2013-08-28T22:24:28.463 回答