简体   繁体   中英

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

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.

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, ExpiredJwtException {
        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());

        
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM