Skip to content

Commit

Permalink
add Log4j2ContextFilter demo
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Jun 16, 2015
1 parent f150c6d commit 7a3ced9
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 13 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@
<version>3.19.0-GA</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.3</version>
</dependency>
<!-- Testing frameworks and related dependencies -->
<dependency>
<groupId>junit</groupId>
Expand Down
68 changes: 68 additions & 0 deletions src/test/java/log4j2/Log4j2ContextFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package log4j2;

import com.alibaba.mtc.MtContextRunnable;
import com.alibaba.mtc.MtContextThreadLocal;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
* @author ding.lid
*/
public class Log4j2ContextFilter {
static Logger logger = LogManager.getLogger(Log4j2ContextFilter.class);

// log4j2 context
static MtContextThreadLocal<Map<String, String>> mtc = new MtContextThreadLocal<Map<String, String>>() {
@Override
protected void beforeExecute() {
final Map<String, String> log4j2Context = get();
for (Map.Entry<String, String> entry : log4j2Context.entrySet()) {
ThreadContext.put(entry.getKey(), entry.getValue());
}
}

@Override
protected void afterExecute() {
ThreadContext.clearAll();
}

@Override
protected Map<String, String> initialValue() {
return new HashMap<String, String>();
}
};

public static void main(String[] args) throws Exception {
// Init Log Context, set MTC
// 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);
mtc.get().put(TRACE_ID, TRACE_ID_VALUE);

// Log in Main Thread
logger.info("Log in main!");

// Run task in thread pool
final ExecutorService executorService = Executors.newFixedThreadPool(1);
final Runnable task = new Runnable() {
@Override
public void run() {
// Log in thread pool
logger.info("Log in Runnable!");
}
};
final Future<?> submit = executorService.submit(MtContextRunnable.get(task));
submit.get();

executorService.shutdown();
}
}
13 changes: 0 additions & 13 deletions src/test/resources/log4j.xml

This file was deleted.

13 changes: 13 additions & 0 deletions src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} %X{trace-id} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

0 comments on commit 7a3ced9

Please sign in to comment.