话不多说直接上干货
1、配置文件
log4j.rootLogger=DEBUG,CONSOLE,MongoDB
#console
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=INFO
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%m%n
#file
#log4j.appender.logfile=com.demo.common.log4j.RoolingAndDateFileAppender
#log4j.appender.logfile.Encoding=UTF-8
#log4j.appender.logfile.file=logs/vision.log
#log4j.appender.logfile.Append=true
#log4j.appender.logfile.Threshold=DEBUG
#log4j.appender.logfile.DatePattern=yyyy-MM-dd
#log4j.appender.logfile.MaxFileSize=50MB
#log4j.appender.logfile.maxIndex=200
#log4j.appender.logfile.expirDays=300
#log4j.appender.logfile.isCleanLog=true
#log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
#log4j.appender.logfile.layout.ConversionPattern=%n[%-5p] TRACE_ID=%X{
traceId} %d{
yyyy-MM-dd HH\:mm\:ss}--- method\:%l%n%m%n
#mongdb
log4j.appender.MongoDB=com.demo.common.log4j.MongoDbAppender
2、请求拦截
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author liudean
* @date 2022/9/1 14:41
* 请求拦截
*/
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Bean
public TraceInterceptor initTraceInterceptor() {
return new TraceInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(initTraceInterceptor()).addPathPatterns("/**");
}
}
3、通过日志的MDC方法设置日志中的traceId,便于日志跟踪定位
import com.demo.common.log4j.Constants;
import com.demo.common.utils.StringUtils;
import org.apache.log4j.MDC;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
/**
* @author liudean
* @date 2022/9/1 14:24
* 设置日志中的traceId,便于日志跟踪定位
*/
public class TraceInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// "traceId"
String traceId = request.getHeader(Constants.LOG_TRACE_ID);//根据请求头获取日志跟踪id(当前流程视觉识别完之后理解等流程就会携带)
if(StringUtils.isNull(traceId)){
String userId = request.getHeader(Constants.USER_ID);
String uuid = UUID.randomUUID().toString().toUpperCase();
traceId = userId+"&"+uuid;//生成日志跟踪id
}
String[] split = traceId.split("&");
MDC.put(Constants.SERVICE,"consumer");
MDC.put(Constants.USER_ID, split[0]);
MDC.put(Constants.LOG_TRACE_ID,traceId);
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
}
}
以下几个步骤不是分布式就不需要使用
1.由于用的是分布式,这是入口服务,使用feign发送请求到其他服务时需要添加请求头信息,下游服务才能获取到日志跟踪id
import com.demo.common.log4j.Constants;
import com.demo.common.utils.StringUtils;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.log4j.MDC;
import org.springframework.stereotype.Component;
import java.util.UUID;
/**
* @author liudean
* @date 2022/9/1 20:03
* 对feign进行拦截和自定义的处理
*/
@Component
public class FeignClientInterceptor implements RequestInterceptor {
/**
* feign进行拦截,并在请求头添加,需要添加的header参数
* */
@Override
public void apply(RequestTemplate requestTemplate) {
String traceId = (String) MDC.get(Constants.LOG_TRACE_ID);
if(StringUtils.isNull(traceId)){
traceId = UUID.randomUUID().toString().toUpperCase();
}
requestTemplate.header(Constants.LOG_TRACE_ID,traceId);//设置日志跟踪id
}
}
2.下游服务需要对feign或者ribbon等发送来的请求进行拦截
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author liudean
* @date 2022/9/1 14:41
* 请求拦截
*/
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Bean
public TraceInterceptor initTraceInterceptor() {
return new TraceInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(initTraceInterceptor()).addPathPatterns("/**");
}
}
3.根据请求头获取日志跟踪id,使用MDC插入到日志中
import com.demo.common.log4j.Constants;
import com.demo.common.utils.StringUtils;
import org.apache.log4j.MDC;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax<