-
Notifications
You must be signed in to change notification settings - Fork 21
/
Log4j2Demo.java
51 lines (39 loc) · 1.46 KB
/
Log4j2Demo.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
49
50
51
package com.alibaba.ttl.log4j2;
import com.alibaba.ttl.TtlRunnable;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Log4j2Demo {
private static Logger logger = LogManager.getLogger(Log4j2Demo.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";
ThreadContext.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 = new Runnable() {
@Override
public void run() {
// Log in thread pool
ThreadContext.put("task", new Date().toString());
logger.info("Log in Runnable!");
}
};
return TtlRunnable.get(task);
}
}