Intermediate work
This commit is contained in:
parent
be967daeb3
commit
189bee7872
|
@ -7,7 +7,29 @@
|
|||
<name>logmdcndc</name>
|
||||
<description>tutorial on logging with MDC and NDC</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.4.1.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jayway.jsonpath</groupId>
|
||||
<artifactId>json-path</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!--log4j dependencies -->
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
|
|
|
@ -2,6 +2,6 @@ package com.baeldung.mdc;
|
|||
|
||||
public interface BusinessService {
|
||||
|
||||
void businessMethod(String transactionId);
|
||||
boolean transfer(Long amount);
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -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()
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue