From e4180fb976d2e20f9e2c7f56201dbc695c2f921a Mon Sep 17 00:00:00 2001
From: jasmine <362055143@qq.com>
Date: Tue, 30 Jan 2024 13:40:49 +0000
Subject: [PATCH] =?UTF-8?q?feat:=20=E9=9B=86=E6=88=90TLog=EF=BC=8C?=
=?UTF-8?q?=E8=BD=BB=E9=87=8F=E7=BA=A7=E7=9A=84=E5=88=86=E5=B8=83=E5=BC=8F?=
=?UTF-8?q?=E6=97=A5=E5=BF=97=E6=A0=87=E8=AE=B0=E8=BF=BD=E8=B8=AA=E7=A5=9E?=
=?UTF-8?q?=E5=99=A8=20*=20=E9=9B=86=E6=88=90TLog=EF=BC=8C=E8=BD=BB?=
=?UTF-8?q?=E9=87=8F=E7=BA=A7=E7=9A=84=E5=88=86=E5=B8=83=E5=BC=8F=E6=97=A5?=
=?UTF-8?q?=E5=BF=97=E6=A0=87=E8=AE=B0=E8=BF=BD=E8=B8=AA=E7=A5=9E=E5=99=A8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
continew-admin-common/pom.xml | 6 ++
.../config/properties/TLogProperties.java | 41 ++++++++++++
.../common/config/tlog/TLogConfiguration.java | 54 +++++++++++++++
.../common/config/tlog/TLogServletFilter.java | 64 ++++++++++++++++++
.../common/config/tlog/TLogWebCommon.java | 65 +++++++++++++++++++
.../common/config/tlog/TraceIdGenerator.java | 34 ++++++++++
.../src/main/resources/config/application.yml | 8 ++-
.../src/main/resources/logback-spring.xml | 8 +--
pom.xml | 8 +++
9 files changed, 283 insertions(+), 5 deletions(-)
create mode 100644 continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/properties/TLogProperties.java
create mode 100644 continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/tlog/TLogConfiguration.java
create mode 100644 continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/tlog/TLogServletFilter.java
create mode 100644 continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/tlog/TLogWebCommon.java
create mode 100644 continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/tlog/TraceIdGenerator.java
diff --git a/continew-admin-common/pom.xml b/continew-admin-common/pom.xml
index 2494d00b..65a9a914 100644
--- a/continew-admin-common/pom.xml
+++ b/continew-admin-common/pom.xml
@@ -107,5 +107,11 @@
top.charles7c.continew
continew-starter-json-jackson
+
+
+
+ com.yomahub
+ tlog-web-spring-boot-starter
+
\ No newline at end of file
diff --git a/continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/properties/TLogProperties.java b/continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/properties/TLogProperties.java
new file mode 100644
index 00000000..2784e0a6
--- /dev/null
+++ b/continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/properties/TLogProperties.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.admin.common.config.properties;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+/**
+ * TLog 配置属性
+ *
+ * @author Jasmine
+ * @since 2024/01/30 11:39
+ */
+@Data
+@Component
+@ConfigurationProperties(prefix = "tlog")
+public class TLogProperties {
+ /** 日志标签模板 */
+ private String pattern;
+ /** 自动打印调用参数和时间 */
+ private Boolean enableInvokeTimePrint;
+ /** 自定义TraceId生成器 */
+ private String idGenerator;
+ /** MDC模式 */
+ private Boolean mdcEnable;
+}
diff --git a/continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/tlog/TLogConfiguration.java b/continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/tlog/TLogConfiguration.java
new file mode 100644
index 00000000..cdc29fe9
--- /dev/null
+++ b/continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/tlog/TLogConfiguration.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.admin.common.config.tlog;
+
+import com.yomahub.tlog.id.TLogIdGeneratorLoader;
+import com.yomahub.tlog.spring.TLogPropertyInit;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Primary;
+import top.charles7c.continew.admin.common.config.properties.TLogProperties;
+
+/**
+ * TLog 配置
+ *
+ * @see TLogConfiguration
+ * @author Jasmine
+ * @since 2024/01/30 11:39
+ */
+@Configuration
+public class TLogConfiguration {
+
+ private final TLogProperties tLogProperties;
+
+ public TLogConfiguration(TLogProperties tLogProperties) {
+ this.tLogProperties = tLogProperties;
+ }
+
+ @Bean
+ @Primary
+ public TLogPropertyInit tLogPropertyInit() {
+ TLogPropertyInit tLogPropertyInit = new TLogPropertyInit();
+ tLogPropertyInit.setPattern(tLogProperties.getPattern());
+ tLogPropertyInit.setEnableInvokeTimePrint(tLogProperties.getEnableInvokeTimePrint());
+ tLogPropertyInit.setMdcEnable(tLogProperties.getMdcEnable());
+
+ // 设置自定义TraceId生成器
+ TLogIdGeneratorLoader.setIdGenerator(new TraceIdGenerator());
+ return tLogPropertyInit;
+ }
+}
diff --git a/continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/tlog/TLogServletFilter.java b/continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/tlog/TLogServletFilter.java
new file mode 100644
index 00000000..1e180b22
--- /dev/null
+++ b/continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/tlog/TLogServletFilter.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.admin.common.config.tlog;
+
+import com.yomahub.tlog.constant.TLogConstants;
+import com.yomahub.tlog.context.TLogContext;
+import jakarta.servlet.*;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+
+/**
+ * TLog 过滤器
+ *
+ * @see TLogConfiguration
+ * @author Jasmine
+ * @since 2024/01/30 11:39
+ */
+@Component
+public class TLogServletFilter implements Filter {
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+
+ }
+
+ @Override
+ public void doFilter(ServletRequest request,
+ ServletResponse response,
+ FilterChain chain) throws IOException, ServletException {
+ if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
+ try {
+ TLogWebCommon.loadInstance().preHandle((HttpServletRequest)request);
+ // 把traceId放入response的header,为了方便有些人有这样的需求,从前端拿整条链路的traceId
+ ((HttpServletResponse)response).addHeader(TLogConstants.TLOG_TRACE_KEY, TLogContext.getTraceId());
+ chain.doFilter(request, response);
+ return;
+ } finally {
+ TLogWebCommon.loadInstance().afterCompletion();
+ }
+ }
+ chain.doFilter(request, response);
+ }
+
+ @Override
+ public void destroy() {
+
+ }
+}
diff --git a/continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/tlog/TLogWebCommon.java b/continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/tlog/TLogWebCommon.java
new file mode 100644
index 00000000..d6811a3a
--- /dev/null
+++ b/continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/tlog/TLogWebCommon.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.admin.common.config.tlog;
+
+import com.yomahub.tlog.constant.TLogConstants;
+import com.yomahub.tlog.core.rpc.TLogLabelBean;
+import com.yomahub.tlog.core.rpc.TLogRPCHandler;
+import jakarta.servlet.http.HttpServletRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * TLog
+ *
+ * @see TLogWebCommon
+ * @author Jasmine
+ * @since 2024/01/30 11:39
+ */
+public class TLogWebCommon extends TLogRPCHandler {
+
+ private final static Logger log = LoggerFactory.getLogger(TLogWebCommon.class);
+
+ private static volatile TLogWebCommon tLogWebCommon;
+
+ public static TLogWebCommon loadInstance() {
+ if (tLogWebCommon == null) {
+ synchronized (TLogWebCommon.class) {
+ if (tLogWebCommon == null) {
+ tLogWebCommon = new TLogWebCommon();
+ }
+ }
+ }
+ return tLogWebCommon;
+ }
+
+ public void preHandle(HttpServletRequest request) {
+ String traceId = request.getHeader(TLogConstants.TLOG_TRACE_KEY);
+ String spanId = request.getHeader(TLogConstants.TLOG_SPANID_KEY);
+ String preIvkApp = request.getHeader(TLogConstants.PRE_IVK_APP_KEY);
+ String preIvkHost = request.getHeader(TLogConstants.PRE_IVK_APP_HOST);
+ String preIp = request.getHeader(TLogConstants.PRE_IP_KEY);
+
+ TLogLabelBean labelBean = new TLogLabelBean(preIvkApp, preIvkHost, preIp, traceId, spanId);
+
+ processProviderSide(labelBean);
+ }
+
+ public void afterCompletion() {
+ cleanThreadLocal();
+ }
+}
diff --git a/continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/tlog/TraceIdGenerator.java b/continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/tlog/TraceIdGenerator.java
new file mode 100644
index 00000000..0bd36abd
--- /dev/null
+++ b/continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/tlog/TraceIdGenerator.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.charles7c.continew.admin.common.config.tlog;
+
+import com.yomahub.tlog.id.TLogIdGenerator;
+
+/**
+ * TLog 配置
+ *
+ * @see TraceIdGenerator
+ * @author Jasmine
+ * @since 2024/01/30 11:39
+ */
+public class TraceIdGenerator extends TLogIdGenerator {
+ @Override
+ public String generateTraceId() {
+ String traceId = String.valueOf(System.nanoTime());
+ return traceId;
+ }
+}
\ No newline at end of file
diff --git a/continew-admin-webapi/src/main/resources/config/application.yml b/continew-admin-webapi/src/main/resources/config/application.yml
index 7468f2c7..6a922b3b 100644
--- a/continew-admin-webapi/src/main/resources/config/application.yml
+++ b/continew-admin-webapi/src/main/resources/config/application.yml
@@ -247,4 +247,10 @@ generator:
packageName: service.impl
Controller:
templatePath: generator/Controller.ftl
- packageName: controller
\ No newline at end of file
+ packageName: controller
+
+--- ### TLog
+tlog:
+ enable-invoke-time-print: true
+ pattern: '[$spanId][$traceId]'
+ mdc-enable: false
\ No newline at end of file
diff --git a/continew-admin-webapi/src/main/resources/logback-spring.xml b/continew-admin-webapi/src/main/resources/logback-spring.xml
index 6005daf9..a69cea81 100644
--- a/continew-admin-webapi/src/main/resources/logback-spring.xml
+++ b/continew-admin-webapi/src/main/resources/logback-spring.xml
@@ -25,7 +25,7 @@
-
+
${CONSOLE_LOG_PATTERN}
${LOG_CHARSET}
@@ -33,7 +33,7 @@
-
+
${FILE_LOG_PATTERN}
${LOG_CHARSET}
@@ -52,14 +52,14 @@
${FILE_MAX_HISTORY}
-
+
${FILE_LOG_PATTERN}
${LOG_CHARSET}
-
+
0
diff --git a/pom.xml b/pom.xml
index 51ac2cf6..4e6a8d3f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -33,6 +33,7 @@
2.4.0-SNAPSHOT
+ 1.5.1
@@ -72,6 +73,13 @@
continew-admin-common
${revision}
+
+
+
+ com.yomahub
+ tlog-web-spring-boot-starter
+ ${tlog.version}
+