Skip to content

Commit 9bd83d9

Browse files
committed
Examples done.
1 parent 189bee7 commit 9bd83d9

25 files changed

Lines changed: 197 additions & 272 deletions

‎log-mdc-ndc/pom.xml‎

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,18 @@
77
<name>logmdcndc</name>
88
<description>tutorial on logging with MDC and NDC</description>
99

10-
<parent>
11-
<groupId>org.springframework.boot</groupId>
12-
<artifactId>spring-boot-starter-parent</artifactId>
13-
<version>1.4.1.RELEASE</version>
14-
</parent>
15-
1610
<dependencies>
17-
18-
<dependency>
19-
<groupId>org.springframework.boot</groupId>
20-
<artifactId>spring-boot-starter-web</artifactId>
21-
</dependency>
22-
<dependency>
23-
<groupId>org.springframework.boot</groupId>
24-
<artifactId>spring-boot-starter-test</artifactId>
25-
<scope>test</scope>
26-
</dependency>
27-
<dependency>
28-
<groupId>com.jayway.jsonpath</groupId>
29-
<artifactId>json-path</artifactId>
30-
<scope>test</scope>
31-
</dependency>
11+
12+
<dependency>
13+
<groupId>org.springframework</groupId>
14+
<artifactId>spring-context</artifactId>
15+
<version>4.3.3.RELEASE</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.springframework</groupId>
19+
<artifactId>spring-webmvc</artifactId>
20+
<version>4.3.3.RELEASE</version>
21+
</dependency>
3222

3323
<!--log4j dependencies -->
3424
<dependency>

‎log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
public interface BusinessService {
44

5-
boolean transfer(Long amount);
5+
/** Sample service transferring a given amount of money.
6+
* @return {@code true} when the transfer complete successfully, {@code false} otherwise. */
7+
boolean transfer(long amount);
68

79
}
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package com.baeldung.mdc;
22

