From 41c1d67c2ae2afa732c65944fc018b660f5a42bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B5=A9=E6=9D=B0?= <897546244@qq.com> Date: Mon, 17 Apr 2023 20:18:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=98=AF=E5=90=A6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=93=8D=E5=BA=94=E5=A4=B4tlogTraceId=EF=BC=8C=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E9=85=8D=E7=BD=AE=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yomahub/tlog/context/TLogContext.java | 10 +++++++ .../yomahub/tlog/spring/TLogPropertyInit.java | 14 ++++++++++ .../lifecircle/TLogPropertyConfiguration.java | 1 + .../springboot/property/TLogProperty.java | 10 +++++++ .../META-INF/tlog-default.properties | 3 ++- .../com/yomahub/tlog/web/TLogWebConfig.java | 27 ++++++++++--------- .../web/interceptor/TLogWebInterceptor.java | 5 +--- 7 files changed, 53 insertions(+), 17 deletions(-) diff --git a/tlog-common/src/main/java/com/yomahub/tlog/context/TLogContext.java b/tlog-common/src/main/java/com/yomahub/tlog/context/TLogContext.java index da25067..493c423 100644 --- a/tlog-common/src/main/java/com/yomahub/tlog/context/TLogContext.java +++ b/tlog-common/src/main/java/com/yomahub/tlog/context/TLogContext.java @@ -17,6 +17,8 @@ public class TLogContext { private static boolean hasLogstash; + private static boolean addResponseTraceId; + private static final TransmittableThreadLocal traceIdTL = new TransmittableThreadLocal<>(); private static final TransmittableThreadLocal preIvkAppTL = new TransmittableThreadLocal<>(); @@ -122,4 +124,12 @@ public class TLogContext { public static void setHasLogstash(boolean hasLogstash) { TLogContext.hasLogstash = hasLogstash; } + + public static boolean isAddResponseTraceId() { + return addResponseTraceId; + } + + public static void setAddResponseTraceId(boolean addResponseTraceId) { + TLogContext.addResponseTraceId = addResponseTraceId; + } } diff --git a/tlog-common/src/main/java/com/yomahub/tlog/spring/TLogPropertyInit.java b/tlog-common/src/main/java/com/yomahub/tlog/spring/TLogPropertyInit.java index 116c113..a33a026 100644 --- a/tlog-common/src/main/java/com/yomahub/tlog/spring/TLogPropertyInit.java +++ b/tlog-common/src/main/java/com/yomahub/tlog/spring/TLogPropertyInit.java @@ -25,6 +25,8 @@ public class TLogPropertyInit implements InitializingBean { private Boolean mdcEnable; + private Boolean addResponseTraceId; + @Override public void afterPropertiesSet() throws Exception { if (StrUtil.isNotBlank(pattern)){ @@ -47,6 +49,10 @@ public class TLogPropertyInit implements InitializingBean { if (BooleanUtil.isTrue(mdcEnable)) { TLogContext.setHasTLogMDC(true); } + + if (BooleanUtil.isTrue(addResponseTraceId)) { + TLogContext.setAddResponseTraceId(true); + } } public String getPattern() { @@ -80,4 +86,12 @@ public class TLogPropertyInit implements InitializingBean { public void setMdcEnable(Boolean mdcEnable) { this.mdcEnable = mdcEnable; } + + public Boolean getAddResponseTraceId() { + return addResponseTraceId; + } + + public void setAddResponseTraceId(Boolean addResponseTraceId) { + this.addResponseTraceId = addResponseTraceId; + } } diff --git a/tlog-spring-boot/tlog-spring-boot-configuration/src/main/java/com/yomahub/tlog/springboot/lifecircle/TLogPropertyConfiguration.java b/tlog-spring-boot/tlog-spring-boot-configuration/src/main/java/com/yomahub/tlog/springboot/lifecircle/TLogPropertyConfiguration.java index 7dda0f7..2bf36b9 100644 --- a/tlog-spring-boot/tlog-spring-boot-configuration/src/main/java/com/yomahub/tlog/springboot/lifecircle/TLogPropertyConfiguration.java +++ b/tlog-spring-boot/tlog-spring-boot-configuration/src/main/java/com/yomahub/tlog/springboot/lifecircle/TLogPropertyConfiguration.java @@ -32,6 +32,7 @@ public class TLogPropertyConfiguration { tLogPropertyInit.setEnableInvokeTimePrint(tLogProperty.enableInvokeTimePrint()); tLogPropertyInit.setIdGenerator(tLogProperty.getIdGenerator()); tLogPropertyInit.setMdcEnable(tLogProperty.getMdcEnable()); + tLogPropertyInit.setAddResponseTraceId(tLogProperty.getAddResponseTraceId()); return tLogPropertyInit; } } diff --git a/tlog-spring-boot/tlog-spring-boot-configuration/src/main/java/com/yomahub/tlog/springboot/property/TLogProperty.java b/tlog-spring-boot/tlog-spring-boot-configuration/src/main/java/com/yomahub/tlog/springboot/property/TLogProperty.java index 0c13eb5..af2376d 100644 --- a/tlog-spring-boot/tlog-spring-boot-configuration/src/main/java/com/yomahub/tlog/springboot/property/TLogProperty.java +++ b/tlog-spring-boot/tlog-spring-boot-configuration/src/main/java/com/yomahub/tlog/springboot/property/TLogProperty.java @@ -19,6 +19,8 @@ public class TLogProperty { private Boolean mdcEnable; + private Boolean addResponseTraceId; + public String getPattern() { return pattern; } @@ -54,4 +56,12 @@ public class TLogProperty { public void setMdcEnable(Boolean mdcEnable) { this.mdcEnable = mdcEnable; } + + public Boolean getAddResponseTraceId() { + return addResponseTraceId; + } + + public void setAddResponseTraceId(Boolean addResponseTraceId) { + this.addResponseTraceId = addResponseTraceId; + } } diff --git a/tlog-spring-boot/tlog-spring-boot-configuration/src/main/resources/META-INF/tlog-default.properties b/tlog-spring-boot/tlog-spring-boot-configuration/src/main/resources/META-INF/tlog-default.properties index 6f04221..1246c06 100644 --- a/tlog-spring-boot/tlog-spring-boot-configuration/src/main/resources/META-INF/tlog-default.properties +++ b/tlog-spring-boot/tlog-spring-boot-configuration/src/main/resources/META-INF/tlog-default.properties @@ -1,4 +1,5 @@ tlog.pattern=<$spanId><$traceId> tlog.enableInvokeTimePrint=false tlog.idGenerator=com.yomahub.tlog.id.TLogDefaultIdGenerator -tlog.mdcEnable=false \ No newline at end of file +tlog.mdcEnable=false +tlog.addResponseTraceId=true \ No newline at end of file diff --git a/tlog-webroot/src/main/java/com/yomahub/tlog/web/TLogWebConfig.java b/tlog-webroot/src/main/java/com/yomahub/tlog/web/TLogWebConfig.java index 22730bb..5176229 100644 --- a/tlog-webroot/src/main/java/com/yomahub/tlog/web/TLogWebConfig.java +++ b/tlog-webroot/src/main/java/com/yomahub/tlog/web/TLogWebConfig.java @@ -2,6 +2,7 @@ package com.yomahub.tlog.web; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ReflectUtil; +import com.yomahub.tlog.context.TLogContext; import com.yomahub.tlog.web.interceptor.TLogWebInterceptor; import com.yomahub.tlog.web.interceptor.TLogWebInvokeTimeInterceptor; import org.springframework.core.Ordered; @@ -27,25 +28,27 @@ public class TLogWebConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { InterceptorRegistration interceptorRegistration; - interceptorRegistration = registry.addInterceptor(new TLogWebInterceptor()); + interceptorRegistration = registry.addInterceptor(new TLogWebInvokeTimeInterceptor()); //这里是为了兼容springboot 1.5.X,1.5.x没有order这个方法 - try{ + try { Method method = ReflectUtil.getMethod(InterceptorRegistration.class, "order", Integer.class); - if (ObjectUtil.isNotNull(method)){ + if (ObjectUtil.isNotNull(method)) { method.invoke(interceptorRegistration, Ordered.HIGHEST_PRECEDENCE); } - }catch (Exception e){ + } catch (Exception e) { } - interceptorRegistration = registry.addInterceptor(new TLogWebInvokeTimeInterceptor()); - //这里是为了兼容springboot 1.5.X,1.5.x没有order这个方法 - try{ - Method method = ReflectUtil.getMethod(InterceptorRegistration.class, "order", Integer.class); - if (ObjectUtil.isNotNull(method)){ - method.invoke(interceptorRegistration, Ordered.HIGHEST_PRECEDENCE); - } - }catch (Exception e){ + if (TLogContext.isAddResponseTraceId()) { + interceptorRegistration = registry.addInterceptor(new TLogWebInterceptor()); + //这里是为了兼容springboot 1.5.X,1.5.x没有order这个方法 + try { + Method method = ReflectUtil.getMethod(InterceptorRegistration.class, "order", Integer.class); + if (ObjectUtil.isNotNull(method)) { + method.invoke(interceptorRegistration, Ordered.HIGHEST_PRECEDENCE); + } + } catch (Exception e) { + } } } diff --git a/tlog-webroot/src/main/java/com/yomahub/tlog/web/interceptor/TLogWebInterceptor.java b/tlog-webroot/src/main/java/com/yomahub/tlog/web/interceptor/TLogWebInterceptor.java index 47cf5a8..c8584fe 100644 --- a/tlog-webroot/src/main/java/com/yomahub/tlog/web/interceptor/TLogWebInterceptor.java +++ b/tlog-webroot/src/main/java/com/yomahub/tlog/web/interceptor/TLogWebInterceptor.java @@ -3,9 +3,8 @@ package com.yomahub.tlog.web.interceptor; import com.yomahub.tlog.constant.TLogConstants; import com.yomahub.tlog.context.TLogContext; import com.yomahub.tlog.web.common.TLogWebCommon; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.web.servlet.ModelAndView; + import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -17,8 +16,6 @@ import javax.servlet.http.HttpServletResponse; */ public class TLogWebInterceptor extends AbsTLogWebHandlerMethodInterceptor { - private static final Logger log = LoggerFactory.getLogger(TLogWebInterceptor.class); - @Override public boolean preHandleByHandlerMethod(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { TLogWebCommon.loadInstance().preHandle(request); -- Gitee