Log4J MDC, Log4J2 MDC samples added.
This commit is contained in:
parent
c0ed2e50cb
commit
62de84bb52
13
log-mdc-ndc/README.md
Normal file
13
log-mdc-ndc/README.md
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
### Relevant Articles:
|
||||||
|
- TBD
|
||||||
|
|
||||||
|
### References
|
||||||
|
|
||||||
|
_Log4j MDC_
|
||||||
|
* <https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/MDC.html>
|
||||||
|
* <http://veerasundar.com/blog/2009/10/log4j-mdc-mapped-diagnostic-context-what-and-why/>
|
||||||
|
|
||||||
|
_Log4j2_
|
||||||
|
* <https://logging.apache.org/log4j/2.x/manual/thread-context.html>
|
||||||
|
|
||||||
|
|
39
log-mdc-ndc/pom.xml
Normal file
39
log-mdc-ndc/pom.xml
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.baeldug</groupId>
|
||||||
|
<artifactId>logmdcndc</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>logmdcndc</name>
|
||||||
|
<description>tutorial on logging with MDC and NDC</description>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!--log4j dependencies -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>log4j</groupId>
|
||||||
|
<artifactId>log4j</artifactId>
|
||||||
|
<version>1.2.17</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--log4j2 dependencies-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-api</artifactId>
|
||||||
|
<version>2.7</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-core</artifactId>
|
||||||
|
<version>2.7</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--disruptor for log4j2 async logging-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.lmax</groupId>
|
||||||
|
<artifactId>disruptor</artifactId>
|
||||||
|
<version>3.3.4</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.baeldung.mdc;
|
||||||
|
|
||||||
|
public interface BusinessService {
|
||||||
|
|
||||||
|
void businessMethod(String transactionId);
|
||||||
|
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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 );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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 );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
12
log-mdc-ndc/src/main/resources/log4j.properties
Normal file
12
log-mdc-ndc/src/main/resources/log4j.properties
Normal file
@ -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
|
32
log-mdc-ndc/src/main/resources/log4j2.xml
Normal file
32
log-mdc-ndc/src/main/resources/log4j2.xml
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<Configuration status="INFO">
|
||||||
|
<Appenders>
|
||||||
|
# Console appender
|
||||||
|
<Console name="stdout" target="SYSTEM_OUT">
|
||||||
|
# Pattern of log message for console appender
|
||||||
|
<PatternLayout pattern="Log4j2 - %d{yyyy-MM-dd HH:mm:ss} %p %m owner=%X{transaction.owner} creation=%X{transaction.createdAt}%n"/>
|
||||||
|
</Console>
|
||||||
|
|
||||||
|
# File appender
|
||||||
|
<!--
|
||||||
|
|
||||||
|
<File name="fout" fileName="log4j/target/baeldung-log4j2.log" immediateFlush="false" append="false">
|
||||||
|
# Pattern of log message for file appender
|
||||||
|
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %p %m%n"/>
|
||||||
|
</File>
|
||||||
|
-->
|
||||||
|
</Appenders>
|
||||||
|
|
||||||
|
<Loggers>
|
||||||
|
# Override log level for specified package
|
||||||
|
<Logger name="com.baeldung.log4j2" level="TRACE"/>
|
||||||
|
|
||||||
|
<AsyncRoot level="DEBUG">
|
||||||
|
<AppenderRef ref="stdout"/>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
<AppenderRef ref="fout"/>
|
||||||
|
-->
|
||||||
|
</AsyncRoot>
|
||||||
|
</Loggers>
|
||||||
|
</Configuration>
|
Loading…
x
Reference in New Issue
Block a user