Skip to content

Commit 4981a39

Browse files
author
Eugen
authored
Merge pull request eugenp#814 from danidemi/master
PR for "Improve Logging With Mapped Diagnostic Context"
2 parents 55e8a4f + 23db51f commit 4981a39

18 files changed

Lines changed: 454 additions & 0 deletions

‎log-mdc/README.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
### Relevant Articles:
2+
- TBD
3+
4+
### References
5+
6+
_Log4j MDC_
7+
* <https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/MDC.html>
8+
* <http://veerasundar.com/blog/2009/10/log4j-mdc-mapped-diagnostic-context-what-and-why/>
9+
10+
_Log4j2 MDC_
11+
* <https://logging.apache.org/log4j/2.x/manual/thread-context.html>
12+
13+
_Logback MDC_
14+
* <http://logback.qos.ch/manual/mdc.html>
15+
16+

‎log-mdc/pom.xml‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>logmdc</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<name>logmdc</name>
8+
<description>tutorial on logging with MDC</description>
9+
10+
<dependencies>
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>
22+
23+
<!--log4j dependencies -->
24+
<dependency>
25+
<groupId>log4j</groupId>
26+
<artifactId>log4j</artifactId>
27+
<version>1.2.17</version>
28+
</dependency>
29+
30+
<!--log4j2 dependencies -->
31+
<dependency>
32+
<groupId>org.apache.logging.log4j</groupId>
33+
<artifactId>log4j-api</artifactId>
34+
<version>2.7</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.apache.logging.log4j</groupId>
38+
<artifactId>log4j-core</artifactId>
39+
<version>2.7</version>
40+
</dependency>
41+
42+
<!--disruptor for log4j2 async logging -->
43+
<dependency>
44+
<groupId>com.lmax</groupId>
45+
<artifactId>disruptor</artifactId>
46+
<version>3.3.4</version>
47+
</dependency>
48+
49+
<!--logback dependencies -->
50+
<dependency>
51+
<groupId>ch.qos.logback</groupId>
52+
<artifactId>logback-classic</artifactId>
53+
<version>1.1.7</version>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>junit</groupId>
58+
<artifactId>junit</artifactId>
59+
<version>4.12</version>
60+
<scope>test</scope>
61+
</dependency>
62+
63+
64+
</dependencies>
65+
66+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.mdc;
2+
3+
import static java.lang.Math.floor;
4+
import static java.lang.Math.random;
5+
6+
import java.util.UUID;
7+
8+
public class TransactionFactory {
9+
10+
private static final String[] NAMES = {"John", "Susan", "Marc", "Samantha"};
11+
private static long nextId = 1;
12+
13+
public Transfer newInstance() {
14+
String transactionId = String.valueOf( nextId++ );
15+
String owner = NAMES[ (int) floor(random()*NAMES.length) ];
16+
long amount = (long) (random()*1500 + 500);
17+
Transfer tx = new Transfer(transactionId, owner, amount);
18+
return tx;
19+
}
20+
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.mdc;
2+
3+
public class Transfer {
4+
5+
private String transactionId;
6+
private String sender;
7+
private Long amount;
8+
9+
public Transfer(String transactionId, String sender, long amount) {
10+
this.transactionId = transactionId;
11+
this.sender = sender;
12+
this.amount = amount;
13+
}
14+
15+
public String getSender() {
16+
return sender;
17+
}
18+
19+
public String getTransactionId() {
20+
return transactionId;
21+
}
22+
23+
public Long getAmount() {
24+
return amount;
25+
}
26+
27+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.mdc;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
6+
import org.apache.log4j.Logger;
7+
8+
import com.baeldung.mdc.log4j.Log4JRunnable;
9+
import com.baeldung.mdc.log4j2.Log4J2Runnable;
10+
import com.baeldung.mdc.slf4j.Slf4jRunnable;
11+
12+
public class TransferDemo {
13+
14+
public static void main(String[] args) {
15+
16+
ExecutorService executor = Executors.newFixedThreadPool(3);
17+
TransactionFactory transactionFactory = new TransactionFactory();
18+
19+
for (int i = 0; i < 10; i++) {
20+
Transfer tx = transactionFactory.newInstance();
21+
22+
//Runnable task = new Log4JRunnable(tx);
23+
//Runnable task = new Log4J2Runnable(tx);
24+
Runnable task = new Slf4jRunnable(tx);
25+
26+
executor.submit(task);
27+
}
28+
29+
executor.shutdown();
30+
31+
}
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.mdc;
2+
3+
/**
4+
* A fake transfer service simulating an actual one.
5+
*/
6+
public abstract class TransferService {
7+
8+
/** Sample service transferring a given amount of money.
9+
* @return {@code true} when the transfer complete successfully, {@code false} otherwise. */
10+
public boolean transfer(long amount) {
11+
beforeTransfer(amount);
12+
// exchange messages with a remote system to transfer the money
13+
try {
14+
// let's pause randomly to properly simulate an actual system.
15+
Thread.sleep((long) (500 + Math.random() * 500));
16+
} catch (InterruptedException e) {
17+
// should never happen
18+
}
19+
// let's simulate both failing and successful transfers
20+
boolean outcome = Math.random() >= 0.25;
21+
afterTransfer(amount, outcome);
22+
return outcome;
23+
}
24+
25+
abstract protected void beforeTransfer(long amount);
26+
27+
abstract protected void afterTransfer(long amount, boolean outcome);
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.mdc.log4j;
2+
3+
import org.apache.log4j.MDC;
4+
5+
import com.baeldung.mdc.Transfer;
6+
7+
public class Log4JRunnable implements Runnable {
8+
9+
private Transfer tx;
10+
private static Log4JTransferService log4jBusinessService = new Log4JTransferService();
11+
12+
public Log4JRunnable(Transfer tx) {
13+
this.tx = tx;
14+
}
15+
16+
public void run() {
17+
18+
MDC.put("transaction.id", tx.getTransactionId());
19+
MDC.put("transaction.owner", tx.getSender());
20+
21+
log4jBusinessService.transfer(tx.getAmount());
22+
23+
MDC.clear();
24+
25+
}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.mdc.log4j;
2+
3+
import org.apache.log4j.Logger;
4+
5+
import com.baeldung.mdc.TransferService;
6+
7+
public class Log4JTransferService extends TransferService {
8+
9+
private Logger logger = Logger.getLogger(Log4JTransferService.class);
10+
11+
@Override
12+
protected void beforeTransfer(long amount) {
13+
logger.info("Preparing to transfer " + amount + "$.");
14+
}
15+
16+
@Override
17+
protected void afterTransfer(long amount, boolean outcome) {
18+
logger.info("Has transfer of " + amount + "$ completed successfully ? " + outcome + ".");
19+
}
20+
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.mdc.log4j2;
2+
3+
import org.apache.logging.log4j.ThreadContext;
4+
5+
import com.baeldung.mdc.Transfer;
6+
7+
public class Log4J2Runnable implements Runnable {
8+
private final Transfer tx;
9+
private Log4J2TransferService log4j2BusinessService = new Log4J2TransferService();
10+
11+
public Log4J2Runnable(Transfer tx) {
12+
this.tx = tx;
13+
}
14+
15+
public void run() {
16+
17+
ThreadContext.put("transaction.id", tx.getTransactionId());
18+
ThreadContext.put("transaction.owner", tx.getSender());
19+
20+
log4j2BusinessService.transfer(tx.getAmount());
21+
22+
ThreadContext.clearAll();
23+
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.mdc.log4j2;
2+
3+
import org.apache.logging.log4j.LogManager;
4+
import org.apache.logging.log4j.Logger;
5+
6+
import com.baeldung.mdc.TransferService;
7+
8+
public class Log4J2TransferService extends TransferService {
9+
10+
private static final Logger logger = LogManager.getLogger();
11+
12+
@Override
13+
protected void beforeTransfer(long amount) {
14+
logger.info("Preparing to transfer {}$.", amount);
15+
}
16+
17+
@Override
18+
protected void afterTransfer(long amount, boolean outcome) {
19+
logger.info("Has transfer of {}$ completed successfully ? {}.", amount, outcome);
20+
}
21+
22+
}

0 commit comments

Comments
 (0)