Logback demo added
This commit is contained in:
parent
62de84bb52
commit
be967daeb3
|
@ -7,7 +7,10 @@ _Log4j MDC_
|
||||||
* <https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/MDC.html>
|
* <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/>
|
* <http://veerasundar.com/blog/2009/10/log4j-mdc-mapped-diagnostic-context-what-and-why/>
|
||||||
|
|
||||||
_Log4j2_
|
_Log4j2 MDC_
|
||||||
* <https://logging.apache.org/log4j/2.x/manual/thread-context.html>
|
* <https://logging.apache.org/log4j/2.x/manual/thread-context.html>
|
||||||
|
|
||||||
|
_Logback MDC_
|
||||||
|
* <http://logback.qos.ch/manual/mdc.html>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,13 @@
|
||||||
<version>3.3.4</version>
|
<version>3.3.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--logback dependencies-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>ch.qos.logback</groupId>
|
||||||
|
<artifactId>logback-classic</artifactId>
|
||||||
|
<version>1.1.7</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,17 @@
|
||||||
|
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 void businessMethod(String transactionId) {
|
||||||
|
logger.info("Executing transaction #{}", transactionId);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.baeldung.mdc.logback;
|
||||||
|
|
||||||
|
import org.slf4j.MDC;
|
||||||
|
|
||||||
|
import com.baeldung.mdc.TransactionContext;
|
||||||
|
|
||||||
|
public class LogbackExecutor 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().toString());
|
||||||
|
|
||||||
|
new LogbackBusinessService().businessMethod(ctx.getTransactionId());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
<configuration>
|
||||||
|
|
||||||
|
# Console appender
|
||||||
|
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||||
|
<pattern>logback %d{yyyy-MM-dd HH:mm:ss} %p %m %X{transaction.owner} %X{transaction.createdAt} %n</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="TRACE">
|
||||||
|
<appender-ref ref="stdout" />
|
||||||
|
</root>
|
||||||
|
|
||||||
|
</configuration>
|
Loading…
Reference in New Issue