File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ### Relevant Articles:
2+ - TBD
3+
4+ ### References
5+
6+ _ Log4j MDC_
7+ * < https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/MDC.html >
8+ * < http://veerasundar.com/blog/2009/10/log4j-mdc-mapped-diagnostic-context-what-and-why/ >
9+
10+ _ Log4j2_
11+ * < https://logging.apache.org/log4j/2.x/manual/thread-context.html >
12+
13+
Original file line number Diff line number Diff line change 1+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
2+ xsi:schemaLocation=" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
3+ <modelVersion >4.0.0</modelVersion >
4+ <groupId >com.baeldug</groupId >
5+ <artifactId >logmdcndc</artifactId >
6+ <version >0.0.1-SNAPSHOT</version >
7+ <name >logmdcndc</name >
8+ <description >tutorial on logging with MDC and NDC</description >
9+
10+ <dependencies >
11+ <!-- log4j dependencies -->
12+ <dependency >
13+ <groupId >log4j</groupId >
14+ <artifactId >log4j</artifactId >
15+ <version >1.2.17</version >
16+ </dependency >
17+
18+ <!-- log4j2 dependencies-->
19+ <dependency >
20+ <groupId >org.apache.logging.log4j</groupId >
21+ <artifactId >log4j-api</artifactId >
22+ <version >2.7</version >
23+ </dependency >
24+ <dependency >
25+ <groupId >org.apache.logging.log4j</groupId >
26+ <artifactId >log4j-core</artifactId >
27+ <version >2.7</version >
28+ </dependency >
29+
30+ <!-- disruptor for log4j2 async logging-->
31+ <dependency >
32+ <groupId >com.lmax</groupId >
33+ <artifactId >disruptor</artifactId >
34+ <version >3.3.4</version >
35+ </dependency >
36+
37+ </dependencies >
38+
39+ </project >
Original file line number Diff line number Diff line change 1+ package com .baeldung .mdc ;
2+
3+ public interface BusinessService {
4+
5+ void businessMethod (String transactionId );
6+
7+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .mdc ;
2+
3+ import java .util .Date ;
4+
5+ public class TransactionContext {
6+
7+ private String transactionId ;
8+ private String owner ;
9+ private Date createdAt ;
10+
11+ public TransactionContext (String transactionId , String owner ) {
12+ this .transactionId = transactionId ;
13+ this .owner = owner ;
14+ this .createdAt = new Date ();
15+ }
16+
17+ public String getOwner () {
18+ return owner ;
19+ }
20+
21+ public String getTransactionId () {
22+ return transactionId ;
23+ }
24+
25+ public Date getCreatedAt () {
26+ return createdAt ;
27+ }
28+
29+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .mdc .log4j ;
2+
3+ import org .apache .log4j .Logger ;
4+
5+ import com .baeldung .mdc .BusinessService ;
6+
7+ public class Log4jBusinessService implements BusinessService {
8+
9+ private final static Logger logger = Logger .getLogger (Log4jBusinessService .class );
10+
11+ @ Override
12+ public void businessMethod (String transactionId ) {
13+ logger .info ("Executing transaction #" + transactionId );
14+ }
15+
16+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .mdc .log4j ;
2+
3+ public class Log4jDemo {
4+
5+ public static void main (String [] args ) throws InterruptedException {
6+ Log4jExecutor greeter = new Log4jExecutor ();
7+ Thread thread1 = new Thread (greeter );
8+ Thread thread2 = new Thread (greeter );
9+
10+ thread1 .start ();
11+ thread2 .start ();
12+
13+ thread1 .join ();
14+ thread2 .join ();
15+ }
16+
17+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .mdc .log4j ;
2+
3+ import org .apache .log4j .Logger ;
4+ import org .apache .log4j .MDC ;
5+
6+ import com .baeldung .mdc .TransactionContext ;
7+
8+ public class Log4jExecutor implements Runnable {
9+
10+ public void run () {
11+
12+ String transactionId = "" + Math .random ();
13+ String owner = "owner" + Math .random ();
14+ TransactionContext ctx = new TransactionContext (transactionId , owner );
15+
16+ MDC .put ("transaction.id" , transactionId );
17+ MDC .put ("transaction.owner" , owner );
18+ MDC .put ("transaction.createdAt" , ctx .getCreatedAt ());
19+
20+ new Log4jBusinessService ().businessMethod (ctx .getTransactionId ());
21+ }
22+
23+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .mdc .log4j2 ;
2+
3+
4+
5+ import org .apache .logging .log4j .LogManager ;
6+ import org .apache .logging .log4j .Logger ;
7+
8+ import com .baeldung .mdc .BusinessService ;
9+
10+ public class Log4j2BusinessService implements BusinessService {
11+
12+ private static final Logger logger = LogManager .getLogger (); //Log4j2BusinessService.class);
13+
14+ @ Override
15+ public void businessMethod (String transactionId ) {
16+ logger .info ("Executing transaction #{}" , transactionId );
17+ }
18+
19+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .mdc .log4j2 ;
2+
3+ public class Log4j2Demo {
4+
5+ public static void main (String [] args ) throws InterruptedException {
6+ Log4j2Executor greeter = new Log4j2Executor ();
7+ Thread thread1 = new Thread (greeter );
8+ Thread thread2 = new Thread (greeter );
9+
10+ thread1 .start ();
11+ thread2 .start ();
12+
13+ thread1 .join ();
14+ thread2 .join ();
15+ }
16+
17+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .mdc .log4j2 ;
2+
3+ import org .apache .logging .log4j .ThreadContext ;
4+
5+ import com .baeldung .mdc .TransactionContext ;
6+
7+ public class Log4j2Executor implements Runnable {
8+
9+ public void run () {
10+
11+ String transactionId = "" + Math .random ();
12+ String owner = "owner" + Math .random ();
13+ TransactionContext ctx = new TransactionContext (transactionId , owner );
14+
15+ ThreadContext .put ("transaction.id" , transactionId );
16+ ThreadContext .put ("transaction.owner" , owner );
17+ ThreadContext .put ("transaction.createdAt" , ctx .getCreatedAt ().toString ());
18+
19+ new Log4j2BusinessService ().businessMethod (ctx .getTransactionId ());
20+ }
21+
22+ }
You can’t perform that action at this time.
0 commit comments