获取Java中的所有异常并远程发送 [英] Get all exceptions in Java and send them remotely
问题描述
我有一个庞大的Java应用程序.我想拦截所有Java异常并通过电子邮件发送它们.我无法在任何地方添加代码以通过try-catch
发送代码,因此可以使用例如Aspect将异常拦截为低级类并获取异常内容吗?
I have a huge Java application. I want to intercept all Java Exceptions adn send them by e-mail. I can't add everywhere code for sending the code via try-catch
so is it possible to use for example Aspect to intercept the exception into low level classes and get the exception content?
还是有某种方法可以覆盖某些内部Java类并获取异常有效负载?
Or is there some way to override some internal Java Class and get the exception payload?
有什么可能?
推荐答案
您可以使用
You can use the @AfterThrowing
advice of spring-aop.
@Aspect
@Component
public class MailExceptionAspect {
@AfterThrowing(value="execution(* com.example..*.*(..))", throwing="ex" )
public void mailAfterThrowing(Throwable ex) {
// do something to send an email
}
}
这将拦截包com.example
中所有未处理的异常.注意,在应用程序中处理(捕获)的异常不能被拦截.
This will intercept all exceptions, that are not handled, in the package com.example
. Beware, that exceptions that are handled (caught) in the application, can not be intercepted.
另一种解决方案是使用应用程序的日志记录框架. logback,log4j等许多框架提供了可以通过电子邮件发送日志的内置配置.
Another solution would be to use the logging framework of the application. Many frameworks, like logback, log4j provide builtin configurations that can send logs by email.
这篇关于获取Java中的所有异常并远程发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!