保存下简易的logback-spring.xm配置,包括spring多环境配置、本地打印SQL日志、简易链路追踪trace_id拦截器
xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="false">
<springProperty scope="context" name="LOG_PATH" source="logging.file.path" defaultValue="logs"/>
<property name="CONSOLE_LOG_PATTERN" value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr([%-5level]) %clr([%thread]){magenta} %clr([%X{trace_id}]){blue} %clr([%logger{10}:%L]){cyan} - %msg%n"/>
<property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] [%thread] [%X{trace_id}] [%logger{10}:%L] - %msg%n"/>
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="SYSTEM_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/system_info.log</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/system_info.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
</appender>
<appender name="SYSTEM_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/system_error.log</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/system_error.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
</appender>
<springProfile name="local">
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
<logger name="com.meetyou.offer" level="DEBUG"/>
</springProfile>
<springProfile name="!local">
<root level="INFO">
<appender-ref ref="SYSTEM_INFO"/>
<appender-ref ref="SYSTEM_ERROR"/>
</root>
</springProfile>
</configuration>
TraceInterceptor
package com.meetyou.offer.interceptor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
/**
* 简易请求链路跟踪拦截器:后续根据APM情况修改
*
* @author wg
* @since 2022/03/30
*/
@Component
public class TraceInterceptor implements HandlerInterceptor {
public static final String TRACE_ID_HEADER = "trace_id";
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
String traceId = request.getHeader(TRACE_ID_HEADER);
if (StringUtils.isBlank(traceId)) {
MDC.put(TRACE_ID_HEADER, UUID.randomUUID().toString().replace("-",""));
} else {
MDC.put(TRACE_ID_HEADER, traceId);
}
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex) {
MDC.clear();
}
}
打印示例
针对没有接入apm的项目,后续从日志中可以很容易查出请求链路
2022-03-30 15:15:41.428 [DEBUG] [http-nio-8080-exec-2] [ba121bd6f9ab479c939073ca46ecd9d9] [c.m.o.b.o.d.O.selectMaps:137] - ==> Preparing: SELECT count(*) as count, tenant, type FROM offer WHERE (status = ?) GROUP BY tenant,type HAVING count <= 30 ORDER BY tenant ASC
2022-03-30 15:15:41.440 [DEBUG] [http-nio-8080-exec-2] [ba121bd6f9ab479c939073ca46ecd9d9] [c.m.o.b.o.d.O.selectMaps:137] - ==> Parameters: SHELVED(String)
2022-03-30 15:15:41.536 [DEBUG] [http-nio-8080-exec-2] [ba121bd6f9ab479c939073ca46ecd9d9] [c.m.o.b.o.d.O.selectMaps:137] - <== Total: 2
2022-03-30 15:15:41.539 [DEBUG] [http-nio-8080-exec-2] [ba121bd6f9ab479c939073ca46ecd9d9] [c.m.o.t.d.T.selectList:137] - ==> Preparing: SELECT id,code,created_at FROM tenant
2022-03-30 15:15:41.539 [DEBUG] [http-nio-8080-exec-2] [ba121bd6f9ab479c939073ca46ecd9d9] [c.m.o.t.d.T.selectList:137] - ==> Parameters:
2022-03-30 15:15:41.634 [DEBUG] [http-nio-8080-exec-2] [ba121bd6f9ab479c939073ca46ecd9d9] [c.m.o.t.d.T.selectList:137] - <== Total: 5
2022-03-30 15:15:41.657 [INFO ] [http-nio-8080-exec-2] [ba121bd6f9ab479c939073ca46ecd9d9] [c.m.o.m.MonitorController:102] - success