diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/common/config/ConfigurerAdapter.java b/pmapi/src/main/java/com/ningdatech/pmapi/common/config/ConfigurerAdapter.java index a39effe..553ba4f 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/common/config/ConfigurerAdapter.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/common/config/ConfigurerAdapter.java @@ -3,10 +3,13 @@ package com.ningdatech.pmapi.common.config; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; +import com.ningdatech.pmapi.common.interceptor.LogInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.lang.NonNull; import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.nio.charset.StandardCharsets; @@ -34,4 +37,9 @@ public class ConfigurerAdapter implements WebMvcConfigurer { converter.setDefaultCharset(StandardCharsets.UTF_8); converters.add(converter); } + + @Override + public void addInterceptors(@NonNull InterceptorRegistry registry) { + registry.addInterceptor(new LogInterceptor()).addPathPatterns("/**").order(-100); + } } diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/common/interceptor/LogInterceptor.java b/pmapi/src/main/java/com/ningdatech/pmapi/common/interceptor/LogInterceptor.java new file mode 100644 index 0000000..533306c --- /dev/null +++ b/pmapi/src/main/java/com/ningdatech/pmapi/common/interceptor/LogInterceptor.java @@ -0,0 +1,38 @@ +package com.ningdatech.pmapi.common.interceptor; + +import cn.hutool.core.lang.UUID; +import com.ningdatech.pmapi.common.util.MDCUtil; +import org.springframework.lang.NonNull; +import org.springframework.web.servlet.HandlerInterceptor; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + *

+ * LogInterceptor + *

+ * + * @author WendyYang + * @since 2023/10/26 + **/ +public class LogInterceptor implements HandlerInterceptor { + + @Override + public boolean preHandle(@NonNull HttpServletRequest request, + @NonNull HttpServletResponse response, + @NonNull Object handler) { + String traceId = UUID.randomUUID().toString(true); + response.addHeader(MDCUtil.TRACE_ID, traceId); + MDCUtil.setTraceId(traceId); + return true; + } + + @Override + public void afterCompletion(@NonNull HttpServletRequest request, + @NonNull HttpServletResponse response, + @NonNull Object handler, Exception ex) { + MDCUtil.removeTraceId(); + } + +} diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/common/util/MDCUtil.java b/pmapi/src/main/java/com/ningdatech/pmapi/common/util/MDCUtil.java new file mode 100644 index 0000000..8b18c46 --- /dev/null +++ b/pmapi/src/main/java/com/ningdatech/pmapi/common/util/MDCUtil.java @@ -0,0 +1,25 @@ +package com.ningdatech.pmapi.common.util; + +import org.slf4j.MDC; + +/** + *

+ * MDCUtil + *

+ * + * @author WendyYang + * @since 2023/10/26 + **/ +public class MDCUtil { + + public static final String TRACE_ID = "TraceId"; + + public static void setTraceId(String traceId) { + MDC.put(TRACE_ID, traceId); + } + + public static void removeTraceId() { + MDC.remove(TRACE_ID); + } + +} diff --git a/pmapi/src/main/resources/logback-spring.xml b/pmapi/src/main/resources/logback-spring.xml index 75f5472..d35d51f 100644 --- a/pmapi/src/main/resources/logback-spring.xml +++ b/pmapi/src/main/resources/logback-spring.xml @@ -19,7 +19,7 @@ - %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n + [traceId:%X{TraceId}] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n UTF-8 @@ -41,7 +41,7 @@ - %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n + [traceId:%X{TraceId}] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n UTF-8 @@ -51,7 +51,7 @@ - %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n + [traceId:%X{TraceId}] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n UTF-8