3+
/**
4+
* A fake {@link BusinessService} simulating an actual one.
5+
*/
36
public class DefaultBusinessService implements BusinessService {
47

5-
@Override
6-
public boolean transfer(Long amount) {
8+
public boolean transfer(long amount) {
79
try {
8-
Thread.sleep((long) (Math.random()*1000));
10+
// let's pause randomly to properly simulate an actual system.
11+
Thread.sleep((long) (500 + Math.random()*500));
912
} catch (InterruptedException e) {
10-
// should not happen
13+
// should never happen
1114
}
12-
return Math.random() >= 0.5;
15+
// let's simulate both failing and successful transfers
16+
return Math.random() >= 0.25;
1317
}
1418

1519
}

‎log-mdc-ndc/src/main/java/com/baeldung/mdc/DelegateBusiness.java‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,20 @@ public DelegateBusiness(BusinessService delegate) {
99
this.delegate = delegate;
1010
}
1111

12-
1312
public DelegateBusiness() {
1413
this( new DefaultBusinessService() );
1514
}
1615

17-
public boolean transfer(Long amount) {
18-
preTransfer();
16+
public boolean transfer(long amount) {
17+
preTransfer(amount);
1918
boolean outcome = delegate.transfer(amount);
20-
postTransfer(outcome);
19+
postTransfer(amount, outcome);
2120
return outcome;
2221
}
2322

24-
abstract protected void postTransfer(boolean outcome);
23+
abstract protected void postTransfer(long amount, boolean outcome);
2524

26-
abstract protected void preTransfer();
25+
abstract protected void preTransfer(long amount);
2726

2827

2928

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,25 @@
11
package com.baeldung.mdc;
22

3-
import java.util.concurrent.Callable;
43
import java.util.concurrent.ExecutorService;
54
import java.util.concurrent.Executors;
65

7-
import org.apache.log4j.MDC;
6+
import org.apache.log4j.Logger;
87

9-
import com.baeldung.mdc.log4j.Log4jBusinessService;
8+
import com.baeldung.mdc.log4j.Log4JRunnable;
109

1110
public class GenericApp {
11+
1212
public static void main(String[] args) {
1313

1414
ExecutorService executor = Executors.newFixedThreadPool(3);
1515
TransactionFactory transactionFactory = new TransactionFactory();
1616

1717
for(int i=0; i<10; i++){
18-
19-
TransactionContext tx = transactionFactory.buildTransaction();
20-
21-
executor.submit(new Runnable() {
22-
@Override
23-
public void run() {
24-
new DelegateBusiness() {
25-
26-
@Override
27-
protected void preTransfer() {
28-
MDC.put("transaction.id", tx.getTransactionId());
29-
MDC.put("transaction.owner", tx.getOwner());
30-
MDC.put("transaction.createdAt", tx.getCreatedAt());
31-
}
32-
33-
@Override
34-
protected void postTransfer(boolean outcome) {
35-
// TODO Auto-generated method stub
36-
37-
}
38-
}.transfer(
39-
tx.getAmount()
40-
);
41-
}
42-
});
18+
final Transaction tx = transactionFactory.newInstance();
19+
executor.submit(new Log4JRunnable(tx));
4320
}
4421

22+
executor.shutdown();
23+
4524
}
4625
}

log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionContext.java renamed to log-mdc-ndc/src/main/java/com/baeldung/mdc/Transaction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
import java.util.Date;
44

5-
public class TransactionContext {
5+
public class Transaction {
66

77
private String transactionId;
88
private String owner;
99
private Date createdAt;
1010
private Long amount;
1111

12-
public TransactionContext(String transactionId, String owner) {
12+
public Transaction(String transactionId, String owner, long amount) {
1313
this.transactionId = transactionId;
1414
this.owner = owner;
1515
this.createdAt = new Date();
16-
this.amount = (long) (Math.random()*100);
16+
this.amount = amount;
1717
}
1818

1919
public String getOwner() {
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
package com.baeldung.mdc;
22

3+
import static java.lang.Math.floor;
4+
import static java.lang.Math.random;
5+
6+
import java.util.UUID;
7+
38
public class TransactionFactory {
49

5-
public TransactionContext buildTransaction() {
6-
TransactionContext ctx = new TransactionContext("" + Math.random(), "owner" + Math.random());
7-
return ctx;
10+
private static final String[] NAMES = {"John", "Susan", "Marc", "Samantha"};
11+
12+
public Transaction newInstance() {
13+
String transactionId = UUID.randomUUID().toString();
14+
String owner = NAMES[ (int) floor(random()*NAMES.length) ];
15+
long amount = (long) (random()*1500 + 500);
16+
Transaction tx = new Transaction(transactionId, owner, amount);
17+
return tx;
818
}
919

1020
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.mdc.log4j;
2+
3+
import org.apache.logging.log4j.LogManager;
4+
import org.apache.logging.log4j.Logger;
5+
6+
import com.baeldung.mdc.DelegateBusiness;
7+
8+
final class Log4JBusinessService extends DelegateBusiness {
9+
10+
private static final Logger logger = LogManager.getLogger();
11+
12+
@Override
13+
protected void preTransfer(long amount) {
14+
logger.info("Preparing to transfer {}$.");
15+
}
16+
17+
@Override
18+
protected void postTransfer(long amount, boolean outcome) {
19+
logger.info("Has transfer of {}$ completed successfully ? {}.");
20+
}
21+
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.mdc.log4j;
2+
3+
import org.apache.log4j.MDC;
4+
5+
import com.baeldung.mdc.Transaction;
6+
7+
public class Log4JRunnable implements Runnable {
8+
private final Transaction tx;
9+
10+
public Log4JRunnable(Transaction tx) {
11+
this.tx = tx;
12+
}
13+
14+
public void run() {
15+
16+
MDC.put("transaction.id", tx.getTransactionId());
17+
MDC.put("transaction.owner", tx.getOwner());
18+
MDC.put("transaction.createdAt", tx.getCreatedAt());
19+
20+
new Log4JBusinessService().transfer(tx.getAmount());
21+
22+
MDC.clear();
23+
24+
}
25+
}

‎log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jBusinessService.java‎

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)