我的异常日志记录方面将相同的异常记录两次
我正在编写一个独立的应用程序,它必须启动并在无人值守的情况下长时间运行。它需要记录异常并提供足够的信息,以便支持人员了解发生了什么并继续操作,而不是让异常停止。
因此,每个异常都被包装在运行时异常中,然后抛出并由应用程序的不同部分记录。我使用 aop:config 标签创建一个方面来记录应用程序其余部分抛出的运行时异常。然后,该异常将在调用堆栈中向上传递到 UncaughtExceptionHandler 以静默结束异常。 但是,相同的异常会被重复捕获并记录(每个异常由单独的线程写入,并进入单独的日志文件)。在调试器中,两个异常具有相同的 ID。
我的 applicationContext 对此是基本的:
<aop:config> <aop:aspect ref="exceptionLoggingAspect"> <aop:after-throwing method="logException" pointcut="execution(* *.*(..))" throwing="exception" /> </aop:aspect> </aop:config>
UncaughtExceptionHandler 同样基本,至少在我让它工作之前是这样:
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'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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论