alicjasalamon
alicjasalamon

Reputation: 4291

How to use one of @After and @AfterThrowing in AspectJ?

How can I avoid double logging in this situation?

@AfterThrowing(pointcut="execution(String greeting(..))",throwing="e")
public void itsAFoo1(JoinPoint joinPoint, RemoteException e) {
    logger.error(joinPoint.getSignature().toLongString() + " exception here!");
}

@After("execution(String greeting(..))")
public void itsAFoo2(JoinPoint joinPoint, RemoteException e) {
    logger.error(joinPoint.getSignature().toLongString());
}

I got two logs:

[2012-08-07 17:02:01,585] [request1344351718430] [public java.lang.String componentC.Hello.greeting(java.lang.String) exception here!]
[2012-08-07 17:02:01,585] [request1344351718430] [public java.lang.String componentC.Hello.greeting(java.lang.String)]

But I need to receive one if there was an exception and one if there wasn't.

Upvotes: 1

Views: 302

Answers (1)

gkamal
gkamal

Reputation: 21000

Change the @After to an @AfterReturning

@AfterReturning("execution(String greeting(..))")
public void itsAFoo2(JoinPoint joinPoint, RemoteException e) {
    logger.error(joinPoint.getSignature().toLongString());
}

Upvotes: 2

Related Questions