Method entry-exit logging in Google Guice through AOP
July 30th, 2009
1 comment
This cool tip explains 2 step logic to log all methods entry and exit information with profiling in Guice with AOP (aop-alliance)
1) Define a Guice Module with a method level logging Interceptor binding at all class levels and method levels.
public class MyModule extends AbstractModule
{ public void configure()
{ bindInterceptor(Matchers.any(), Matchers.any(), new LoggingMethodInterceptor());
}
}
2) Define a method level logging interceptor to log entry and exit messages with profiled information.
public class LoggingMethodInterceptor implements MethodInterceptor
{ public Object invoke(MethodInvocation invocation) throws Throwable
{ Object returnValue = null;
long start = System.currentTimeInMillis();
System.out.println("Method "+invocation.getMethod()+" entered from "+invocation.getThis()+" with arguments "+invocation.getArguments());
returnValue = invocation.proceed();
long end = System.currentTimeInMillis();
System.out.println("Method "+invocation.getMethod()+" exited in "+((end-start)/1000L)+" seconds");
return returnValue;
}
}
One could use this logic to extend and introduce many cross-cutting concerns such as Transaction Management, secure access to method