I currently have two interceptors that handle a similar function. I'd like to merge these.
One interceptor is an access request logger, that shows the logged in user, the session id, and the requested URL.
The other interceptor is a process time logger.
The access logger, in order to log all that must be logged, logs the request in the preHandle method. The idea is that regardless of what happens after (ie. exceptions), the exact access request will be there.
The process time logger however, due to its nature, must log in the postHandle method.
For merging this functionality, I'd have to move everything into a single postHandle method. However, it seems I may lose some logging if an exception occurs somewhere, especially one that is not (yet) handled properly in the application code.
Is there any guarantee or description as to these considerations?
Simply put, a Spring interceptor is a class that either extends the HandlerInterceptorAdapter class or implements the HandlerInterceptor interface. The HandlerInterceptor contains three main methods:
Please notice, if multiple Spring Interceptors are configured, the preHandle () method is executed in the order of configuration whereas postHandle () and afterCompletion () methods are invoked in the reverse order. 5. Conclusion
For simplicity, I am extending abstract class HandlerInterceptorAdapter. HandlerInterceptorAdapter is abstract adapter class for the HandlerInterceptor interface, for simplified implementation of pre-only/post-only interceptors.
afterCompletion () : After the completion of all request processing i.e. after the view has been rendered. Problem with HandlerInterceptor interface is that your new class will have to implement all three methods irrespective of whether it is needed or not. To avoid overriding, you can use HandlerInterceptorAdapter class.
You can consider merging the logic inside the afterCompletion which will be called even in the case when a handler method throws an exception. A good online example
public class RequestProcessingTimeInterceptor extends HandlerInterceptorAdapter {
private static final Logger logger = LoggerFactory
.getLogger(RequestProcessingTimeInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
long startTime = System.currentTimeMillis();
logger.info("Request URL::" + request.getRequestURL().toString()
+ ":: Start Time=" + System.currentTimeMillis());
request.setAttribute("startTime", startTime);
//if returned false, we need to make sure 'response' is sent
return true;
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("Request URL::" + request.getRequestURL().toString()
+ " Sent to Handler :: Current Time=" + System.currentTimeMillis());
//we can add attributes in the modelAndView and use that in the view page
}
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
long startTime = (Long) request.getAttribute("startTime");
logger.info("Request URL::" + request.getRequestURL().toString()
+ ":: End Time=" + System.currentTimeMillis());
logger.info("Request URL::" + request.getRequestURL().toString()
+ ":: Time Taken=" + (System.currentTimeMillis() - startTime));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With