I'm planning to handle logging in spring.
Few suggest to handle it via LoggingInterceptor and some suggest to use AOP cross cutting concerns
which one is the recommended or best practice to follow for enterprise applications?
Thanks.
Both approaches have there own benefits and shortcomings. Logging via LoggingInterceptor is better approach as It is a standard library provided by Java to handle logging, you can do custom logging also.
AOP is better appraoch when you want to implement logging on more granular level as on certain return values of functions or classes. e.g if you want to log request only when the returning value is null. you can do it using AOP as :
@AfterReturning(pointcut = "execution(* com.foo.bar..*.*(..))", returning =
"retVal")
public void logAfterMethod(JoinPoint joinPoint, Object retVal) {
...
}
You can also do logging on returning value using Logging Interceptor but you need to write another function for that. i-e
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object
arg3) throws Throwable {
log = LogFactory.getLog(arg3.getClass());
log.info("LOGGING IS: "+arg1.getName());
}
For production point of view AOP has performance overhead as you are adding overhead to execution of all your service calls. While Logging Intercept is just a class with normal execution cycle.