-
Notifications
You must be signed in to change notification settings - Fork 21
/
Slf4jMdcDemo.java
48 lines (36 loc) · 1.32 KB
/
Slf4jMdcDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.alibaba.ttl.log4j2;
import com.alibaba.ttl.TtlRunnable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Slf4jMdcDemo {
private static final Logger logger = LoggerFactory.getLogger(Slf4jMdcDemo.class);
public static void main(String[] args) throws Exception {
// Log in Main Thread
logger.info("Log in main!");
final ExecutorService executorService = Executors.newFixedThreadPool(2);
// Run task in thread pool
executorService.submit(createTask()).get();
// Init Log Context, set TTL
// More KV if needed
final String TRACE_ID = "trace-id";
final String TRACE_ID_VALUE = "XXX-YYY-ZZZ";
MDC.put(TRACE_ID, TRACE_ID_VALUE);
// Log in Main Thread
logger.info("Log in main!");
executorService.submit(createTask()).get();
logger.info("Exit main");
executorService.shutdown();
}
private static Runnable createTask() {
final Runnable task = () -> {
// Log in thread pool
MDC.put("task", new Date().toString());
logger.info("Log in Runnable!");
};
return TtlRunnable.get(task);
}
}