diff --git a/log-mdc-ndc/README.md b/log-mdc-ndc/README.md index 29b0c95996..c271cbc63a 100644 --- a/log-mdc-ndc/README.md +++ b/log-mdc-ndc/README.md @@ -7,7 +7,10 @@ _Log4j MDC_ * * -_Log4j2_ +_Log4j2 MDC_ * +_Logback MDC_ +* + diff --git a/log-mdc-ndc/pom.xml b/log-mdc-ndc/pom.xml index b82285a283..9f1d48cae4 100644 --- a/log-mdc-ndc/pom.xml +++ b/log-mdc-ndc/pom.xml @@ -34,6 +34,13 @@ 3.3.4 + + + ch.qos.logback + logback-classic + 1.1.7 + + \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackBusinessService.java new file mode 100644 index 0000000000..d15016c5c9 --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackBusinessService.java @@ -0,0 +1,17 @@ +package com.baeldung.mdc.logback; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.baeldung.mdc.BusinessService; + +public class LogbackBusinessService implements BusinessService { + + private static final Logger logger = LoggerFactory.getLogger(LogbackBusinessService.class); + + @Override + public void businessMethod(String transactionId) { + logger.info("Executing transaction #{}", transactionId); + } + +} \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackDemo.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackDemo.java new file mode 100644 index 0000000000..877ded8cc3 --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackDemo.java @@ -0,0 +1,17 @@ +package com.baeldung.mdc.logback; + +public class LogbackDemo { + + public static void main(String[] args) throws InterruptedException { + LogbackExecutor greeter = new LogbackExecutor(); + Thread thread1 = new Thread(greeter); + Thread thread2 = new Thread(greeter); + + thread1.start(); + thread2.start(); + + thread1.join(); + thread2.join(); + } + +} diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackExecutor.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackExecutor.java new file mode 100644 index 0000000000..1e53ef9585 --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackExecutor.java @@ -0,0 +1,22 @@ +package com.baeldung.mdc.logback; + +import org.slf4j.MDC; + +import com.baeldung.mdc.TransactionContext; + +public class LogbackExecutor implements Runnable { + + public void run() { + + String transactionId = "" + Math.random(); + String owner = "owner" + Math.random(); + TransactionContext ctx = new TransactionContext(transactionId, owner); + + MDC.put("transaction.id", transactionId); + MDC.put("transaction.owner", owner); + MDC.put("transaction.createdAt", ctx.getCreatedAt().toString()); + + new LogbackBusinessService().businessMethod(ctx.getTransactionId()); + } + +} diff --git a/log-mdc-ndc/src/main/resources/logback.xml b/log-mdc-ndc/src/main/resources/logback.xml new file mode 100644 index 0000000000..7bea9976e7 --- /dev/null +++ b/log-mdc-ndc/src/main/resources/logback.xml @@ -0,0 +1,14 @@ + + + # Console appender + + + logback %d{yyyy-MM-dd HH:mm:ss} %p %m %X{transaction.owner} %X{transaction.createdAt} %n + + + + + + + + \ No newline at end of file