使用Spring AOP清理记录器 [英] Logger clean up Using Spring AOP

查看:186
本文介绍了使用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,您可以使用handler(*)切入点,如此答案中的示例代码所述>.这将使您能够从catch块中剔除日志语句,而无需升级(重新抛出)已经正确处理的异常,从而更改了逻辑并有必要在以后将其捕获到其他位置. /p>

We're trying to introduce generic logger in our application using Spring AOP for log statements which are under catch block.

Before 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
}

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 Exception inside catch block something like this for AfterThrowing Advice to work.

After AOP:

try
{
\\Business Logic
}
catch(Exception e){
 \\some recovery mechanism that won't be generic across different layers
throw e;
}

AOP code:

@Aspect
@Sl4j
@Component
public class LoggingAdvice {
    @AfterThrowing(pointcut = "execution (* * com..*(..)", throwing = "e")
    public void myAfterThrowing(JoinPoint joinPoint, Exception e) {    
    log.error("Exception occured",e);
    }
}

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 log.error inside catch block and have it generic using AOP.

解决方案

As was discussed here, @AfterThrowing is nice for logging exceptions which are actually thrown.

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 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.

这篇关于使用Spring AOP清理记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