1

我正在编写一个独立的应用程序,它必须启动并长时间无人看管。与其让异常让它停止,不如用足够的信息记录异常,让支持人员知道发生了什么,然后继续。

因此,每个异常都包装在运行时异常中,然后抛出以由应用程序的不同部分记录。我正在使用 aop:config 标签来创建一个方面来记录应用程序其余部分引发的运行时异常。然后异常将继续调用堆栈到 UncaughtExceptionHandler 以静默结束异常。但是,相同的异常被重复捕获并记录(每个异常由单独的线程写入,并转到单独的日志文件)。在调试器中,两个异常具有相同的 ID。

我的 applicationContext 是基本的:

    <aop:配置>
        <aop:aspect ref="exceptionLoggingAspect">
             <aop:after-throwing method="logException"
              pointcut="执行(* *.*(..))" throwing="异常" />
        </aop:aspect>
    </aop:config>

The UncaughtExceptionHandler is equally basic, at least till I get it working :

private void setUncaughtExceptionHandler()
{
    final Handler handler = new Handler();
    Thread.setDefaultUncaughtExceptionHandler(handler);

}

class Handler implements Thread.UncaughtExceptionHandler
{

    @Override
    public void uncaughtException(Thread t, Throwable e)
    {
        System.out.println("Throwable: " + e.getMessage());
        System.out.println(t.toString());
    }
}

I have experimented by restricting the pointcut to a single package, and throwing an exception from that package (not the package the exception logging is in), but it is still logged twice. Is there something fundamentally wrong with this idea ? Advice appreciated.

4

0 回答 0