My exception logging aspect is logging the same exception twice

676 Views Asked by At

I'm writing a stand alone application, that has to start up and be left running unattended for long periods of time. Rather than have exceptions bring it to a halt, it needs to log the exception with enough information for the support people to have an idea what happened, and carry on.

As a result each exception is wrapped in a runtime exception, then thrown to be logged by a different part of the application. I'm using aop:config tags to create an aspect to log the runtime exceptions thrown by the rest of the application. The exception would then carry on up the call stack to an UncaughtExceptionHandler to end the exception silently. However, the same exception is being caught repeatedly, and logged (each exception is written by a separate thread, and goes to a separate log file). In the debugger, both exceptions have the same ID.

My applicationContext is basic for this :

    <aop:config>
        <aop:aspect ref="exceptionLoggingAspect">
             <aop:after-throwing method="logException"
              pointcut="execution(* *.*(..))" throwing="exception" />
        </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.

0

There are 0 best solutions below