打印mdc日志
This commit is contained in:
parent
dfcd8633ca
commit
0dccc02d3c
@ -336,7 +336,7 @@ public class AsyncConfig {
|
||||
|
||||
@Bean("hikvisionExecutor")
|
||||
public ThreadPoolTaskExecutor hikvisionExecutor() {
|
||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||
MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor();
|
||||
/** 核心线程数(默认线程数) */
|
||||
executor.setCorePoolSize(1);
|
||||
/** 最大线程数 */
|
||||
|
||||
@ -0,0 +1,64 @@
|
||||
package com.zhgd.xmgl.config;
|
||||
|
||||
import com.zhgd.xmgl.util.RequestIdUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
@Slf4j
|
||||
public class MdcThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {
|
||||
|
||||
/**
|
||||
* 接口请求开启的异步线程会调用下述方法
|
||||
*/
|
||||
@Override
|
||||
public void execute(Runnable task) {
|
||||
Map<String, String> context = MDC.getCopyOfContextMap(); //复制主线程MDC
|
||||
log.info("MDC异步多线程...");
|
||||
super.execute(() -> {
|
||||
if (null != context) {
|
||||
MDC.setContextMap(context); //主线程MDC赋予子线程
|
||||
} else {
|
||||
RequestIdUtil.setRequestId(); //主线程没有MDC就自己生成一个
|
||||
}
|
||||
try {
|
||||
task.run();
|
||||
} finally {
|
||||
try {
|
||||
RequestIdUtil.clear();
|
||||
} catch (Exception e) {
|
||||
log.warn("MDC clear exception:{}", e.getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 定时任务会调用下述方法
|
||||
*/
|
||||
@Override
|
||||
public <T> Future<T> submit(Callable<T> task) {
|
||||
Map<String, String> context = MDC.getCopyOfContextMap(); //复制主线程MDC
|
||||
log.info("MDC异步多线程...");
|
||||
return super.submit(() -> {
|
||||
if (null != context) {
|
||||
MDC.setContextMap(context); //主线程MDC赋予子线程
|
||||
} else {
|
||||
RequestIdUtil.setRequestId(); //主线程没有MDC就自己生成一个
|
||||
}
|
||||
try {
|
||||
return task.call();
|
||||
} finally {
|
||||
try {
|
||||
RequestIdUtil.clear();
|
||||
} catch (Exception e) {
|
||||
log.warn("MDC clear exception:{}", e.getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
68
src/main/java/com/zhgd/xmgl/config/TaskAspect.java
Normal file
68
src/main/java/com/zhgd/xmgl/config/TaskAspect.java
Normal file
@ -0,0 +1,68 @@
|
||||
package com.zhgd.xmgl.config;
|
||||
|
||||
import com.zhgd.xmgl.util.RequestIdUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
@Slf4j
|
||||
public class TaskAspect {
|
||||
|
||||
@Pointcut("@annotation(org.springframework.scheduling.annotation.Scheduled)")
|
||||
public void servicePointcut() {
|
||||
System.out.println("Pointcut: 不会被执行");
|
||||
}
|
||||
|
||||
@Around("servicePointcut()")
|
||||
public void doAround(ProceedingJoinPoint jointPoint) throws Throwable {
|
||||
Map<String, String> context = MDC.getCopyOfContextMap(); //复制主线程MDC
|
||||
// 获取当前访问的class类及类名
|
||||
Class<?> clazz = jointPoint.getTarget().getClass();
|
||||
String clazzName = jointPoint.getTarget().getClass().getName();
|
||||
|
||||
// 获取访问的方法名
|
||||
String methodName = jointPoint.getSignature().getName();
|
||||
try {
|
||||
|
||||
if (null == context) {
|
||||
RequestIdUtil.setRequestId("-t"); //主线程没有MDC就自己生成一个
|
||||
log.info("task方法开始:{}",methodName);
|
||||
}
|
||||
|
||||
// 获取方法所有参数及其类型
|
||||
Object[] args = jointPoint.getArgs();
|
||||
Class[] argClz = ((MethodSignature) jointPoint.getSignature()).getParameterTypes();
|
||||
// 获取访问的方法对象
|
||||
Method method = clazz.getDeclaredMethod(methodName, argClz);
|
||||
|
||||
// 判断当前访问的方法是否存在指定注解
|
||||
if (method.isAnnotationPresent(Scheduled.class)) {
|
||||
Scheduled annotation = method.getAnnotation(Scheduled.class);
|
||||
|
||||
// 获取注解标识值与注解描述
|
||||
// String value = annotation.value();
|
||||
// String desc = annotation.description();
|
||||
|
||||
// 执行目标方法
|
||||
Object proceed = jointPoint.proceed();
|
||||
}
|
||||
} finally {
|
||||
if (null == context) {
|
||||
RequestIdUtil.clear();
|
||||
log.info("task方法结束:{}",methodName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
25
src/main/java/com/zhgd/xmgl/security/RequestIdFilter.java
Normal file
25
src/main/java/com/zhgd/xmgl/security/RequestIdFilter.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.zhgd.xmgl.security;
|
||||
|
||||
import com.zhgd.xmgl.util.RequestIdUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.*;
|
||||
import java.io.IOException;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class RequestIdFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
||||
RequestIdUtil.setRequestId("-i");
|
||||
log.info("接口开始...");
|
||||
try {
|
||||
filterChain.doFilter(servletRequest, servletResponse);
|
||||
} finally {
|
||||
log.info("接口结束...");
|
||||
RequestIdUtil.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
25
src/main/java/com/zhgd/xmgl/util/RequestIdUtil.java
Normal file
25
src/main/java/com/zhgd/xmgl/util/RequestIdUtil.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.zhgd.xmgl.util;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import org.slf4j.MDC;
|
||||
|
||||
public class RequestIdUtil {
|
||||
public static final String REQUEST_ID = "requestId";
|
||||
|
||||
public static void setRequestId() {
|
||||
MDC.put(REQUEST_ID, IdUtil.fastSimpleUUID());
|
||||
}
|
||||
|
||||
public static String getRequestId() {
|
||||
return MDC.get(REQUEST_ID);
|
||||
}
|
||||
|
||||
public static void setRequestId(String s) {
|
||||
MDC.put(REQUEST_ID, IdUtil.fastSimpleUUID() + s);
|
||||
}
|
||||
|
||||
public static void clear() {
|
||||
MDC.clear();
|
||||
}
|
||||
|
||||
}
|
||||
@ -6,7 +6,7 @@
|
||||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %highlight(%-5level) %method:%L %cyan(%logger{50}) - %highlight(%msg) %n</pattern>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{requestId}] [%thread] %highlight(%-5level) %method:%L %cyan(%logger{50}) - %highlight(%msg) %n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
@ -16,7 +16,7 @@
|
||||
<file>${log.path}/zhgd-debug.log</file>
|
||||
<!--日志文件输出格式-->
|
||||
<encoder>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{requestId}] [%thread] %-5level %logger{50} - %msg%n</pattern>
|
||||
<charset>UTF-8</charset> <!-- 设置字符集 -->
|
||||
</encoder>
|
||||
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
|
||||
@ -43,7 +43,7 @@
|
||||
<file>${log.path}/zhgd-info.log</file>
|
||||
<!--日志文件输出格式-->
|
||||
<encoder>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{56}.%method:%L - %msg%n</pattern>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{requestId}] [%thread] %-5level %logger{56}.%method:%L - %msg%n</pattern>
|
||||
<charset>UTF-8</charset>
|
||||
</encoder>
|
||||
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
|
||||
@ -69,7 +69,7 @@
|
||||
<file>${log.path}/zhgd-error.log</file>
|
||||
<!--日志文件输出格式-->
|
||||
<encoder>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{requestId}] [%thread] %-5level %logger{50} - %msg%n</pattern>
|
||||
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
|
||||
</encoder>
|
||||
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
|
||||
@ -95,7 +95,7 @@
|
||||
<file>${log.path}/zhgd-all.log</file>
|
||||
<!--日志文件输出格式-->
|
||||
<encoder>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %highlight(%-5level) %method:%L %cyan(%logger{50}) - %highlight(%msg) %n</pattern>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{requestId}] [%thread] %highlight(%-5level) %method:%L %cyan(%logger{50}) - %highlight(%msg) %n</pattern>
|
||||
<charset>UTF-8</charset> <!-- 设置字符集 -->
|
||||
</encoder>
|
||||
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user