From 9bd83d921cff2b0ccfb0b575a06a5daf2f72dc1b Mon Sep 17 00:00:00 2001 From: danidemi Date: Sun, 6 Nov 2016 15:57:07 +0100 Subject: [PATCH] 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