使用Spring AOP清理记录器 [英] Logger clean up Using Spring AOP
问题描述
我们正在尝试使用Spring AOP在catch块下的日志语句中将通用记录器引入我们的应用程序.
在AOP之前
try
{
\\Business Logic
}
catch(Exception e){
\\some recovery mechanism that won't be generic across different layers
log.error();//These statements needs to be moved to generic logger
}
在阅读了Spring Docs之后,我发现可以使用AfterThrowing建议来完成. 抛出建议后,如果方法因抛出异常而退出,则将执行建议.
为此,我们将通过在catch
块内重新抛出Exception
来更改现有的异常处理代码,以使AfterThrowing
建议起作用.
AOP之后:
try
{
\\Business Logic
}
catch(Exception e){
\\some recovery mechanism that won't be generic across different layers
throw e;
}
AOP代码:
@Aspect
@Sl4j
@Component
public class LoggingAdvice {
@AfterThrowing(pointcut = "execution (* * com..*(..)", throwing = "e")
public void myAfterThrowing(JoinPoint joinPoint, Exception e) {
log.error("Exception occured",e);
}
}
您是否认为有比此方法更好的解决方案,而不是在catch块中重新抛出Exception并按照调用层次结构向上传播Exception?
请注意,任何引发或未经检查的异常都会被AfterThrowing Advice捕获.我要做的就是通过删除catch块内的log.error
并使用AOP将其通用化来执行记录器清理.
如此处所讨论的,@AfterThrowing
非常适合记录实际上是 抛出 的异常. /p>
您的案例非常特殊,因为您要记录被 捕获/处理 的异常.如果您使用完整的AspectJ 代替Spring AOP,您可以使用 We're trying to introduce generic logger in our application using Spring AOP for log statements which are under catch block. Before AOP After going through Spring Docs,I have found this can be done using AfterThrowing advice.
After throwing advice is Advice to be executed if a method exits by throwing an exception. In order to do this We'll to change our existing exception handling code by re throwing After AOP: AOP code: Do you think is there any better solution than this rather than rethrowing Exception in catch block and propagating it upwards as per call hierarchy? Note any raised or unchecked exceptions would be catched anyway by AfterThrowing Advice..All i want to do is perform logger clean up by removing As was discussed here, Your case is quite special as you want to log exceptions which are being caught/handled. If you use full AspectJ instead of Spring AOP for this use case you can use a 这篇关于使用Spring AOP清理记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!handler(*)
切入点,如此答案中的示例代码所述>.这将使您能够从catch
块中剔除日志语句,而无需升级(重新抛出)已经正确处理的异常,从而更改了逻辑并有必要在以后将其捕获到其他位置. /p>try
{
\\Business Logic
}
catch(Exception e){
\\some recovery mechanism that won't be generic across different layers
log.error();//These statements needs to be moved to generic logger
}
Exception
inside catch
block something like this for AfterThrowing
Advice to work.try
{
\\Business Logic
}
catch(Exception e){
\\some recovery mechanism that won't be generic across different layers
throw e;
}
@Aspect
@Sl4j
@Component
public class LoggingAdvice {
@AfterThrowing(pointcut = "execution (* * com..*(..)", throwing = "e")
public void myAfterThrowing(JoinPoint joinPoint, Exception e) {
log.error("Exception occured",e);
}
}
log.error
inside catch block and have it generic using AOP.@AfterThrowing
is nice for logging exceptions which are actually thrown.handler(*)
pointcut as described with sample code in this answer. It would enable you to factor out your log statements from your catch
blocks without the need to escalate (re-throw) exceptions which have already been properly handled, thus changing your logic and making it necessary to catch them somewhere else later.