如何快速过滤出一次请求的所有日志?
码农突围
共 5226字,需浏览 11分钟
·
2021-11-15 08:45
点击上方“码农突围”,马上关注
这里是码农充电第一站,回复“666”,获取一份专属大礼包 真爱,请设置“星标”或点个“在看
02、正文
public class Main {
private static final String KEY = "requestId";
private static final Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
// 入口传入请求ID
MDC.put(KEY, UUID.randomUUID().toString());
// 打印日志
logger.debug("log in main thread 1");
logger.debug("log in main thread 2");
logger.debug("log in main thread 3");
// 出口移除请求ID
MDC.remove(KEY);
}
}
MDC.put()
方法传入请求ID,在出口调用MDC.remove()
方法移除请求ID。配置好log4j2.xml 文件后,运行main函数,可以在控制台看到以下日志输出:2018-02-17 13:19:52.606 {requestId=f97ea0fb-2a43-40f4-a3e8-711f776857d0} [main] DEBUG cn.wudashan.Main - log in main thread 1
2018-02-17 13:19:52.609 {requestId=f97ea0fb-2a43-40f4-a3e8-711f776857d0} [main] DEBUG cn.wudashan.Main - log in main thread 2
2018-02-17 13:19:52.609 {requestId=f97ea0fb-2a43-40f4-a3e8-711f776857d0} [main] DEBUG cn.wudashan.Main - log in main thread 3
put()
和remove()
代码,在现网定位问题时,我们就可以通过grep requestId=xxx *.log
快速的过滤出某次请求的所有日志。03、进阶
public class Main {
private static final String KEY = "requestId";
private static final Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
// 入口传入请求ID
MDC.put(KEY, UUID.randomUUID().toString());
// 主线程打印"color: #1e6bb8;word-wrap: break-word;font-weight: bold;border-bottom: 1px solid">日志
logger.debug("log in main thread");
// 异步线程打印"color: #1e6bb8;word-wrap: break-word;font-weight: bold;border-bottom: 1px solid">日志
new Thread(new Runnable() {
@Override
public void run() {
logger.debug("log in other thread");
}
}).start();
// 出口移除请求ID
MDC.remove(KEY);
}
}
2018-02-17 14:05:43.487 {requestId=e6099c85-72be-4986-8a28-de6bb2e52b01} [main] DEBUG cn.wudashan.Main - log in main thread
2018-02-17 14:05:43.490 {} [Thread-1] DEBUG cn.wudashan.Main - log in other thread
MDC.put()
方法传入的请求ID只在当前线程有效。感兴趣的小伙伴可以自己深入一下代码细节。MDCRunnable类
对Runnable接口
进行一层装饰。在创建MDCRunnable类
时保存当前线程的MDC值,在执行run()
方法时再将保存的MDC值拷贝到异步线程中去。代码实现如下:public class MDCRunnable implements Runnable {
private final Runnable runnable;
private final Mapmap;
public MDCRunnable(Runnable runnable) {
this.runnable = runnable;
// 保存当前线程的MDC值
this.map = MDC.getCopyOfContextMap();
}
@Override
public void run() {
// 传入已保存的MDC值
for (Map.Entryentry : map.entrySet()) {
MDC.put(entry.getKey(), entry.getValue());
}
// 装饰器模式,执行run方法
runnable.run();
// 移除已保存的MDC值
for (Map.Entryentry : map.entrySet()) {
MDC.remove(entry.getKey());
}
}
}
public class Main {
private static final String KEY = "requestId";
private static final Logger logger = LoggerFactory.getLogger(Main.class);
private static final ExecutorService EXECUTOR = Executors.newSingleThreadExecutor();
public static void main(String[] args) {
// 入口传入请求ID
MDC.put(KEY, UUID.randomUUID().toString());
// 主线程打印"color: #1e6bb8;word-wrap: break-word;font-weight: bold;border-bottom: 1px solid">日志
logger.debug("log in main thread");
// 异步线程打印"color: #1e6bb8;word-wrap: break-word;font-weight: bold;border-bottom: 1px solid">日志,用MDCRunnable装饰Runnable
new Thread(new MDCRunnable(new Runnable() {
@Override
public void run() {
logger.debug("log in other thread");
}
})).start();
// 异步线程池打印日志,用MDCRunnable装饰Runnable
EXECUTOR.execute(new MDCRunnable(new Runnable() {
@Override
public void run() {
logger.debug("log in other thread pool");
}
}));
EXECUTOR.shutdown();
// 出口移除请求ID
MDC.remove(KEY);
}
}
2018-03-04 23:44:05.343 {requestId=5ee2a117-e090-41d8-977b-cef5dea09d34} [main] DEBUG cn.wudashan.Main - log in main thread
2018-03-04 23:44:05.346 {requestId=5ee2a117-e090-41d8-977b-cef5dea09d34} [Thread-1] DEBUG cn.wudashan.Main - log in other thread
2018-03-04 23:44:05.347 {requestId=5ee2a117-e090-41d8-977b-cef5dea09d34} [pool-2-thread-1] DEBUG cn.wudashan.Main - log in other thread pool
04、总结
- END - 最近热文
• 为什么下载小电影时,经常会卡在99%? • 朝阳群众盯上了望京A座?举报996造成交通严重堵塞 • 院士拿布袋领奖归来,朋友圈刷屏了 • 32岁清华女教授获奖百万走红后回应:人生第一次因颜值受到关注
评论