Is Spring AOP the easiest solution for crosscut logging of Http requests (inbound and outbound)?

Viewed 37

I wanted to check to see if I hadn't missed another option for logging.

I want to be able to add logging of HTTP input requests and HTTP output requests without having to add explicit logging constructs just before/after each call. AOP seems to be a way of doing this. Is there another fashion? I was also looking at wiretap/global channel interceptors but this would not appear to apply to inbound-endpoints and outbound-endpoints. Thanks for any pointers.

1 Answers

You could log inbound requests and outbound responses with a javax.servlet.Filter implementing class.

@WebFilter(urlPatterns = {"/*"})
public class logFilter implements Filter {

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
        
                        System.out.println("Inbound request "  + httpServletRequest.getRequestURI());
                        filterChain.doFilter(servletRequest, servletResponse);
                        System.out.println("Outbound response "  + httpServletResponse.getStatus());

        
        }
}
Related