소스 검색

增加调用traceId

master
WendyYang 1 년 전
부모
커밋
6407d91b20
4개의 변경된 파일74개의 추가작업 그리고 3개의 파일을 삭제
  1. +8
    -0
      pmapi/src/main/java/com/ningdatech/pmapi/common/config/ConfigurerAdapter.java
  2. +38
    -0
      pmapi/src/main/java/com/ningdatech/pmapi/common/interceptor/LogInterceptor.java
  3. +25
    -0
      pmapi/src/main/java/com/ningdatech/pmapi/common/util/MDCUtil.java
  4. +3
    -3
      pmapi/src/main/resources/logback-spring.xml

+ 8
- 0
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);
}
}

+ 38
- 0
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;

/**
* <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();
}

}

+ 25
- 0
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;

/**
* <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);
}

}

+ 3
- 3
pmapi/src/main/resources/logback-spring.xml 파일 보기

@@ -19,7 +19,7 @@
</rollingPolicy>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<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>
</layout>
<charset>UTF-8</charset>
@@ -41,7 +41,7 @@
</rollingPolicy>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<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>
</layout>
<charset>UTF-8</charset>
@@ -51,7 +51,7 @@
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<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>
<charset>UTF-8</charset>
</encoder>


불러오는 중...
취소
저장