background
To implement an interceptor, ensure that log stamps are unique for all logs of each thread requested.
The solution? Delete the log stamp when entering the controller method. 2. Set the log stamp 3 when printing logs for the first time. 4. At the end of the controller method, delete the log stamp
Based on the interceptor implementation, the custom logging class is required to remove and set the log stamp. The interceptor simply calls the custom logging class method to remove and set the log stamp. This article focuses only on interceptors and ignores custom logging classes.
Introduction to interceptors
The function of the servlet filter is the same as that of the spring interceptor, but the function is the same, and the life cycle is the same, you need to do some unified operations before and after entering the method.
implementation
The implementation class
package com.xxx.payment.intercepter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import xxx.log.logback.layout.converter.LogPreFixConverter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; /** ** log stamp interceptor, * * @author GZH * @createTime 2021/2/2 9:37am */ public class LogPreInterceptor implements HandlerInterceptor { public static final Logger log = LoggerFactory.getLogger(LogPreInterceptor.class); /** * delete the log stamp immediately after entering the controller's method. Then the first time the log is printed, a random log stamp is set. Starting with the second log print, use the log stamp set the first time. * * @param request * @param response * @param handler * @return * @throws Exception */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { LogPreFixConverter.resetLogPreFix(); return true; } /** * After the controller's methods are finished, Delete the log timestamp (postHandle() method is executed after the Controller method returns normally) * * @param Request @param Response @param handler @param modelAndView * @throws Exception */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // LogPreFixConverter.resetLogPreFix(); } /** * After the controller's methods are finished, Remove the log stamp (afterCompletion method is executed regardless of whether the Controller method throws an exception or not) * * @param Request * @param Response * @param handler * @param ex * @throws Exception */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { LogPreFixConverter.resetLogPreFix(); }}Copy the code
configuration
<! < MVC :interceptors> < MVC :interceptor> < MVC :mapping path="/**/*.do"/> <bean class="com.xxx.payment.intercepter.LogPreInterceptor"></bean> </mvc:interceptor> </mvc:interceptors>Copy the code
Filtering request paths
1. Match any directory path /**. 2. Match the specified suffix path /**/*.do
reference
AntPathMatcher This is a path pattern that used in Apache ant, spring team implement it and use it throughout the framework. The mapping matches URLs using the following rules: ? matches one character //? Matches match directories * matches zero or more characters ** matches zero or more 'directories' in a path //**, Match 0 more directories Some examples: com/t? st.jsp - matches com/test.jsp but also com/tast.jsp or com/txst.jsp com/*.jsp - matches all .jsp files in the com directory com/**/test.jsp - matches all test.jsp files underneath the com path org/springframework/**/*.jsp - matches all .jsp files underneath the org/springframework path org/**/servlet/bla.jsp - matches org/springframework/servlet/bla.jsp but also org/springframework/testing/servlet/bla.jsp and org/servlet/bla.jspCopy the code
Opensourceforgeeks.blogspot.com/2016/01/dif…
Method call stack
PreHandle: 32, LogPreInterceptor (com) XXX. Payment. Intercepter) / / custom interceptors applyPreHandle: 133, HandlerExecutionChain (org.springframework.web.servlet) doDispatch:962, DispatcherServlet (org. Springframework. Web. Servlet) / / spring MVC request forwarded doService: 901, DispatcherServlet (org.springframework.web.servlet) processRequest:970, FrameworkServlet (org.springframework.web.servlet) doGet:861, FrameworkServlet (org.springframework.web.servlet) service:621, HttpServlet (javax.servlet.http) service:846, FrameworkServlet (org.springframework.web.servlet) service:728, HttpServlet (javax.servlet.http) internalDoFilter:303, ApplicationFilterChain (org.apache.catalina.core) doFilter:208, ApplicationFilterChain (org.apache.catalina.core) doFilter:52, WsFilter (org.apache.tomcat.websocket.server) internalDoFilter:241, ApplicationFilterChain (org.apache.catalina.core) doFilter:208, ApplicationFilterChain (org.apache.catalina.core) handle:438, CatFilter$Context (com.dianping.cat.servlet) handle:235, CatFilter$CatHandler$3 (com.dianping.cat.servlet) handle:436, CatFilter$Context (com.dianping.cat.servlet) handle:328, CatFilter$CatHandler$4 (com.dianping.cat.servlet) handle:436, CatFilter$Context (com.dianping.cat.servlet) handle:218, CatFilter$CatHandler$2 (com.dianping.cat.servlet) handle:436, CatFilter$Context (com.dianping.cat.servlet) handle:127, CatFilter$CatHandler$1 (com.dianping.cat.servlet) handle:436, CatFilter$Context (com.dianping.cat.servlet) doFilter:65, CatFilter (com.dianping.cat.servlet) internalDoFilter:241, ApplicationFilterChain (org.apache.catalina.core) doFilter:208, ApplicationFilterChain (org.apache.catalina.core) doFilter:31, EncodingFilter (hikefa.core.web.filter) internalDoFilter:241, ApplicationFilterChain (org.apache.catalina.core) doFilter:208, ApplicationFilterChain (org.apache.catalina.core) invoke:219, StandardWrapperValve (org.apache.catalina.core) invoke:110, StandardContextValve (org.apache.catalina.core) invoke:444, AuthenticatorBase (org.apache.catalina.authenticator) invoke:169, StandardHostValve (org.apache.catalina.core) invoke:104, ErrorReportValve (org.apache.catalina.valves) invoke:1025, AccessLogValve (org.apache.catalina.valves) invoke:116, StandardEngineValve (org.apache.catalina.core) service:445, CoyoteAdapter (org.apache.catalina.connector) process:1137, AbstractHttp11Processor (org.apache.coyote.http11) process:637, AbstractProtocol$AbstractConnectionHandler (org.apache.coyote) run:319, JIoEndpoint$SocketProcessor (org.apache.tomcat.util.net) runWorker:1149, ThreadPoolExecutor (java.util.concurrent) run:624, ThreadPoolExecutor$Worker (java.util.concurrent) run:61, TaskThread$WrappingRunnable (org.apache.tomcat.util.threads) run:748, Thread (java.lang)Copy the code
reference
www.liaoxuefeng.com/wiki/125259…