当前位置: 代码迷 >> 综合 >> log4j2 日志中添加业务单号
  详细解决方案

log4j2 日志中添加业务单号

热度:16   发布时间:2024-01-20 17:44:09.0
  • 日志配置如下,自动从上下文中获取 logId
  • 配置mvc拦截器,在每个请求中生成logId 
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF"><properties><property name="log_pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level [LOGID:%X{logId}] %logger{36}\:%L - %msg%n</property><property name="file_name" value="log" /><property name="every_file_size">10M</property><!-- 日志切割的最小单位 --><property name="output_log_level">INFO</property><!-- 日志输出级别 --><property name="sql_log_level">${sys:sql.log.level:-DEBUG}</property><!-- sql日志输出级别 --></properties><appenders><!-- 定义控制台输出 --><Console name="console" target="SYSTEM_OUT"><!-- <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/> --><PatternLayout charset="UTF-8" pattern="${log_pattern}"/></Console><!-- 显示info级别以上的信息 --><RollingRandomAccessFile name="devLog" fileName="${sys:log.path:-..}/logs/${file_name}.log" filePattern="${sys:log.path:-..}/logs/${file_name}.%d{yyyy-MM-dd-HH}-%i.log" ><PatternLayout charset="UTF-8" pattern="${log_pattern}"/><Policies><SizeBasedTriggeringPolicy size="${every_file_size}"/></Policies><Filters><!-- <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/> --><ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/></Filters><DefaultRolloverStrategy max="10"/></RollingRandomAccessFile><!-- 显示error级别以上的信息 --><RollingRandomAccessFile name="errorLog" fileName="${sys:log.path:-..}/logs/${file_name}_error.log" filePattern="${sys:log.path:-..}/logs/${file_name}_error.%d{yyyy-MM-dd-HH}-%i.log" ><PatternLayout charset="UTF-8" pattern="${log_pattern}"/><Policies><SizeBasedTriggeringPolicy size="${every_file_size}"/></Policies><Filters><ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/></Filters><DefaultRolloverStrategy max="10"/></RollingRandomAccessFile></appenders><loggers><logger name="org.springframework" level="INFO"/><logger name="org.mybatis" level="INFO"/><logger name="org.apache" level="INFO"/><logger name="com.alibaba.nacos" level="ERROR"/><logger name="com.zhongan.mars.dao" level="${sql_log_level}"/><root level="${output_log_level}"><appender-ref ref="console"/><appender-ref ref="devLog"/><appender-ref ref="errorLog"/></root></loggers>
</configuration>
public class LogMDCInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {String logTraceId = null;if (StringUtils.isNotEmpty(request.getHeader(Constants.LOGHEADERKEY))) {logTraceId = request.getHeader(Constants.LOGHEADERKEY);} else {logTraceId = SnowFlakeUtil.getId();}//设置logid  传到feign下游request.setAttribute(Constants.LOGHEADERKEY, logTraceId);return true;}
}
@Configuration
public class WebConfig implements WebMvcConfigurer {/*** 添加拦截器* @param registry*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(getLogMDCInterceptor());}/*** 拦截日志设置 traceId* @return*/@Beanpublic HandlerInterceptor getLogMDCInterceptor() {return new LogMDCInterceptor();}
}