Skip to content

Commit be967da

Browse files
committed
Logback demo added
1 parent 62de84b commit be967da

6 files changed

Lines changed: 81 additions & 1 deletion

File tree

‎log-mdc-ndc/README.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff 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

‎log-mdc-ndc/pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@
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>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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>

0 commit comments

Comments
 (0)