@@ -3,10 +3,13 @@ package com.ningdatech.pmapi.common.config; | |||||
import com.alibaba.fastjson.serializer.SerializerFeature; | import com.alibaba.fastjson.serializer.SerializerFeature; | ||||
import com.alibaba.fastjson.support.config.FastJsonConfig; | import com.alibaba.fastjson.support.config.FastJsonConfig; | ||||
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; | import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; | ||||
import com.ningdatech.pmapi.common.interceptor.LogInterceptor; | |||||
import org.springframework.context.annotation.Configuration; | import org.springframework.context.annotation.Configuration; | ||||
import org.springframework.http.MediaType; | import org.springframework.http.MediaType; | ||||
import org.springframework.http.converter.HttpMessageConverter; | 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.EnableWebMvc; | ||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | |||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||||
import java.nio.charset.StandardCharsets; | import java.nio.charset.StandardCharsets; | ||||
@@ -34,4 +37,9 @@ public class ConfigurerAdapter implements WebMvcConfigurer { | |||||
converter.setDefaultCharset(StandardCharsets.UTF_8); | converter.setDefaultCharset(StandardCharsets.UTF_8); | ||||
converters.add(converter); | converters.add(converter); | ||||
} | } | ||||
@Override | |||||
public void addInterceptors(@NonNull InterceptorRegistry registry) { | |||||
registry.addInterceptor(new LogInterceptor()).addPathPatterns("/**").order(-100); | |||||
} | |||||
} | } |
@@ -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; | |||||
/** | |||||
* <p> | |||||
* LogInterceptor | |||||
* </p> | |||||
* | |||||
* @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(); | |||||
} | |||||
} |
@@ -0,0 +1,25 @@ | |||||
package com.ningdatech.pmapi.common.util; | |||||
import org.slf4j.MDC; | |||||
/** | |||||
* <p> | |||||
* MDCUtil | |||||
* </p> | |||||
* | |||||
* @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); | |||||
} | |||||
} |
@@ -19,7 +19,7 @@ | |||||
</rollingPolicy> | </rollingPolicy> | ||||
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> | <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> | ||||
<layout class="ch.qos.logback.classic.PatternLayout"> | <layout class="ch.qos.logback.classic.PatternLayout"> | ||||
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n | |||||
<Pattern>[traceId:%X{TraceId}] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n | |||||
</Pattern> | </Pattern> | ||||
</layout> | </layout> | ||||
<charset>UTF-8</charset> | <charset>UTF-8</charset> | ||||
@@ -41,7 +41,7 @@ | |||||
</rollingPolicy> | </rollingPolicy> | ||||
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> | <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> | ||||
<layout class="ch.qos.logback.classic.PatternLayout"> | <layout class="ch.qos.logback.classic.PatternLayout"> | ||||
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n | |||||
<Pattern>[traceId:%X{TraceId}] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n | |||||
</Pattern> | </Pattern> | ||||
</layout> | </layout> | ||||
<charset>UTF-8</charset> | <charset>UTF-8</charset> | ||||
@@ -51,7 +51,7 @@ | |||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||||
<encoder> | <encoder> | ||||
<pattern> | <pattern> | ||||
%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 | |||||
</pattern> | </pattern> | ||||
<charset>UTF-8</charset> | <charset>UTF-8</charset> | ||||
</encoder> | </encoder> | ||||