1

So I have written a large chunk of code that mostly has the following lines

public <method_constraint> <methodname>(<some args>) {
    try { 
        // do some logic here 
    } catch(Exception e) { 
        // do some logic here
    } finally { 
        //do some logic here
    }
}

Some of those catch lines have e.printStackTrace(); while some of them don't.

I'm looking at SpringAOP to create some JoinPoints and PointCuts to handle that. Is there anyway for me to actually log the exception being caught without the function throwing out an exception?

What I'm trying to achieve here is whenever I have a 'catch' line, I would like to log the stacktrace of the exception on that catch line.

Is this possible?

Here is one of my existing Aspects. This logs all the method calls made:

@Aspect
@Component
public class MethodCallAspect {

    private static DebugManager logger = DebugManager.getInstance(MethodCallAspect.class);

    @Before("execution(* com.cistera..*(*))")
    public void loggingAdvice(JoinPoint joinPoint) {
        StringBuilder methodCall = new StringBuilder(joinPoint.getTarget().getClass().toString()).append(" - ").append(joinPoint.getSignature().getName());
        logger.debug(methodCall.toString());
    }

}

PS: I could go into each class and log the stacktrace from each 'catch' line but that will be a pain having to update a lot of code.

Thank you.

Miki
  • 103
  • 1
  • 11
  • @StackFlowed: This is totally different as this has Spring-AOP in the picture. :) – Miki Aug 10 '15 at 19:27
  • @NathanHughes: I only want to log the exception on the 'catch' lines. I've handle the exceptions but I need the stacktrace for the maintenance people to understand what's happening down below. – Miki Aug 10 '15 at 19:31
  • @NathanHughes: Yes. Aspectj is usable. I have added some code above as an example of what I have for AOP implementation on this. I was thinking if it would be possible to log the stacktrace for exceptions like HibernateExceptions\ or some runtime exception, etc. – Miki Aug 10 '15 at 19:42

0 Answers0