添加MDC trace-id.

This commit is contained in:
lijiahang
2023-06-28 22:04:47 +08:00
parent dc0d75a8fb
commit c4cf34330b
3 changed files with 29 additions and 3 deletions

View File

@@ -9,9 +9,9 @@ package com.orion.ops.framework.common.constant;
*/
public interface FilterOrderConst {
int CORS_FILTER = Integer.MIN_VALUE;
int TRICE_ID_FILTER = Integer.MIN_VALUE + 1000;
int TRICE_ID_FILTER = Integer.MIN_VALUE + 10;
int CORS_FILTER = Integer.MIN_VALUE + 2000;
int MYBATIS_CACHE_CLEAR_FILTER = Integer.MIN_VALUE + 100000;

View File

@@ -2,6 +2,7 @@ package com.orion.ops.framework.web.core.filter;
import com.orion.lang.id.UUIds;
import com.orion.ops.framework.common.meta.TraceIdHolder;
import org.slf4j.MDC;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
@@ -21,19 +22,26 @@ public class TraceIdFilter extends OncePerRequestFilter {
private static final String TRACE_ID_HEADER = "trace-id";
private static final String TRACE_ID_MDC = "tid";
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
try {
// 获 traceId
String traceId = UUIds.random32();
// 设置应用上下文
TraceIdHolder.set(traceId);
// 设置日志上下文
MDC.put(TRACE_ID_MDC, traceId);
// 设置响应头
response.setHeader(TRACE_ID_HEADER, traceId);
// 执行请求
filterChain.doFilter(request, response);
} finally {
// 清理缓存
// 清理应用上下文
TraceIdHolder.remove();
// 清理日志上下文
MDC.clear();
}
}

View File

@@ -64,6 +64,9 @@ spring:
type: REDIS
redis:
time-to-live: 1h
output:
ansi:
enabled: detect
mybatis-plus:
configuration:
@@ -91,6 +94,21 @@ knife4j:
setting:
language: zh_cn
logging:
file:
path: ${user.home}/orion/logs/orion-ops-pro
name: ${logging.file.path}/app.log
logback:
rollingpolicy:
clean-history-on-start: false
file-name-pattern: ${logging.file.path}/rolling/orion-ops-rolling-%d{yyyy-MM-dd}.%i.gz
max-history: 30
max-file-size: 16MB
total-size-cap: 0B
pattern:
console: '%clr(%d{${LOG_DATEFORMAT_PATTERN:yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %boldBlue([%X{tid}]) %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx}'
file: "%d{${LOG_DATEFORMAT_PATTERN:yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} [%X{tid}] [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx}"
orion:
# 版本
version: @revision@