File tree Expand file tree Collapse file tree
java/com/baeldung/mdc/logback Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -7,7 +7,10 @@ _Log4j MDC_
77* < https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/MDC.html >
88* < http://veerasundar.com/blog/2009/10/log4j-mdc-mapped-diagnostic-context-what-and-why/ >
99
10- _ Log4j2 _
10+ _ Log4j2 MDC _
1111* < https://logging.apache.org/log4j/2.x/manual/thread-context.html >
1212
13+ _ Logback MDC_
14+ * < http://logback.qos.ch/manual/mdc.html >
15+
1316
Original file line number Diff line number Diff line change 3434 <version >3.3.4</version >
3535 </dependency >
3636
37+ <!-- logback dependencies-->
38+ <dependency >
39+ <groupId >ch.qos.logback</groupId >
40+ <artifactId >logback-classic</artifactId >
41+ <version >1.1.7</version >
42+ </dependency >
43+
3744 </dependencies >
3845
3946</project >
Original file line number Diff line number Diff line change 1+ package com .baeldung .mdc .logback ;
2+
3+ import org .slf4j .Logger ;
4+ import org .slf4j .LoggerFactory ;
5+
6+ import com .baeldung .mdc .BusinessService ;
7+
8+ public class LogbackBusinessService implements BusinessService {
9+
10+ private static final Logger logger = LoggerFactory .getLogger (LogbackBusinessService .class );
11+
12+ @ Override
13+ public void businessMethod (String transactionId ) {
14+ logger .info ("Executing transaction #{}" , transactionId );
15+ }
16+
17+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .mdc .logback ;
2+
3+ public class LogbackDemo {
4+
5+ public static void main (String [] args ) throws InterruptedException {
6+ LogbackExecutor greeter = new LogbackExecutor ();
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 .logback ;
2+
3+ import org .slf4j .MDC ;
4+
5+ import com .baeldung .mdc .TransactionContext ;
6+
7+ public class LogbackExecutor 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+ MDC .put ("transaction.id" , transactionId );
16+ MDC .put ("transaction.owner" , owner );
17+ MDC .put ("transaction.createdAt" , ctx .getCreatedAt ().toString ());
18+
19+ new LogbackBusinessService ().businessMethod (ctx .getTransactionId ());
20+ }
21+
22+ }
Original file line number Diff line number Diff line change 1+ <configuration >
2+
3+ # Console appender
4+ <appender name =" stdout" class =" ch.qos.logback.core.ConsoleAppender" >
5+ <encoder class =" ch.qos.logback.classic.encoder.PatternLayoutEncoder" >
6+ <pattern >logback %d{yyyy-MM-dd HH:mm:ss} %p %m %X{transaction.owner} %X{transaction.createdAt} %n</pattern >
7+ </encoder >
8+ </appender >
9+
10+ <root level =" TRACE" >
11+ <appender-ref ref =" stdout" />
12+ </root >
13+
14+ </configuration >
You can’t perform that action at this time.
0 commit comments