From 62de84bb5285f1f094837a8319e5d8b27e0a424e Mon Sep 17 00:00:00 2001 From: danidemi Date: Mon, 31 Oct 2016 16:43:48 +0100 Subject: [PATCH 01/10] Log4J MDC, Log4J2 MDC samples added. --- log-mdc-ndc/README.md | 13 +++++++ log-mdc-ndc/pom.xml | 39 +++++++++++++++++++ .../com/baeldung/mdc/BusinessService.java | 7 ++++ .../com/baeldung/mdc/TransactionContext.java | 29 ++++++++++++++ .../mdc/log4j/Log4jBusinessService.java | 16 ++++++++ .../com/baeldung/mdc/log4j/Log4jDemo.java | 17 ++++++++ .../com/baeldung/mdc/log4j/Log4jExecutor.java | 23 +++++++++++ .../mdc/log4j2/Log4j2BusinessService.java | 19 +++++++++ .../com/baeldung/mdc/log4j2/Log4j2Demo.java | 17 ++++++++ .../baeldung/mdc/log4j2/Log4j2Executor.java | 22 +++++++++++ .../src/main/resources/log4j.properties | 12 ++++++ log-mdc-ndc/src/main/resources/log4j2.xml | 32 +++++++++++++++ 12 files changed, 246 insertions(+) create mode 100644 log-mdc-ndc/README.md create mode 100644 log-mdc-ndc/pom.xml create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionContext.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jBusinessService.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jDemo.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jExecutor.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2BusinessService.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Demo.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Executor.java create mode 100644 log-mdc-ndc/src/main/resources/log4j.properties create mode 100644 log-mdc-ndc/src/main/resources/log4j2.xml diff --git a/log-mdc-ndc/README.md b/log-mdc-ndc/README.md new file mode 100644 index 0000000000..29b0c95996 --- /dev/null +++ b/log-mdc-ndc/README.md @@ -0,0 +1,13 @@ +### Relevant Articles: +- TBD + +### References + +_Log4j MDC_ +* +* + +_Log4j2_ +* + + diff --git a/log-mdc-ndc/pom.xml b/log-mdc-ndc/pom.xml new file mode 100644 index 0000000000..b82285a283 --- /dev/null +++ b/log-mdc-ndc/pom.xml @@ -0,0 +1,39 @@ + + 4.0.0 + com.baeldug + logmdcndc + 0.0.1-SNAPSHOT + logmdcndc + tutorial on logging with MDC and NDC + + + + + log4j + log4j + 1.2.17 + + + + + org.apache.logging.log4j + log4j-api + 2.7 + + + org.apache.logging.log4j + log4j-core + 2.7 + + + + + com.lmax + disruptor + 3.3.4 + + + + + \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java new file mode 100644 index 0000000000..dc33580aca --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java @@ -0,0 +1,7 @@ +package com.baeldung.mdc; + +public interface BusinessService { + + void businessMethod(String transactionId); + +} diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionContext.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionContext.java new file mode 100644 index 0000000000..f5694d0df0 --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionContext.java @@ -0,0 +1,29 @@ +package com.baeldung.mdc; + +import java.util.Date; + +public class TransactionContext { + + private String transactionId; + private String owner; + private Date createdAt; + + public TransactionContext(String transactionId, String owner) { + this.transactionId = transactionId; + this.owner = owner; + this.createdAt = new Date(); + } + + public String getOwner() { + return owner; + } + + public String getTransactionId() { + return transactionId; + } + + public Date getCreatedAt() { + return createdAt; + } + +} diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jBusinessService.java new file mode 100644 index 0000000000..4587f49be4 --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jBusinessService.java @@ -0,0 +1,16 @@ +package com.baeldung.mdc.log4j; + +import org.apache.log4j.Logger; + +import com.baeldung.mdc.BusinessService; + +public class Log4jBusinessService implements BusinessService { + + private final static Logger logger = Logger.getLogger(Log4jBusinessService.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/log4j/Log4jDemo.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jDemo.java new file mode 100644 index 0000000000..de3945020b --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jDemo.java @@ -0,0 +1,17 @@ +package com.baeldung.mdc.log4j; + +public class Log4jDemo { + + public static void main(String[] args) throws InterruptedException { + Log4jExecutor greeter = new Log4jExecutor(); + 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/log4j/Log4jExecutor.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jExecutor.java new file mode 100644 index 0000000000..fb949aa4ef --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jExecutor.java @@ -0,0 +1,23 @@ +package com.baeldung.mdc.log4j; + +import org.apache.log4j.Logger; +import org.apache.log4j.MDC; + +import com.baeldung.mdc.TransactionContext; + +public class Log4jExecutor 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()); + + new Log4jBusinessService().businessMethod(ctx.getTransactionId()); + } + +} diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2BusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2BusinessService.java new file mode 100644 index 0000000000..e9e0461a0a --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2BusinessService.java @@ -0,0 +1,19 @@ +package com.baeldung.mdc.log4j2; + + + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import com.baeldung.mdc.BusinessService; + +public class Log4j2BusinessService implements BusinessService { + + private static final Logger logger = LogManager.getLogger(); //Log4j2BusinessService.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/log4j2/Log4j2Demo.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Demo.java new file mode 100644 index 0000000000..0dec8a9a18 --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Demo.java @@ -0,0 +1,17 @@ +package com.baeldung.mdc.log4j2; + +public class Log4j2Demo { + + public static void main(String[] args) throws InterruptedException { + Log4j2Executor greeter = new Log4j2Executor(); + 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/log4j2/Log4j2Executor.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Executor.java new file mode 100644 index 0000000000..b206638ceb --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Executor.java @@ -0,0 +1,22 @@ +package com.baeldung.mdc.log4j2; + +import org.apache.logging.log4j.ThreadContext; + +import com.baeldung.mdc.TransactionContext; + +public class Log4j2Executor implements Runnable { + + public void run() { + + String transactionId = "" + Math.random(); + String owner = "owner" + Math.random(); + TransactionContext ctx = new TransactionContext(transactionId, owner); + + ThreadContext.put("transaction.id", transactionId); + ThreadContext.put("transaction.owner", owner); + ThreadContext.put("transaction.createdAt", ctx.getCreatedAt().toString()); + + new Log4j2BusinessService().businessMethod(ctx.getTransactionId()); + } + +} diff --git a/log-mdc-ndc/src/main/resources/log4j.properties b/log-mdc-ndc/src/main/resources/log4j.properties new file mode 100644 index 0000000000..dac8abf6b8 --- /dev/null +++ b/log-mdc-ndc/src/main/resources/log4j.properties @@ -0,0 +1,12 @@ +log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender +log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout + +log4j.appender.file=org.apache.log4j.FileAppender +log4j.appender.file.File=/tmp/$X{transaction.owner} +log4j.appender.file.layout=org.apache.log4j.PatternLayout + +#note the %X{userName} - this is how you fetch data from Mapped Diagnostic Context (MDC) +log4j.appender.consoleAppender.layout.ConversionPattern=%-4r [%t] %5p %c %x - %m - owner=%X{transaction.owner} creation=%X{transaction.createdAt}%n +log4j.appender.file.layout.ConversionPattern=%-4r [%t] %5p %c %x - %m - owner=%X{transaction.owner} creation=%X{transaction.createdAt}%n + +log4j.rootLogger = ALL, consoleAppender, file \ No newline at end of file diff --git a/log-mdc-ndc/src/main/resources/log4j2.xml b/log-mdc-ndc/src/main/resources/log4j2.xml new file mode 100644 index 0000000000..f9a2bdcf1a --- /dev/null +++ b/log-mdc-ndc/src/main/resources/log4j2.xml @@ -0,0 +1,32 @@ + + + + # Console appender + + # Pattern of log message for console appender + + + + # File appender + + + + + # Override log level for specified package + + + + + + + + \ No newline at end of file From be967daeb39f0f4a0f42a11568d3c4f278cb7a0b Mon Sep 17 00:00:00 2001 From: danidemi Date: Mon, 31 Oct 2016 17:02:30 +0100 Subject: [PATCH 02/10] Logback demo added --- log-mdc-ndc/README.md | 5 ++++- log-mdc-ndc/pom.xml | 7 ++++++ .../mdc/logback/LogbackBusinessService.java | 17 ++++++++++++++ .../com/baeldung/mdc/logback/LogbackDemo.java | 17 ++++++++++++++ .../baeldung/mdc/logback/LogbackExecutor.java | 22 +++++++++++++++++++ log-mdc-ndc/src/main/resources/logback.xml | 14 ++++++++++++ 6 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackBusinessService.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackDemo.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackExecutor.java create mode 100644 log-mdc-ndc/src/main/resources/logback.xml 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 From 189bee78720d88d6c5e28c34157df28d6a27c3fd Mon Sep 17 00:00:00 2001 From: danidemi Date: Tue, 1 Nov 2016 22:49:32 +0100 Subject: [PATCH 03/10] Intermediate work --- log-mdc-ndc/pom.xml | 22 +++++++++ .../com/baeldung/mdc/BusinessService.java | 2 +- .../baeldung/mdc/DefaultBusinessService.java | 15 ++++++ .../com/baeldung/mdc/DelegateBusiness.java | 30 ++++++++++++ .../java/com/baeldung/mdc/GenericApp.java | 46 +++++++++++++++++++ .../com/baeldung/mdc/TransactionContext.java | 6 +++ .../com/baeldung/mdc/TransactionFactory.java | 10 ++++ .../mdc/log4j/Log4jBusinessService.java | 5 +- .../com/baeldung/mdc/log4j/Log4jExecutor.java | 12 ++--- .../mdc/log4j2/Log4j2BusinessService.java | 5 +- .../baeldung/mdc/log4j2/Log4j2Executor.java | 11 ++--- .../mdc/logback/LogbackBusinessService.java | 5 +- .../baeldung/mdc/logback/LogbackExecutor.java | 13 +++--- .../java/com/baeldung/web/Application.java | 12 +++++ .../com/baeldung/web/GreetingController.java | 18 ++++++++ 15 files changed, 186 insertions(+), 26 deletions(-) create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/DefaultBusinessService.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/DelegateBusiness.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/GenericApp.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/web/Application.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/web/GreetingController.java diff --git a/log-mdc-ndc/pom.xml b/log-mdc-ndc/pom.xml index 9f1d48cae4..7e973b940e 100644 --- a/log-mdc-ndc/pom.xml +++ b/log-mdc-ndc/pom.xml @@ -6,8 +6,30 @@ 0.0.1-SNAPSHOT logmdcndc tutorial on logging with MDC and NDC + + + org.springframework.boot + spring-boot-starter-parent + 1.4.1.RELEASE + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + com.jayway.jsonpath + json-path + test + + log4j diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java index dc33580aca..21d065dc90 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java @@ -2,6 +2,6 @@ package com.baeldung.mdc; public interface BusinessService { - void businessMethod(String transactionId); + boolean transfer(Long amount); } diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/DefaultBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/DefaultBusinessService.java new file mode 100644 index 0000000000..1df6783185 --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/DefaultBusinessService.java @@ -0,0 +1,15 @@ +package com.baeldung.mdc; + +public class DefaultBusinessService implements BusinessService { + + @Override + public boolean transfer(Long amount) { + try { + Thread.sleep((long) (Math.random()*1000)); + } catch (InterruptedException e) { + // should not happen + } + return Math.random() >= 0.5; + } + +} diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/DelegateBusiness.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/DelegateBusiness.java new file mode 100644 index 0000000000..ada8f79b62 --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/DelegateBusiness.java @@ -0,0 +1,30 @@ +package com.baeldung.mdc; + +public abstract class DelegateBusiness { + + private BusinessService delegate = null; + + public DelegateBusiness(BusinessService delegate) { + super(); + this.delegate = delegate; + } + + + public DelegateBusiness() { + this( new DefaultBusinessService() ); + } + + public boolean transfer(Long amount) { + preTransfer(); + boolean outcome = delegate.transfer(amount); + postTransfer(outcome); + return outcome; + } + + abstract protected void postTransfer(boolean outcome); + + abstract protected void preTransfer(); + + + +} diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/GenericApp.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/GenericApp.java new file mode 100644 index 0000000000..3eee17ba5d --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/GenericApp.java @@ -0,0 +1,46 @@ +package com.baeldung.mdc; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import org.apache.log4j.MDC; + +import com.baeldung.mdc.log4j.Log4jBusinessService; + +public class GenericApp { + public static void main(String[] args) { + + ExecutorService executor = Executors.newFixedThreadPool(3); + TransactionFactory transactionFactory = new TransactionFactory(); + + for(int i=0; i<10; i++){ + + TransactionContext tx = transactionFactory.buildTransaction(); + + executor.submit(new Runnable() { + @Override + public void run() { + new DelegateBusiness() { + + @Override + protected void preTransfer() { + MDC.put("transaction.id", tx.getTransactionId()); + MDC.put("transaction.owner", tx.getOwner()); + MDC.put("transaction.createdAt", tx.getCreatedAt()); + } + + @Override + protected void postTransfer(boolean outcome) { + // TODO Auto-generated method stub + + } + }.transfer( + tx.getAmount() + ); + } + }); + } + + } +} diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionContext.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionContext.java index f5694d0df0..c6750edfda 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionContext.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionContext.java @@ -7,11 +7,13 @@ public class TransactionContext { private String transactionId; private String owner; private Date createdAt; + private Long amount; public TransactionContext(String transactionId, String owner) { this.transactionId = transactionId; this.owner = owner; this.createdAt = new Date(); + this.amount = (long) (Math.random()*100); } public String getOwner() { @@ -26,4 +28,8 @@ public class TransactionContext { return createdAt; } + public Long getAmount() { + return amount; + } + } diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java new file mode 100644 index 0000000000..4631c662c0 --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java @@ -0,0 +1,10 @@ +package com.baeldung.mdc; + +public class TransactionFactory { + + public TransactionContext buildTransaction() { + TransactionContext ctx = new TransactionContext("" + Math.random(), "owner" + Math.random()); + return ctx; + } + +} diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jBusinessService.java index 4587f49be4..415e686cae 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jBusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jBusinessService.java @@ -9,8 +9,9 @@ public class Log4jBusinessService implements BusinessService { private final static Logger logger = Logger.getLogger(Log4jBusinessService.class); @Override - public void businessMethod(String transactionId) { - logger.info("Executing transaction #" + transactionId ); + public boolean transfer(Long amount) { + logger.info("Executing transaction #" + amount ); + return false; } } \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jExecutor.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jExecutor.java index fb949aa4ef..96fe63a3ed 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jExecutor.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jExecutor.java @@ -1,23 +1,21 @@ package com.baeldung.mdc.log4j; -import org.apache.log4j.Logger; import org.apache.log4j.MDC; import com.baeldung.mdc.TransactionContext; +import com.baeldung.mdc.TransactionFactory; public class Log4jExecutor implements Runnable { public void run() { - String transactionId = "" + Math.random(); - String owner = "owner" + Math.random(); - TransactionContext ctx = new TransactionContext(transactionId, owner); + TransactionContext ctx = new TransactionFactory().buildTransaction(); - MDC.put("transaction.id", transactionId); - MDC.put("transaction.owner", owner); + MDC.put("transaction.id", ctx.getTransactionId()); + MDC.put("transaction.owner", ctx.getOwner()); MDC.put("transaction.createdAt", ctx.getCreatedAt()); - new Log4jBusinessService().businessMethod(ctx.getTransactionId()); + new Log4jBusinessService().transfer(ctx.getAmount()); } } diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2BusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2BusinessService.java index e9e0461a0a..d1a8dca518 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2BusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2BusinessService.java @@ -12,8 +12,9 @@ public class Log4j2BusinessService implements BusinessService { private static final Logger logger = LogManager.getLogger(); //Log4j2BusinessService.class); @Override - public void businessMethod(String transactionId) { - logger.info("Executing transaction #{}", transactionId ); + public boolean transfer(Long amount) { + logger.info("Executing transaction #{}", amount ); + return false; } } \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Executor.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Executor.java index b206638ceb..a686486e8e 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Executor.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Executor.java @@ -3,20 +3,19 @@ package com.baeldung.mdc.log4j2; import org.apache.logging.log4j.ThreadContext; import com.baeldung.mdc.TransactionContext; +import com.baeldung.mdc.TransactionFactory; public class Log4j2Executor implements Runnable { public void run() { - String transactionId = "" + Math.random(); - String owner = "owner" + Math.random(); - TransactionContext ctx = new TransactionContext(transactionId, owner); + TransactionContext ctx = new TransactionFactory().buildTransaction(); - ThreadContext.put("transaction.id", transactionId); - ThreadContext.put("transaction.owner", owner); + ThreadContext.put("transaction.id", ctx.getTransactionId()); + ThreadContext.put("transaction.owner", ctx.getOwner()); ThreadContext.put("transaction.createdAt", ctx.getCreatedAt().toString()); - new Log4j2BusinessService().businessMethod(ctx.getTransactionId()); + new Log4j2BusinessService().transfer(ctx.getAmount()); } } 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 index d15016c5c9..74f96ef15e 100644 --- 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 @@ -10,8 +10,9 @@ 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); + public boolean transfer(Long amount) { + logger.info("Executing transaction #{}", amount); + return false; } } \ No newline at end of file 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 index 1e53ef9585..614188b757 100644 --- 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 @@ -3,20 +3,21 @@ package com.baeldung.mdc.logback; import org.slf4j.MDC; import com.baeldung.mdc.TransactionContext; +import com.baeldung.mdc.TransactionFactory; public class LogbackExecutor implements Runnable { public void run() { - String transactionId = "" + Math.random(); - String owner = "owner" + Math.random(); - TransactionContext ctx = new TransactionContext(transactionId, owner); + TransactionContext ctx = new TransactionFactory().buildTransaction(); - MDC.put("transaction.id", transactionId); - MDC.put("transaction.owner", owner); + MDC.put("transaction.id", ctx.getTransactionId()); + MDC.put("transaction.owner", ctx.getOwner()); MDC.put("transaction.createdAt", ctx.getCreatedAt().toString()); - new LogbackBusinessService().businessMethod(ctx.getTransactionId()); + new LogbackBusinessService().transfer(ctx.getAmount()); } + + } diff --git a/log-mdc-ndc/src/main/java/com/baeldung/web/Application.java b/log-mdc-ndc/src/main/java/com/baeldung/web/Application.java new file mode 100644 index 0000000000..a700e345db --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/web/Application.java @@ -0,0 +1,12 @@ +package com.baeldung.web; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/web/GreetingController.java b/log-mdc-ndc/src/main/java/com/baeldung/web/GreetingController.java new file mode 100644 index 0000000000..760e26a705 --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/web/GreetingController.java @@ -0,0 +1,18 @@ +package com.baeldung.web; + +import java.util.concurrent.atomic.AtomicLong; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class GreetingController { + + @GetMapping("/echo") + public String echo(@RequestBody String body) { + return body; + } +} \ No newline at end of file From 9bd83d921cff2b0ccfb0b575a06a5daf2f72dc1b Mon Sep 17 00:00:00 2001 From: danidemi Date: Sun, 6 Nov 2016 15:57:07 +0100 Subject: [PATCH 04/10] Examples done. --- log-mdc-ndc/pom.xml | 32 ++++++----------- .../com/baeldung/mdc/BusinessService.java | 4 ++- .../baeldung/mdc/DefaultBusinessService.java | 14 +++++--- .../com/baeldung/mdc/DelegateBusiness.java | 11 +++--- .../java/com/baeldung/mdc/GenericApp.java | 35 ++++--------------- ...ansactionContext.java => Transaction.java} | 6 ++-- .../com/baeldung/mdc/TransactionFactory.java | 16 +++++++-- .../mdc/log4j/Log4JBusinessService.java | 22 ++++++++++++ .../com/baeldung/mdc/log4j/Log4JRunnable.java | 25 +++++++++++++ .../mdc/log4j/Log4jBusinessService.java | 17 --------- .../com/baeldung/mdc/log4j/Log4jDemo.java | 17 --------- .../com/baeldung/mdc/log4j/Log4jExecutor.java | 21 ----------- .../mdc/log4j2/Log4J2BusinessService.java | 21 +++++++++++ .../baeldung/mdc/log4j2/Log4J2Runnable.java | 25 +++++++++++++ .../mdc/log4j2/Log4j2BusinessService.java | 20 ----------- .../com/baeldung/mdc/log4j2/Log4j2Demo.java | 17 --------- .../baeldung/mdc/log4j2/Log4j2Executor.java | 21 ----------- .../mdc/logback/LogbackBusinessService.java | 18 ---------- .../com/baeldung/mdc/logback/LogbackDemo.java | 17 --------- .../baeldung/mdc/logback/LogbackExecutor.java | 23 ------------ .../mdc/slf4j/Slf4jBusinessService.java | 22 ++++++++++++ .../com/baeldung/mdc/slf4j/Slf4jRunnable.java | 25 +++++++++++++ .../java/com/baeldung/web/Application.java | 12 ------- .../com/baeldung/web/GreetingController.java | 18 ---------- .../src/main/resources/log4j.properties | 10 +++--- 25 files changed, 197 insertions(+), 272 deletions(-) rename log-mdc-ndc/src/main/java/com/baeldung/mdc/{TransactionContext.java => Transaction.java} (76%) create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JBusinessService.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java delete mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jBusinessService.java delete mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jDemo.java delete mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jExecutor.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2BusinessService.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java delete mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2BusinessService.java delete mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Demo.java delete mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Executor.java delete mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackBusinessService.java delete mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackDemo.java delete mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackExecutor.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jBusinessService.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java delete mode 100644 log-mdc-ndc/src/main/java/com/baeldung/web/Application.java delete mode 100644 log-mdc-ndc/src/main/java/com/baeldung/web/GreetingController.java diff --git a/log-mdc-ndc/pom.xml b/log-mdc-ndc/pom.xml index 7e973b940e..8e9d52a750 100644 --- a/log-mdc-ndc/pom.xml +++ b/log-mdc-ndc/pom.xml @@ -7,28 +7,18 @@ logmdcndc tutorial on logging with MDC and NDC - - org.springframework.boot - spring-boot-starter-parent - 1.4.1.RELEASE - - - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-test - test - - - com.jayway.jsonpath - json-path - test - + + + org.springframework + spring-context + 4.3.3.RELEASE + + + org.springframework + spring-webmvc + 4.3.3.RELEASE + diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java index 21d065dc90..51bebeb713 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java @@ -2,6 +2,8 @@ package com.baeldung.mdc; public interface BusinessService { - boolean transfer(Long amount); + /** Sample service transferring a given amount of money. + * @return {@code true} when the transfer complete successfully, {@code false} otherwise. */ + boolean transfer(long amount); } diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/DefaultBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/DefaultBusinessService.java index 1df6783185..d5bb94d21a 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/DefaultBusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/DefaultBusinessService.java @@ -1,15 +1,19 @@ package com.baeldung.mdc; +/** + * A fake {@link BusinessService} simulating an actual one. + */ public class DefaultBusinessService implements BusinessService { - @Override - public boolean transfer(Long amount) { + public boolean transfer(long amount) { try { - Thread.sleep((long) (Math.random()*1000)); + // let's pause randomly to properly simulate an actual system. + Thread.sleep((long) (500 + Math.random()*500)); } catch (InterruptedException e) { - // should not happen + // should never happen } - return Math.random() >= 0.5; + // let's simulate both failing and successful transfers + return Math.random() >= 0.25; } } diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/DelegateBusiness.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/DelegateBusiness.java index ada8f79b62..d7aab0e284 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/DelegateBusiness.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/DelegateBusiness.java @@ -9,21 +9,20 @@ public abstract class DelegateBusiness { this.delegate = delegate; } - public DelegateBusiness() { this( new DefaultBusinessService() ); } - public boolean transfer(Long amount) { - preTransfer(); + public boolean transfer(long amount) { + preTransfer(amount); boolean outcome = delegate.transfer(amount); - postTransfer(outcome); + postTransfer(amount, outcome); return outcome; } - abstract protected void postTransfer(boolean outcome); + abstract protected void postTransfer(long amount, boolean outcome); - abstract protected void preTransfer(); + abstract protected void preTransfer(long amount); diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/GenericApp.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/GenericApp.java index 3eee17ba5d..3652032e0e 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/GenericApp.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/GenericApp.java @@ -1,46 +1,25 @@ package com.baeldung.mdc; -import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import org.apache.log4j.MDC; +import org.apache.log4j.Logger; -import com.baeldung.mdc.log4j.Log4jBusinessService; +import com.baeldung.mdc.log4j.Log4JRunnable; public class GenericApp { + public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(3); TransactionFactory transactionFactory = new TransactionFactory(); for(int i=0; i<10; i++){ - - TransactionContext tx = transactionFactory.buildTransaction(); - - executor.submit(new Runnable() { - @Override - public void run() { - new DelegateBusiness() { - - @Override - protected void preTransfer() { - MDC.put("transaction.id", tx.getTransactionId()); - MDC.put("transaction.owner", tx.getOwner()); - MDC.put("transaction.createdAt", tx.getCreatedAt()); - } - - @Override - protected void postTransfer(boolean outcome) { - // TODO Auto-generated method stub - - } - }.transfer( - tx.getAmount() - ); - } - }); + final Transaction tx = transactionFactory.newInstance(); + executor.submit(new Log4JRunnable(tx)); } + executor.shutdown(); + } } diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionContext.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/Transaction.java similarity index 76% rename from log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionContext.java rename to log-mdc-ndc/src/main/java/com/baeldung/mdc/Transaction.java index c6750edfda..ac0fe75ac5 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionContext.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/Transaction.java @@ -2,18 +2,18 @@ package com.baeldung.mdc; import java.util.Date; -public class TransactionContext { +public class Transaction { private String transactionId; private String owner; private Date createdAt; private Long amount; - public TransactionContext(String transactionId, String owner) { + public Transaction(String transactionId, String owner, long amount) { this.transactionId = transactionId; this.owner = owner; this.createdAt = new Date(); - this.amount = (long) (Math.random()*100); + this.amount = amount; } public String getOwner() { diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java index 4631c662c0..26039ba1d4 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java @@ -1,10 +1,20 @@ package com.baeldung.mdc; +import static java.lang.Math.floor; +import static java.lang.Math.random; + +import java.util.UUID; + public class TransactionFactory { - public TransactionContext buildTransaction() { - TransactionContext ctx = new TransactionContext("" + Math.random(), "owner" + Math.random()); - return ctx; + private static final String[] NAMES = {"John", "Susan", "Marc", "Samantha"}; + + public Transaction newInstance() { + String transactionId = UUID.randomUUID().toString(); + String owner = NAMES[ (int) floor(random()*NAMES.length) ]; + long amount = (long) (random()*1500 + 500); + Transaction tx = new Transaction(transactionId, owner, amount); + return tx; } } diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JBusinessService.java new file mode 100644 index 0000000000..52439a25f7 --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JBusinessService.java @@ -0,0 +1,22 @@ +package com.baeldung.mdc.log4j; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import com.baeldung.mdc.DelegateBusiness; + +final class Log4JBusinessService extends DelegateBusiness { + + private static final Logger logger = LogManager.getLogger(); + + @Override + protected void preTransfer(long amount) { + logger.info("Preparing to transfer {}$."); + } + + @Override + protected void postTransfer(long amount, boolean outcome) { + logger.info("Has transfer of {}$ completed successfully ? {}."); + } + +} \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java new file mode 100644 index 0000000000..2aeb75f95a --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java @@ -0,0 +1,25 @@ +package com.baeldung.mdc.log4j; + +import org.apache.log4j.MDC; + +import com.baeldung.mdc.Transaction; + +public class Log4JRunnable implements Runnable { + private final Transaction tx; + + public Log4JRunnable(Transaction tx) { + this.tx = tx; + } + + public void run() { + + MDC.put("transaction.id", tx.getTransactionId()); + MDC.put("transaction.owner", tx.getOwner()); + MDC.put("transaction.createdAt", tx.getCreatedAt()); + + new Log4JBusinessService().transfer(tx.getAmount()); + + MDC.clear(); + + } +} \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jBusinessService.java deleted file mode 100644 index 415e686cae..0000000000 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jBusinessService.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.mdc.log4j; - -import org.apache.log4j.Logger; - -import com.baeldung.mdc.BusinessService; - -public class Log4jBusinessService implements BusinessService { - - private final static Logger logger = Logger.getLogger(Log4jBusinessService.class); - - @Override - public boolean transfer(Long amount) { - logger.info("Executing transaction #" + amount ); - return false; - } - -} \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jDemo.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jDemo.java deleted file mode 100644 index de3945020b..0000000000 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jDemo.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.mdc.log4j; - -public class Log4jDemo { - - public static void main(String[] args) throws InterruptedException { - Log4jExecutor greeter = new Log4jExecutor(); - 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/log4j/Log4jExecutor.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jExecutor.java deleted file mode 100644 index 96fe63a3ed..0000000000 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jExecutor.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.mdc.log4j; - -import org.apache.log4j.MDC; - -import com.baeldung.mdc.TransactionContext; -import com.baeldung.mdc.TransactionFactory; - -public class Log4jExecutor implements Runnable { - - public void run() { - - TransactionContext ctx = new TransactionFactory().buildTransaction(); - - MDC.put("transaction.id", ctx.getTransactionId()); - MDC.put("transaction.owner", ctx.getOwner()); - MDC.put("transaction.createdAt", ctx.getCreatedAt()); - - new Log4jBusinessService().transfer(ctx.getAmount()); - } - -} diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2BusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2BusinessService.java new file mode 100644 index 0000000000..8e7655697e --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2BusinessService.java @@ -0,0 +1,21 @@ +package com.baeldung.mdc.log4j2; + +import org.apache.log4j.Logger; + +import com.baeldung.mdc.DelegateBusiness; + +final class Log4J2BusinessService extends DelegateBusiness { + + private Logger logger = Logger.getLogger(Log4J2BusinessService.class); + + @Override + protected void preTransfer(long amount) { + logger .info("Preparing to transfer " + amount + "$."); + } + + @Override + protected void postTransfer(long amount, boolean outcome) { + logger.info("Has transfer of " + amount + "$ completed successfully ? " + outcome + "."); + } + +} \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java new file mode 100644 index 0000000000..32f2161af8 --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java @@ -0,0 +1,25 @@ +package com.baeldung.mdc.log4j2; + +import org.apache.logging.log4j.ThreadContext; + +import com.baeldung.mdc.Transaction; + +final class Log4J2Runnable implements Runnable { + private final Transaction tx; + + Log4J2Runnable(Transaction tx) { + this.tx = tx; + } + + public void run() { + + ThreadContext.put("transaction.id", tx.getTransactionId()); + ThreadContext.put("transaction.owner", tx.getOwner()); + ThreadContext.put("transaction.createdAt", tx.getCreatedAt().toString()); + + new Log4J2BusinessService().transfer(tx.getAmount()); + + ThreadContext.clearAll(); + + } +} \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2BusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2BusinessService.java deleted file mode 100644 index d1a8dca518..0000000000 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2BusinessService.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.mdc.log4j2; - - - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import com.baeldung.mdc.BusinessService; - -public class Log4j2BusinessService implements BusinessService { - - private static final Logger logger = LogManager.getLogger(); //Log4j2BusinessService.class); - - @Override - public boolean transfer(Long amount) { - logger.info("Executing transaction #{}", amount ); - return false; - } - -} \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Demo.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Demo.java deleted file mode 100644 index 0dec8a9a18..0000000000 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Demo.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.mdc.log4j2; - -public class Log4j2Demo { - - public static void main(String[] args) throws InterruptedException { - Log4j2Executor greeter = new Log4j2Executor(); - 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/log4j2/Log4j2Executor.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Executor.java deleted file mode 100644 index a686486e8e..0000000000 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Executor.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.mdc.log4j2; - -import org.apache.logging.log4j.ThreadContext; - -import com.baeldung.mdc.TransactionContext; -import com.baeldung.mdc.TransactionFactory; - -public class Log4j2Executor implements Runnable { - - public void run() { - - TransactionContext ctx = new TransactionFactory().buildTransaction(); - - ThreadContext.put("transaction.id", ctx.getTransactionId()); - ThreadContext.put("transaction.owner", ctx.getOwner()); - ThreadContext.put("transaction.createdAt", ctx.getCreatedAt().toString()); - - new Log4j2BusinessService().transfer(ctx.getAmount()); - } - -} 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 deleted file mode 100644 index 74f96ef15e..0000000000 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackBusinessService.java +++ /dev/null @@ -1,18 +0,0 @@ -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 boolean transfer(Long amount) { - logger.info("Executing transaction #{}", amount); - return false; - } - -} \ 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 deleted file mode 100644 index 877ded8cc3..0000000000 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackDemo.java +++ /dev/null @@ -1,17 +0,0 @@ -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 deleted file mode 100644 index 614188b757..0000000000 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackExecutor.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.mdc.logback; - -import org.slf4j.MDC; - -import com.baeldung.mdc.TransactionContext; -import com.baeldung.mdc.TransactionFactory; - -public class LogbackExecutor implements Runnable { - - public void run() { - - TransactionContext ctx = new TransactionFactory().buildTransaction(); - - MDC.put("transaction.id", ctx.getTransactionId()); - MDC.put("transaction.owner", ctx.getOwner()); - MDC.put("transaction.createdAt", ctx.getCreatedAt().toString()); - - new LogbackBusinessService().transfer(ctx.getAmount()); - } - - - -} diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jBusinessService.java new file mode 100644 index 0000000000..af14abcf0f --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jBusinessService.java @@ -0,0 +1,22 @@ +package com.baeldung.mdc.slf4j; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.baeldung.mdc.DelegateBusiness; + +final class Slf4jBusinessService extends DelegateBusiness { + + private static final Logger logger = LoggerFactory.getLogger(Slf4jBusinessService.class); + + @Override + protected void preTransfer(long amount) { + logger.info("Preparing to transfer " + amount + "$."); + } + + @Override + protected void postTransfer(long amount, boolean outcome) { + logger.info("Has transfer of " + amount + "$ completed successfully ? " + outcome + "."); + } + +} \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java new file mode 100644 index 0000000000..938b18c257 --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java @@ -0,0 +1,25 @@ +package com.baeldung.mdc.slf4j; + +import org.slf4j.MDC; + +import com.baeldung.mdc.Transaction; + +final class Slf4jRunnable implements Runnable { + private final Transaction tx; + + Slf4jRunnable(Transaction tx) { + this.tx = tx; + } + + public void run() { + + MDC.put("transaction.id", tx.getTransactionId()); + MDC.put("transaction.owner", tx.getOwner()); + MDC.put("transaction.createdAt", tx.getCreatedAt().toString()); + + new Slf4jBusinessService().transfer(tx.getAmount()); + + MDC.clear(); + + } +} \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/web/Application.java b/log-mdc-ndc/src/main/java/com/baeldung/web/Application.java deleted file mode 100644 index a700e345db..0000000000 --- a/log-mdc-ndc/src/main/java/com/baeldung/web/Application.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.baeldung.web; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class Application { - - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } -} \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/web/GreetingController.java b/log-mdc-ndc/src/main/java/com/baeldung/web/GreetingController.java deleted file mode 100644 index 760e26a705..0000000000 --- a/log-mdc-ndc/src/main/java/com/baeldung/web/GreetingController.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.baeldung.web; - -import java.util.concurrent.atomic.AtomicLong; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class GreetingController { - - @GetMapping("/echo") - public String echo(@RequestBody String body) { - return body; - } -} \ No newline at end of file diff --git a/log-mdc-ndc/src/main/resources/log4j.properties b/log-mdc-ndc/src/main/resources/log4j.properties index dac8abf6b8..03ae9a0c02 100644 --- a/log-mdc-ndc/src/main/resources/log4j.properties +++ b/log-mdc-ndc/src/main/resources/log4j.properties @@ -2,11 +2,13 @@ log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout log4j.appender.file=org.apache.log4j.FileAppender -log4j.appender.file.File=/tmp/$X{transaction.owner} +log4j.appender.file.File=/tmp/baeldung.log log4j.appender.file.layout=org.apache.log4j.PatternLayout #note the %X{userName} - this is how you fetch data from Mapped Diagnostic Context (MDC) -log4j.appender.consoleAppender.layout.ConversionPattern=%-4r [%t] %5p %c %x - %m - owner=%X{transaction.owner} creation=%X{transaction.createdAt}%n -log4j.appender.file.layout.ConversionPattern=%-4r [%t] %5p %c %x - %m - owner=%X{transaction.owner} creation=%X{transaction.createdAt}%n +#log4j.appender.consoleAppender.layout.ConversionPattern=%-4r [%t] %5p %c %x - %m - owner=%X{transaction.owner} creation=%X{transaction.createdAt}%n +#log4j.appender.file.layout.ConversionPattern=%-4r [%t] %5p %c %x - %m - owner=%X{transaction.owner} creation=%X{transaction.createdAt}%n +log4j.appender.consoleAppender.layout.ConversionPattern=%-4r [%t] %5p %c %x - %m%n +log4j.appender.file.layout.ConversionPattern=%-4r [%t] %5p %c %x - %m%n -log4j.rootLogger = ALL, consoleAppender, file \ No newline at end of file +log4j.rootLogger = TRACE, consoleAppender, file \ No newline at end of file From dd1679f7b0d46519c0deb5c19134e30e02c7da34 Mon Sep 17 00:00:00 2001 From: danidemi Date: Sun, 6 Nov 2016 16:30:50 +0100 Subject: [PATCH 05/10] Removed some clutter --- .../com/baeldung/mdc/BusinessService.java | 25 +++++++++++++--- .../baeldung/mdc/DefaultBusinessService.java | 19 ------------ .../com/baeldung/mdc/DelegateBusiness.java | 29 ------------------- .../com/baeldung/mdc/IBusinessService.java | 9 ++++++ .../mdc/log4j/Log4JBusinessService.java | 8 ++--- .../mdc/log4j2/Log4J2BusinessService.java | 8 ++--- .../mdc/slf4j/Slf4jBusinessService.java | 8 ++--- 7 files changed, 42 insertions(+), 64 deletions(-) delete mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/DefaultBusinessService.java delete mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/DelegateBusiness.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/IBusinessService.java diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java index 51bebeb713..a5fa940a42 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java @@ -1,9 +1,26 @@ package com.baeldung.mdc; -public interface BusinessService { +/** + * A fake {@link IBusinessService} simulating an actual one. + */ +public abstract class BusinessService implements IBusinessService { - /** Sample service transferring a given amount of money. - * @return {@code true} when the transfer complete successfully, {@code false} otherwise. */ - boolean transfer(long amount); + public boolean transfer(long amount) { + beforeTransfer(amount); + // exchange messages with a remote system to transfer the money + try { + // let's pause randomly to properly simulate an actual system. + Thread.sleep((long) (500 + Math.random() * 500)); + } catch (InterruptedException e) { + // should never happen + } + // let's simulate both failing and successful transfers + boolean outcome = Math.random() >= 0.25; + afterTransfer(amount, outcome); + return outcome; + } + abstract protected void beforeTransfer(long amount); + + abstract protected void afterTransfer(long amount, boolean outcome); } diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/DefaultBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/DefaultBusinessService.java deleted file mode 100644 index d5bb94d21a..0000000000 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/DefaultBusinessService.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.mdc; - -/** - * A fake {@link BusinessService} simulating an actual one. - */ -public class DefaultBusinessService implements BusinessService { - - public boolean transfer(long amount) { - try { - // let's pause randomly to properly simulate an actual system. - Thread.sleep((long) (500 + Math.random()*500)); - } catch (InterruptedException e) { - // should never happen - } - // let's simulate both failing and successful transfers - return Math.random() >= 0.25; - } - -} diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/DelegateBusiness.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/DelegateBusiness.java deleted file mode 100644 index d7aab0e284..0000000000 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/DelegateBusiness.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.mdc; - -public abstract class DelegateBusiness { - - private BusinessService delegate = null; - - public DelegateBusiness(BusinessService delegate) { - super(); - this.delegate = delegate; - } - - public DelegateBusiness() { - this( new DefaultBusinessService() ); - } - - public boolean transfer(long amount) { - preTransfer(amount); - boolean outcome = delegate.transfer(amount); - postTransfer(amount, outcome); - return outcome; - } - - abstract protected void postTransfer(long amount, boolean outcome); - - abstract protected void preTransfer(long amount); - - - -} diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/IBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/IBusinessService.java new file mode 100644 index 0000000000..a2f2fbfdb3 --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/IBusinessService.java @@ -0,0 +1,9 @@ +package com.baeldung.mdc; + +public interface IBusinessService { + + /** Sample service transferring a given amount of money. + * @return {@code true} when the transfer complete successfully, {@code false} otherwise. */ + boolean transfer(long amount); + +} diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JBusinessService.java index 52439a25f7..c7d5c6904a 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JBusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JBusinessService.java @@ -3,19 +3,19 @@ package com.baeldung.mdc.log4j; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import com.baeldung.mdc.DelegateBusiness; +import com.baeldung.mdc.BusinessService; -final class Log4JBusinessService extends DelegateBusiness { +class Log4JBusinessService extends BusinessService { private static final Logger logger = LogManager.getLogger(); @Override - protected void preTransfer(long amount) { + protected void beforeTransfer(long amount) { logger.info("Preparing to transfer {}$."); } @Override - protected void postTransfer(long amount, boolean outcome) { + protected void afterTransfer(long amount, boolean outcome) { logger.info("Has transfer of {}$ completed successfully ? {}."); } diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2BusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2BusinessService.java index 8e7655697e..1000b7edc2 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2BusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2BusinessService.java @@ -2,19 +2,19 @@ package com.baeldung.mdc.log4j2; import org.apache.log4j.Logger; -import com.baeldung.mdc.DelegateBusiness; +import com.baeldung.mdc.BusinessService; -final class Log4J2BusinessService extends DelegateBusiness { +final class Log4J2BusinessService extends BusinessService { private Logger logger = Logger.getLogger(Log4J2BusinessService.class); @Override - protected void preTransfer(long amount) { + protected void beforeTransfer(long amount) { logger .info("Preparing to transfer " + amount + "$."); } @Override - protected void postTransfer(long amount, boolean outcome) { + protected void afterTransfer(long amount, boolean outcome) { logger.info("Has transfer of " + amount + "$ completed successfully ? " + outcome + "."); } diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jBusinessService.java index af14abcf0f..b241ec4b77 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jBusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jBusinessService.java @@ -3,19 +3,19 @@ package com.baeldung.mdc.slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.baeldung.mdc.DelegateBusiness; +import com.baeldung.mdc.BusinessService; -final class Slf4jBusinessService extends DelegateBusiness { +final class Slf4jBusinessService extends BusinessService { private static final Logger logger = LoggerFactory.getLogger(Slf4jBusinessService.class); @Override - protected void preTransfer(long amount) { + protected void beforeTransfer(long amount) { logger.info("Preparing to transfer " + amount + "$."); } @Override - protected void postTransfer(long amount, boolean outcome) { + protected void afterTransfer(long amount, boolean outcome) { logger.info("Has transfer of " + amount + "$ completed successfully ? " + outcome + "."); } From d0e95dfbdd1bb0a927fbe41436ffbdb896bc0495 Mon Sep 17 00:00:00 2001 From: danidemi Date: Sun, 6 Nov 2016 17:39:06 +0100 Subject: [PATCH 06/10] Applied formatting as defined in the rules. --- log-mdc-ndc/pom.xml | 80 +++++++++---------- .../java/com/baeldung/mdc/GenericApp.java | 35 ++++---- .../com/baeldung/mdc/TransactionFactory.java | 3 +- .../mdc/log4j/Log4JBusinessService.java | 27 +++---- .../com/baeldung/mdc/log4j/Log4JRunnable.java | 26 +++--- .../mdc/log4j2/Log4J2BusinessService.java | 27 ++++--- .../baeldung/mdc/log4j2/Log4J2Runnable.java | 34 ++++---- .../mdc/slf4j/Slf4jBusinessService.java | 22 ++--- .../com/baeldung/mdc/slf4j/Slf4jRunnable.java | 32 ++++---- .../src/main/resources/log4j.properties | 12 +-- log-mdc-ndc/src/main/resources/log4j2.xml | 39 +++------ log-mdc-ndc/src/main/resources/logback.xml | 3 +- 12 files changed, 166 insertions(+), 174 deletions(-) diff --git a/log-mdc-ndc/pom.xml b/log-mdc-ndc/pom.xml index 8e9d52a750..927473ad4e 100644 --- a/log-mdc-ndc/pom.xml +++ b/log-mdc-ndc/pom.xml @@ -6,53 +6,53 @@ 0.0.1-SNAPSHOT logmdcndc tutorial on logging with MDC and NDC - + - - - org.springframework - spring-context - 4.3.3.RELEASE - - - org.springframework - spring-webmvc - 4.3.3.RELEASE - - + + + org.springframework + spring-context + 4.3.3.RELEASE + + + org.springframework + spring-webmvc + 4.3.3.RELEASE + + log4j log4j 1.2.17 - - - - org.apache.logging.log4j - log4j-api - 2.7 - - - org.apache.logging.log4j - log4j-core - 2.7 - - - - - com.lmax - disruptor - 3.3.4 - - - - - ch.qos.logback - logback-classic - 1.1.7 - - + + + + org.apache.logging.log4j + log4j-api + 2.7 + + + org.apache.logging.log4j + log4j-core + 2.7 + + + + + com.lmax + disruptor + 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/GenericApp.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/GenericApp.java index 3652032e0e..9f08e33455 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/GenericApp.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/GenericApp.java @@ -6,20 +6,27 @@ import java.util.concurrent.Executors; import org.apache.log4j.Logger; import com.baeldung.mdc.log4j.Log4JRunnable; +import com.baeldung.mdc.log4j2.Log4J2Runnable; +import com.baeldung.mdc.slf4j.Slf4jRunnable; public class GenericApp { - - public static void main(String[] args) { - - ExecutorService executor = Executors.newFixedThreadPool(3); - TransactionFactory transactionFactory = new TransactionFactory(); - - for(int i=0; i<10; i++){ - final Transaction tx = transactionFactory.newInstance(); - executor.submit(new Log4JRunnable(tx)); - } - - executor.shutdown(); - - } + + public static void main(String[] args) { + + ExecutorService executor = Executors.newFixedThreadPool(3); + TransactionFactory transactionFactory = new TransactionFactory(); + + for (int i = 0; i < 10; i++) { + final Transaction tx = transactionFactory.newInstance(); + + //Runnable task = new Log4JRunnable(tx); + //Runnable task = new Log4J2Runnable(tx); + Runnable task = new Slf4jRunnable(tx); + + executor.submit(task); + } + + executor.shutdown(); + + } } diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java index 26039ba1d4..2ae0b3d3f5 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java @@ -8,9 +8,10 @@ import java.util.UUID; public class TransactionFactory { private static final String[] NAMES = {"John", "Susan", "Marc", "Samantha"}; + private static long nextId = 1; public Transaction newInstance() { - String transactionId = UUID.randomUUID().toString(); + String transactionId = String.valueOf( nextId++ ); String owner = NAMES[ (int) floor(random()*NAMES.length) ]; long amount = (long) (random()*1500 + 500); Transaction tx = new Transaction(transactionId, owner, amount); diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JBusinessService.java index c7d5c6904a..26633e229e 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JBusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JBusinessService.java @@ -1,22 +1,21 @@ package com.baeldung.mdc.log4j; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.apache.log4j.Logger; import com.baeldung.mdc.BusinessService; -class Log4JBusinessService extends BusinessService { - - private static final Logger logger = LogManager.getLogger(); +public class Log4JBusinessService extends BusinessService { - @Override - protected void beforeTransfer(long amount) { - logger.info("Preparing to transfer {}$."); - } + private Logger logger = Logger.getLogger(Log4JBusinessService.class); + + @Override + protected void beforeTransfer(long amount) { + logger.info("Preparing to transfer " + amount + "$."); + } + + @Override + protected void afterTransfer(long amount, boolean outcome) { + logger.info("Has transfer of " + amount + "$ completed successfully ? " + outcome + "."); + } - @Override - protected void afterTransfer(long amount, boolean outcome) { - logger.info("Has transfer of {}$ completed successfully ? {}."); - } - } \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java index 2aeb75f95a..ce5d97eddf 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java @@ -1,25 +1,29 @@ package com.baeldung.mdc.log4j; import org.apache.log4j.MDC; +import org.apache.logging.log4j.ThreadContext; import com.baeldung.mdc.Transaction; +import com.baeldung.mdc.log4j2.Log4J2BusinessService; public class Log4JRunnable implements Runnable { - private final Transaction tx; - public Log4JRunnable(Transaction tx) { - this.tx = tx; - } + private Transaction tx; + private static Log4JBusinessService log4jBusinessService = new Log4JBusinessService(); - public void run() { + public Log4JRunnable(Transaction tx) { + this.tx = tx; + } - MDC.put("transaction.id", tx.getTransactionId()); - MDC.put("transaction.owner", tx.getOwner()); - MDC.put("transaction.createdAt", tx.getCreatedAt()); + public void run() { - new Log4JBusinessService().transfer(tx.getAmount()); + MDC.put("transaction.id", tx.getTransactionId()); + MDC.put("transaction.owner", tx.getOwner()); + MDC.put("transaction.createdAt", tx.getCreatedAt()); - MDC.clear(); + log4jBusinessService.transfer(tx.getAmount()); - } + MDC.clear(); + + } } \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2BusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2BusinessService.java index 1000b7edc2..cbfbc5b8a5 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2BusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2BusinessService.java @@ -1,21 +1,22 @@ package com.baeldung.mdc.log4j2; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import com.baeldung.mdc.BusinessService; -final class Log4J2BusinessService extends BusinessService { - - private Logger logger = Logger.getLogger(Log4J2BusinessService.class); +public class Log4J2BusinessService extends BusinessService { - @Override - protected void beforeTransfer(long amount) { - logger .info("Preparing to transfer " + amount + "$."); - } + private static final Logger logger = LogManager.getLogger(); + + @Override + protected void beforeTransfer(long amount) { + logger.info("Preparing to transfer {}$.", amount); + } + + @Override + protected void afterTransfer(long amount, boolean outcome) { + logger.info("Has transfer of {}$ completed successfully ? {}.", amount, outcome); + } - @Override - protected void afterTransfer(long amount, boolean outcome) { - logger.info("Has transfer of " + amount + "$ completed successfully ? " + outcome + "."); - } - } \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java index 32f2161af8..6a3f39ba5b 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java @@ -4,22 +4,24 @@ import org.apache.logging.log4j.ThreadContext; import com.baeldung.mdc.Transaction; -final class Log4J2Runnable implements Runnable { - private final Transaction tx; +public class Log4J2Runnable implements Runnable { + private final Transaction tx; + private Log4J2BusinessService log4j2BusinessService = new Log4J2BusinessService(); - Log4J2Runnable(Transaction tx) { - this.tx = tx; - } + public Log4J2Runnable(Transaction tx) { + this.tx = tx; + } - public void run() { - - ThreadContext.put("transaction.id", tx.getTransactionId()); - ThreadContext.put("transaction.owner", tx.getOwner()); - ThreadContext.put("transaction.createdAt", tx.getCreatedAt().toString()); - - new Log4J2BusinessService().transfer(tx.getAmount()); - - ThreadContext.clearAll(); - - } + public void run() { + + ThreadContext.put("transaction.id", tx.getTransactionId()); + ThreadContext.put("transaction.id", tx.getTransactionId()); + ThreadContext.put("transaction.owner", tx.getOwner()); + ThreadContext.put("transaction.createdAt", tx.getCreatedAt().toString()); + + log4j2BusinessService.transfer(tx.getAmount()); + + ThreadContext.clearAll(); + + } } \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jBusinessService.java index b241ec4b77..d525965ed5 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jBusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jBusinessService.java @@ -6,17 +6,17 @@ import org.slf4j.LoggerFactory; import com.baeldung.mdc.BusinessService; final class Slf4jBusinessService extends BusinessService { - - private static final Logger logger = LoggerFactory.getLogger(Slf4jBusinessService.class); - @Override - protected void beforeTransfer(long amount) { - logger.info("Preparing to transfer " + amount + "$."); - } + private static final Logger logger = LoggerFactory.getLogger(Slf4jBusinessService.class); + + @Override + protected void beforeTransfer(long amount) { + logger.info("Preparing to transfer {}$.", amount); + } + + @Override + protected void afterTransfer(long amount, boolean outcome) { + logger.info("Has transfer of {}$ completed successfully ? {}.", amount, outcome); + } - @Override - protected void afterTransfer(long amount, boolean outcome) { - logger.info("Has transfer of " + amount + "$ completed successfully ? " + outcome + "."); - } - } \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java index 938b18c257..2ef170721c 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java @@ -4,22 +4,22 @@ import org.slf4j.MDC; import com.baeldung.mdc.Transaction; -final class Slf4jRunnable implements Runnable { - private final Transaction tx; +public class Slf4jRunnable implements Runnable { + private final Transaction tx; - Slf4jRunnable(Transaction tx) { - this.tx = tx; - } + public Slf4jRunnable(Transaction tx) { + this.tx = tx; + } - public void run() { - - MDC.put("transaction.id", tx.getTransactionId()); - MDC.put("transaction.owner", tx.getOwner()); - MDC.put("transaction.createdAt", tx.getCreatedAt().toString()); - - new Slf4jBusinessService().transfer(tx.getAmount()); - - MDC.clear(); - - } + public void run() { + + MDC.put("transaction.id", tx.getTransactionId()); + MDC.put("transaction.owner", tx.getOwner()); + MDC.put("transaction.createdAt", tx.getCreatedAt().toString()); + + new Slf4jBusinessService().transfer(tx.getAmount()); + + MDC.clear(); + + } } \ No newline at end of file diff --git a/log-mdc-ndc/src/main/resources/log4j.properties b/log-mdc-ndc/src/main/resources/log4j.properties index 03ae9a0c02..39be027f3f 100644 --- a/log-mdc-ndc/src/main/resources/log4j.properties +++ b/log-mdc-ndc/src/main/resources/log4j.properties @@ -1,14 +1,8 @@ log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout -log4j.appender.file=org.apache.log4j.FileAppender -log4j.appender.file.File=/tmp/baeldung.log -log4j.appender.file.layout=org.apache.log4j.PatternLayout - #note the %X{userName} - this is how you fetch data from Mapped Diagnostic Context (MDC) -#log4j.appender.consoleAppender.layout.ConversionPattern=%-4r [%t] %5p %c %x - %m - owner=%X{transaction.owner} creation=%X{transaction.createdAt}%n -#log4j.appender.file.layout.ConversionPattern=%-4r [%t] %5p %c %x - %m - owner=%X{transaction.owner} creation=%X{transaction.createdAt}%n -log4j.appender.consoleAppender.layout.ConversionPattern=%-4r [%t] %5p %c %x - %m%n -log4j.appender.file.layout.ConversionPattern=%-4r [%t] %5p %c %x - %m%n +#log4j.appender.consoleAppender.layout.ConversionPattern=%-4r [%t] %5p %c{1} %x - %m%n +log4j.appender.consoleAppender.layout.ConversionPattern=%-4r [%t] %5p %c{1} %x - %m - tx.id=%X{transaction.id} tx.owner=%X{transaction.owner}%n -log4j.rootLogger = TRACE, consoleAppender, file \ No newline at end of file +log4j.rootLogger = TRACE, consoleAppender \ No newline at end of file diff --git a/log-mdc-ndc/src/main/resources/log4j2.xml b/log-mdc-ndc/src/main/resources/log4j2.xml index f9a2bdcf1a..800cfacafe 100644 --- a/log-mdc-ndc/src/main/resources/log4j2.xml +++ b/log-mdc-ndc/src/main/resources/log4j2.xml @@ -1,32 +1,17 @@ - - # Console appender - - # Pattern of log message for console appender - - + + + + + - # File appender - - + + - - # Override log level for specified package - - - - - - - + + + + \ No newline at end of file diff --git a/log-mdc-ndc/src/main/resources/logback.xml b/log-mdc-ndc/src/main/resources/logback.xml index 7bea9976e7..44d247c87e 100644 --- a/log-mdc-ndc/src/main/resources/logback.xml +++ b/log-mdc-ndc/src/main/resources/logback.xml @@ -1,9 +1,8 @@ - # Console appender - logback %d{yyyy-MM-dd HH:mm:ss} %p %m %X{transaction.owner} %X{transaction.createdAt} %n + %-4r [%t] %5p %c{1} - %m - tx.id=%X{transaction.id} tx.owner=%X{transaction.owner}%n From 9ae723f5ab04cfe9826c6524e10a0d4165c4b409 Mon Sep 17 00:00:00 2001 From: danidemi Date: Mon, 7 Nov 2016 21:21:28 +0100 Subject: [PATCH 07/10] Removed redundant interface --- .../com/baeldung/mdc/IBusinessService.java | 9 ---- .../java/com/baeldung/mdc/Transaction.java | 50 ++++++++----------- ...inessService.java => TransferService.java} | 4 +- .../mdc/log4j/Log4JBusinessService.java | 4 +- .../com/baeldung/mdc/log4j/Log4JRunnable.java | 3 -- .../mdc/log4j2/Log4J2BusinessService.java | 4 +- .../baeldung/mdc/log4j2/Log4J2Runnable.java | 2 - .../mdc/slf4j/Slf4jBusinessService.java | 4 +- .../com/baeldung/mdc/slf4j/Slf4jRunnable.java | 1 - 9 files changed, 30 insertions(+), 51 deletions(-) delete mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/IBusinessService.java rename log-mdc-ndc/src/main/java/com/baeldung/mdc/{BusinessService.java => TransferService.java} (80%) diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/IBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/IBusinessService.java deleted file mode 100644 index a2f2fbfdb3..0000000000 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/IBusinessService.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.mdc; - -public interface IBusinessService { - - /** Sample service transferring a given amount of money. - * @return {@code true} when the transfer complete successfully, {@code false} otherwise. */ - boolean transfer(long amount); - -} diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/Transaction.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/Transaction.java index ac0fe75ac5..1a038512c0 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/Transaction.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/Transaction.java @@ -1,35 +1,27 @@ package com.baeldung.mdc; -import java.util.Date; - public class Transaction { - - private String transactionId; - private String owner; - private Date createdAt; - private Long amount; - - public Transaction(String transactionId, String owner, long amount) { - this.transactionId = transactionId; - this.owner = owner; - this.createdAt = new Date(); - this.amount = amount; - } - public String getOwner() { - return owner; - } + private String transactionId; + private String owner; + private Long amount; + + public Transaction(String transactionId, String owner, long amount) { + this.transactionId = transactionId; + this.owner = owner; + this.amount = amount; + } + + public String getOwner() { + return owner; + } + + public String getTransactionId() { + return transactionId; + } + + public Long getAmount() { + return amount; + } - public String getTransactionId() { - return transactionId; - } - - public Date getCreatedAt() { - return createdAt; - } - - public Long getAmount() { - return amount; - } - } diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransferService.java similarity index 80% rename from log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java rename to log-mdc-ndc/src/main/java/com/baeldung/mdc/TransferService.java index a5fa940a42..7ab53043d3 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransferService.java @@ -3,8 +3,10 @@ package com.baeldung.mdc; /** * A fake {@link IBusinessService} simulating an actual one. */ -public abstract class BusinessService implements IBusinessService { +public abstract class TransferService { + /** Sample service transferring a given amount of money. + * @return {@code true} when the transfer complete successfully, {@code false} otherwise. */ public boolean transfer(long amount) { beforeTransfer(amount); // exchange messages with a remote system to transfer the money diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JBusinessService.java index 26633e229e..d8b3a15cc2 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JBusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JBusinessService.java @@ -2,9 +2,9 @@ package com.baeldung.mdc.log4j; import org.apache.log4j.Logger; -import com.baeldung.mdc.BusinessService; +import com.baeldung.mdc.TransferService; -public class Log4JBusinessService extends BusinessService { +public class Log4JBusinessService extends TransferService { private Logger logger = Logger.getLogger(Log4JBusinessService.class); diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java index ce5d97eddf..817eee1e3c 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java @@ -1,10 +1,8 @@ package com.baeldung.mdc.log4j; import org.apache.log4j.MDC; -import org.apache.logging.log4j.ThreadContext; import com.baeldung.mdc.Transaction; -import com.baeldung.mdc.log4j2.Log4J2BusinessService; public class Log4JRunnable implements Runnable { @@ -19,7 +17,6 @@ public class Log4JRunnable implements Runnable { MDC.put("transaction.id", tx.getTransactionId()); MDC.put("transaction.owner", tx.getOwner()); - MDC.put("transaction.createdAt", tx.getCreatedAt()); log4jBusinessService.transfer(tx.getAmount()); diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2BusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2BusinessService.java index cbfbc5b8a5..66a554ff65 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2BusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2BusinessService.java @@ -3,9 +3,9 @@ package com.baeldung.mdc.log4j2; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import com.baeldung.mdc.BusinessService; +import com.baeldung.mdc.TransferService; -public class Log4J2BusinessService extends BusinessService { +public class Log4J2BusinessService extends TransferService { private static final Logger logger = LogManager.getLogger(); diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java index 6a3f39ba5b..d76f1346e2 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java @@ -14,10 +14,8 @@ public class Log4J2Runnable implements Runnable { public void run() { - ThreadContext.put("transaction.id", tx.getTransactionId()); ThreadContext.put("transaction.id", tx.getTransactionId()); ThreadContext.put("transaction.owner", tx.getOwner()); - ThreadContext.put("transaction.createdAt", tx.getCreatedAt().toString()); log4j2BusinessService.transfer(tx.getAmount()); diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jBusinessService.java index d525965ed5..6cd2df6b46 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jBusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jBusinessService.java @@ -3,9 +3,9 @@ package com.baeldung.mdc.slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.baeldung.mdc.BusinessService; +import com.baeldung.mdc.TransferService; -final class Slf4jBusinessService extends BusinessService { +final class Slf4jBusinessService extends TransferService { private static final Logger logger = LoggerFactory.getLogger(Slf4jBusinessService.class); diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java index 2ef170721c..61bf2c0743 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java @@ -15,7 +15,6 @@ public class Slf4jRunnable implements Runnable { MDC.put("transaction.id", tx.getTransactionId()); MDC.put("transaction.owner", tx.getOwner()); - MDC.put("transaction.createdAt", tx.getCreatedAt().toString()); new Slf4jBusinessService().transfer(tx.getAmount()); From b27a8cff06b5298022f5b3a991ec87d78af1594a Mon Sep 17 00:00:00 2001 From: danidemi Date: Mon, 7 Nov 2016 22:44:09 +0100 Subject: [PATCH 08/10] Renaming following article review --- .../java/com/baeldung/mdc/TransactionFactory.java | 4 ++-- .../baeldung/mdc/{Transaction.java => Transfer.java} | 12 ++++++------ .../mdc/{GenericApp.java => TransferDemo.java} | 4 ++-- .../main/java/com/baeldung/mdc/TransferService.java | 2 +- .../java/com/baeldung/mdc/log4j/Log4JRunnable.java | 10 +++++----- ...usinessService.java => Log4JTransferService.java} | 4 ++-- .../java/com/baeldung/mdc/log4j2/Log4J2Runnable.java | 10 +++++----- ...sinessService.java => Log4J2TransferService.java} | 2 +- ...BusinessService.java => Slf4TransferService.java} | 4 ++-- .../java/com/baeldung/mdc/slf4j/Slf4jRunnable.java | 10 +++++----- 10 files changed, 31 insertions(+), 31 deletions(-) rename log-mdc-ndc/src/main/java/com/baeldung/mdc/{Transaction.java => Transfer.java} (60%) rename log-mdc-ndc/src/main/java/com/baeldung/mdc/{GenericApp.java => TransferDemo.java} (89%) rename log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/{Log4JBusinessService.java => Log4JTransferService.java} (77%) rename log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/{Log4J2BusinessService.java => Log4J2TransferService.java} (89%) rename log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/{Slf4jBusinessService.java => Slf4TransferService.java} (85%) diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java index 2ae0b3d3f5..ec1887eea6 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java @@ -10,11 +10,11 @@ public class TransactionFactory { private static final String[] NAMES = {"John", "Susan", "Marc", "Samantha"}; private static long nextId = 1; - public Transaction newInstance() { + public Transfer newInstance() { String transactionId = String.valueOf( nextId++ ); String owner = NAMES[ (int) floor(random()*NAMES.length) ]; long amount = (long) (random()*1500 + 500); - Transaction tx = new Transaction(transactionId, owner, amount); + Transfer tx = new Transfer(transactionId, owner, amount); return tx; } diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/Transaction.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/Transfer.java similarity index 60% rename from log-mdc-ndc/src/main/java/com/baeldung/mdc/Transaction.java rename to log-mdc-ndc/src/main/java/com/baeldung/mdc/Transfer.java index 1a038512c0..1a27fe4eec 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/Transaction.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/Transfer.java @@ -1,19 +1,19 @@ package com.baeldung.mdc; -public class Transaction { +public class Transfer { private String transactionId; - private String owner; + private String sender; private Long amount; - public Transaction(String transactionId, String owner, long amount) { + public Transfer(String transactionId, String sender, long amount) { this.transactionId = transactionId; - this.owner = owner; + this.sender = sender; this.amount = amount; } - public String getOwner() { - return owner; + public String getSender() { + return sender; } public String getTransactionId() { diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/GenericApp.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransferDemo.java similarity index 89% rename from log-mdc-ndc/src/main/java/com/baeldung/mdc/GenericApp.java rename to log-mdc-ndc/src/main/java/com/baeldung/mdc/TransferDemo.java index 9f08e33455..daf256007c 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/GenericApp.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransferDemo.java @@ -9,7 +9,7 @@ import com.baeldung.mdc.log4j.Log4JRunnable; import com.baeldung.mdc.log4j2.Log4J2Runnable; import com.baeldung.mdc.slf4j.Slf4jRunnable; -public class GenericApp { +public class TransferDemo { public static void main(String[] args) { @@ -17,7 +17,7 @@ public class GenericApp { TransactionFactory transactionFactory = new TransactionFactory(); for (int i = 0; i < 10; i++) { - final Transaction tx = transactionFactory.newInstance(); + Transfer tx = transactionFactory.newInstance(); //Runnable task = new Log4JRunnable(tx); //Runnable task = new Log4J2Runnable(tx); diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransferService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransferService.java index 7ab53043d3..95e7d38630 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransferService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransferService.java @@ -1,7 +1,7 @@ package com.baeldung.mdc; /** - * A fake {@link IBusinessService} simulating an actual one. + * A fake transfer service simulating an actual one. */ public abstract class TransferService { diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java index 817eee1e3c..7711795a59 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java @@ -2,21 +2,21 @@ package com.baeldung.mdc.log4j; import org.apache.log4j.MDC; -import com.baeldung.mdc.Transaction; +import com.baeldung.mdc.Transfer; public class Log4JRunnable implements Runnable { - private Transaction tx; - private static Log4JBusinessService log4jBusinessService = new Log4JBusinessService(); + private Transfer tx; + private static Log4JTransferService log4jBusinessService = new Log4JTransferService(); - public Log4JRunnable(Transaction tx) { + public Log4JRunnable(Transfer tx) { this.tx = tx; } public void run() { MDC.put("transaction.id", tx.getTransactionId()); - MDC.put("transaction.owner", tx.getOwner()); + MDC.put("transaction.owner", tx.getSender()); log4jBusinessService.transfer(tx.getAmount()); diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JTransferService.java similarity index 77% rename from log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JBusinessService.java rename to log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JTransferService.java index d8b3a15cc2..a8bfe7957a 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JBusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JTransferService.java @@ -4,9 +4,9 @@ import org.apache.log4j.Logger; import com.baeldung.mdc.TransferService; -public class Log4JBusinessService extends TransferService { +public class Log4JTransferService extends TransferService { - private Logger logger = Logger.getLogger(Log4JBusinessService.class); + private Logger logger = Logger.getLogger(Log4JTransferService.class); @Override protected void beforeTransfer(long amount) { diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java index d76f1346e2..0b7f516bd1 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java @@ -2,20 +2,20 @@ package com.baeldung.mdc.log4j2; import org.apache.logging.log4j.ThreadContext; -import com.baeldung.mdc.Transaction; +import com.baeldung.mdc.Transfer; public class Log4J2Runnable implements Runnable { - private final Transaction tx; - private Log4J2BusinessService log4j2BusinessService = new Log4J2BusinessService(); + private final Transfer tx; + private Log4J2TransferService log4j2BusinessService = new Log4J2TransferService(); - public Log4J2Runnable(Transaction tx) { + public Log4J2Runnable(Transfer tx) { this.tx = tx; } public void run() { ThreadContext.put("transaction.id", tx.getTransactionId()); - ThreadContext.put("transaction.owner", tx.getOwner()); + ThreadContext.put("transaction.owner", tx.getSender()); log4j2BusinessService.transfer(tx.getAmount()); diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2BusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2TransferService.java similarity index 89% rename from log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2BusinessService.java rename to log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2TransferService.java index 66a554ff65..fb6eeccc9e 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2BusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2TransferService.java @@ -5,7 +5,7 @@ import org.apache.logging.log4j.Logger; import com.baeldung.mdc.TransferService; -public class Log4J2BusinessService extends TransferService { +public class Log4J2TransferService extends TransferService { private static final Logger logger = LogManager.getLogger(); diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4TransferService.java similarity index 85% rename from log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jBusinessService.java rename to log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4TransferService.java index 6cd2df6b46..f4df150a6a 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jBusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4TransferService.java @@ -5,9 +5,9 @@ import org.slf4j.LoggerFactory; import com.baeldung.mdc.TransferService; -final class Slf4jBusinessService extends TransferService { +final class Slf4TransferService extends TransferService { - private static final Logger logger = LoggerFactory.getLogger(Slf4jBusinessService.class); + private static final Logger logger = LoggerFactory.getLogger(Slf4TransferService.class); @Override protected void beforeTransfer(long amount) { diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java index 61bf2c0743..e30a28a3c7 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java @@ -2,21 +2,21 @@ package com.baeldung.mdc.slf4j; import org.slf4j.MDC; -import com.baeldung.mdc.Transaction; +import com.baeldung.mdc.Transfer; public class Slf4jRunnable implements Runnable { - private final Transaction tx; + private final Transfer tx; - public Slf4jRunnable(Transaction tx) { + public Slf4jRunnable(Transfer tx) { this.tx = tx; } public void run() { MDC.put("transaction.id", tx.getTransactionId()); - MDC.put("transaction.owner", tx.getOwner()); + MDC.put("transaction.owner", tx.getSender()); - new Slf4jBusinessService().transfer(tx.getAmount()); + new Slf4TransferService().transfer(tx.getAmount()); MDC.clear(); From 4e3f9fa626839f6b1f50abc9c147cad0a1e84757 Mon Sep 17 00:00:00 2001 From: danidemi Date: Mon, 7 Nov 2016 23:06:36 +0100 Subject: [PATCH 09/10] Demos are run with JUnit --- {log-mdc-ndc => log-mdc}/README.md | 0 {log-mdc-ndc => log-mdc}/pom.xml | 8 +++++ .../com/baeldung/mdc/TransactionFactory.java | 0 .../main/java/com/baeldung/mdc/Transfer.java | 0 .../java/com/baeldung/mdc/TransferDemo.java | 0 .../com/baeldung/mdc/TransferService.java | 0 .../com/baeldung/mdc/log4j/Log4JRunnable.java | 0 .../mdc/log4j/Log4JTransferService.java | 0 .../baeldung/mdc/log4j2/Log4J2Runnable.java | 0 .../mdc/log4j2/Log4J2TransferService.java | 0 .../mdc/slf4j/Slf4TransferService.java | 0 .../com/baeldung/mdc/slf4j/Slf4jRunnable.java | 0 .../src/main/resources/log4j.properties | 0 .../src/main/resources/log4j2.xml | 0 .../src/main/resources/logback.xml | 0 .../java/com/baeldung/mdc/log4j/Demo.java | 26 ++++++++++++++++ .../java/com/baeldung/mdc/log4j2/Demo.java | 30 +++++++++++++++++++ .../java/com/baeldung/mdc/slf4j/Demo.java | 30 +++++++++++++++++++ 18 files changed, 94 insertions(+) rename {log-mdc-ndc => log-mdc}/README.md (100%) rename {log-mdc-ndc => log-mdc}/pom.xml (90%) rename {log-mdc-ndc => log-mdc}/src/main/java/com/baeldung/mdc/TransactionFactory.java (100%) rename {log-mdc-ndc => log-mdc}/src/main/java/com/baeldung/mdc/Transfer.java (100%) rename {log-mdc-ndc => log-mdc}/src/main/java/com/baeldung/mdc/TransferDemo.java (100%) rename {log-mdc-ndc => log-mdc}/src/main/java/com/baeldung/mdc/TransferService.java (100%) rename {log-mdc-ndc => log-mdc}/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java (100%) rename {log-mdc-ndc => log-mdc}/src/main/java/com/baeldung/mdc/log4j/Log4JTransferService.java (100%) rename {log-mdc-ndc => log-mdc}/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java (100%) rename {log-mdc-ndc => log-mdc}/src/main/java/com/baeldung/mdc/log4j2/Log4J2TransferService.java (100%) rename {log-mdc-ndc => log-mdc}/src/main/java/com/baeldung/mdc/slf4j/Slf4TransferService.java (100%) rename {log-mdc-ndc => log-mdc}/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java (100%) rename {log-mdc-ndc => log-mdc}/src/main/resources/log4j.properties (100%) rename {log-mdc-ndc => log-mdc}/src/main/resources/log4j2.xml (100%) rename {log-mdc-ndc => log-mdc}/src/main/resources/logback.xml (100%) create mode 100644 log-mdc/src/test/java/com/baeldung/mdc/log4j/Demo.java create mode 100644 log-mdc/src/test/java/com/baeldung/mdc/log4j2/Demo.java create mode 100644 log-mdc/src/test/java/com/baeldung/mdc/slf4j/Demo.java diff --git a/log-mdc-ndc/README.md b/log-mdc/README.md similarity index 100% rename from log-mdc-ndc/README.md rename to log-mdc/README.md diff --git a/log-mdc-ndc/pom.xml b/log-mdc/pom.xml similarity index 90% rename from log-mdc-ndc/pom.xml rename to log-mdc/pom.xml index 927473ad4e..a6e9414dc5 100644 --- a/log-mdc-ndc/pom.xml +++ b/log-mdc/pom.xml @@ -52,6 +52,14 @@ logback-classic 1.1.7 + + + junit + junit + 4.12 + test + + diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java b/log-mdc/src/main/java/com/baeldung/mdc/TransactionFactory.java similarity index 100% rename from log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java rename to log-mdc/src/main/java/com/baeldung/mdc/TransactionFactory.java diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/Transfer.java b/log-mdc/src/main/java/com/baeldung/mdc/Transfer.java similarity index 100% rename from log-mdc-ndc/src/main/java/com/baeldung/mdc/Transfer.java rename to log-mdc/src/main/java/com/baeldung/mdc/Transfer.java diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransferDemo.java b/log-mdc/src/main/java/com/baeldung/mdc/TransferDemo.java similarity index 100% rename from log-mdc-ndc/src/main/java/com/baeldung/mdc/TransferDemo.java rename to log-mdc/src/main/java/com/baeldung/mdc/TransferDemo.java diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransferService.java b/log-mdc/src/main/java/com/baeldung/mdc/TransferService.java similarity index 100% rename from log-mdc-ndc/src/main/java/com/baeldung/mdc/TransferService.java rename to log-mdc/src/main/java/com/baeldung/mdc/TransferService.java diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java b/log-mdc/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java similarity index 100% rename from log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java rename to log-mdc/src/main/java/com/baeldung/mdc/log4j/Log4JRunnable.java diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JTransferService.java b/log-mdc/src/main/java/com/baeldung/mdc/log4j/Log4JTransferService.java similarity index 100% rename from log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4JTransferService.java rename to log-mdc/src/main/java/com/baeldung/mdc/log4j/Log4JTransferService.java diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java b/log-mdc/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java similarity index 100% rename from log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java rename to log-mdc/src/main/java/com/baeldung/mdc/log4j2/Log4J2Runnable.java diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2TransferService.java b/log-mdc/src/main/java/com/baeldung/mdc/log4j2/Log4J2TransferService.java similarity index 100% rename from log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4J2TransferService.java rename to log-mdc/src/main/java/com/baeldung/mdc/log4j2/Log4J2TransferService.java diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4TransferService.java b/log-mdc/src/main/java/com/baeldung/mdc/slf4j/Slf4TransferService.java similarity index 100% rename from log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4TransferService.java rename to log-mdc/src/main/java/com/baeldung/mdc/slf4j/Slf4TransferService.java diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java b/log-mdc/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java similarity index 100% rename from log-mdc-ndc/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java rename to log-mdc/src/main/java/com/baeldung/mdc/slf4j/Slf4jRunnable.java diff --git a/log-mdc-ndc/src/main/resources/log4j.properties b/log-mdc/src/main/resources/log4j.properties similarity index 100% rename from log-mdc-ndc/src/main/resources/log4j.properties rename to log-mdc/src/main/resources/log4j.properties diff --git a/log-mdc-ndc/src/main/resources/log4j2.xml b/log-mdc/src/main/resources/log4j2.xml similarity index 100% rename from log-mdc-ndc/src/main/resources/log4j2.xml rename to log-mdc/src/main/resources/log4j2.xml diff --git a/log-mdc-ndc/src/main/resources/logback.xml b/log-mdc/src/main/resources/logback.xml similarity index 100% rename from log-mdc-ndc/src/main/resources/logback.xml rename to log-mdc/src/main/resources/logback.xml diff --git a/log-mdc/src/test/java/com/baeldung/mdc/log4j/Demo.java b/log-mdc/src/test/java/com/baeldung/mdc/log4j/Demo.java new file mode 100644 index 0000000000..f9a210606f --- /dev/null +++ b/log-mdc/src/test/java/com/baeldung/mdc/log4j/Demo.java @@ -0,0 +1,26 @@ +package com.baeldung.mdc.log4j; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +import org.junit.Test; + +import com.baeldung.mdc.TransactionFactory; +import com.baeldung.mdc.Transfer; + +public class Demo { + + @Test + public void main() throws InterruptedException { + ExecutorService executor = Executors.newFixedThreadPool(3); + TransactionFactory transactionFactory = new TransactionFactory(); + for (int i = 0; i < 10; i++) { + Transfer tx = transactionFactory.newInstance(); + Runnable task = new Log4JRunnable(tx); + executor.submit(task); + } + executor.shutdown(); + executor.awaitTermination(60, TimeUnit.SECONDS); + } +} diff --git a/log-mdc/src/test/java/com/baeldung/mdc/log4j2/Demo.java b/log-mdc/src/test/java/com/baeldung/mdc/log4j2/Demo.java new file mode 100644 index 0000000000..3f7c1d37d5 --- /dev/null +++ b/log-mdc/src/test/java/com/baeldung/mdc/log4j2/Demo.java @@ -0,0 +1,30 @@ +package com.baeldung.mdc.log4j2; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +import org.apache.log4j.Logger; +import org.junit.Test; + +import com.baeldung.mdc.TransactionFactory; +import com.baeldung.mdc.Transfer; +import com.baeldung.mdc.log4j.Log4JRunnable; +import com.baeldung.mdc.log4j2.Log4J2Runnable; +import com.baeldung.mdc.slf4j.Slf4jRunnable; + +public class Demo { + + @Test + public void main() throws InterruptedException { + ExecutorService executor = Executors.newFixedThreadPool(3); + TransactionFactory transactionFactory = new TransactionFactory(); + for (int i = 0; i < 10; i++) { + Transfer tx = transactionFactory.newInstance(); + Runnable task = new Log4J2Runnable(tx); + executor.submit(task); + } + executor.shutdown(); + executor.awaitTermination(60, TimeUnit.SECONDS); + } +} diff --git a/log-mdc/src/test/java/com/baeldung/mdc/slf4j/Demo.java b/log-mdc/src/test/java/com/baeldung/mdc/slf4j/Demo.java new file mode 100644 index 0000000000..98db698f47 --- /dev/null +++ b/log-mdc/src/test/java/com/baeldung/mdc/slf4j/Demo.java @@ -0,0 +1,30 @@ +package com.baeldung.mdc.slf4j; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +import org.apache.log4j.Logger; +import org.junit.Test; + +import com.baeldung.mdc.TransactionFactory; +import com.baeldung.mdc.Transfer; +import com.baeldung.mdc.log4j.Log4JRunnable; +import com.baeldung.mdc.log4j2.Log4J2Runnable; +import com.baeldung.mdc.slf4j.Slf4jRunnable; + +public class Demo { + + @Test + public void main() throws InterruptedException { + ExecutorService executor = Executors.newFixedThreadPool(3); + TransactionFactory transactionFactory = new TransactionFactory(); + for (int i = 0; i < 10; i++) { + Transfer tx = transactionFactory.newInstance(); + Runnable task = new Slf4jRunnable(tx); + executor.submit(task); + } + executor.shutdown(); + executor.awaitTermination(60, TimeUnit.SECONDS); + } +} From 23db51fbd75d5dacb95db6acf44c3730c0d89861 Mon Sep 17 00:00:00 2001 From: danidemi Date: Mon, 7 Nov 2016 23:08:51 +0100 Subject: [PATCH 10/10] Minor typos --- log-mdc/pom.xml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/log-mdc/pom.xml b/log-mdc/pom.xml index a6e9414dc5..7d68049cde 100644 --- a/log-mdc/pom.xml +++ b/log-mdc/pom.xml @@ -1,11 +1,11 @@ 4.0.0 - com.baeldug - logmdcndc + com.baeldung + logmdc 0.0.1-SNAPSHOT - logmdcndc - tutorial on logging with MDC and NDC + logmdc + tutorial on logging with MDC