From 2805e5e136e2404b19b906abbb1df7fd834162ca Mon Sep 17 00:00:00 2001 From: Mohamed EL HABIB Date: Sun, 29 Oct 2017 08:31:28 +0100 Subject: [PATCH] provide default implementation to inject correlationid into MDC --- README.md | 31 +++++++++++++++++-- .../log/EnableRequestCorrelationAndMDC.java | 16 ++++++++++ .../correlation/log/MDCAutoConfig.java | 24 ++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 src/main/java/io/jmnarloch/spring/request/correlation/log/EnableRequestCorrelationAndMDC.java create mode 100644 src/main/java/io/jmnarloch/spring/request/correlation/log/MDCAutoConfig.java diff --git a/README.md b/README.md index 04e720b..8a7619c 100644 --- a/README.md +++ b/README.md @@ -65,8 +65,29 @@ Besides that you will also have transparent integration with fallowing: The extension itself simply gives you means to propagate the information. How you going to use it is up to you. -For instance you can apply this information to your logging MDC map. You can achieve that by registering -`RequestCorrelationInterceptor` bean. The `RequestCorrelationInterceptor` gives you only an entry point so that +For instance you can apply this information to your logging MDC map by using `@EnableRequestCorrelationAndMDC` instead of `@EnableRequestCorrelation` + +```java +@EnableRequestCorrelationAndMDC +@SpringBootApplication +public class Application { + +} +``` + +This annotation will register the `correlationId` into MDC map. You can use it for example with logback by overridden the `logging.pattern.level` property + +``` +logging.pattern.level: "%5p [correlationId:%X{correlationId:-}]" +``` + +you will get the following log + +``` +2017-10-29 07:57:48.009 INFO [correlationId:674f1042-a767-4d64-a209-9f0ab1753a78] 31162 --- [nio-8888-exec-1] ... +``` + +Or You can achieve that by registering `RequestCorrelationInterceptor` bean. The `RequestCorrelationInterceptor` gives you only an entry point so that any fallowing operation would be able to access the correlation identifier. You may also use Spring's [HandlerInterceptor](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/HandlerInterceptor.html) and set the value there. @@ -79,6 +100,12 @@ public RequestCorrelationInterceptor correlationLoggingInterceptor() { public void afterCorrelationIdSet(String correlationId) { MDC.put("correlationId", correlationId); } + + @Override + public void cleanUp(String correlationId) { + MDC.remove("correlationId"); + } + }; } ``` diff --git a/src/main/java/io/jmnarloch/spring/request/correlation/log/EnableRequestCorrelationAndMDC.java b/src/main/java/io/jmnarloch/spring/request/correlation/log/EnableRequestCorrelationAndMDC.java new file mode 100644 index 0000000..bb21f59 --- /dev/null +++ b/src/main/java/io/jmnarloch/spring/request/correlation/log/EnableRequestCorrelationAndMDC.java @@ -0,0 +1,16 @@ +package io.jmnarloch.spring.request.correlation.log; + +import io.jmnarloch.spring.request.correlation.api.EnableRequestCorrelation; +import org.springframework.context.annotation.Import; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Import(MDCAutoConfig.class) +@EnableRequestCorrelation +public @interface EnableRequestCorrelationAndMDC { +} \ No newline at end of file diff --git a/src/main/java/io/jmnarloch/spring/request/correlation/log/MDCAutoConfig.java b/src/main/java/io/jmnarloch/spring/request/correlation/log/MDCAutoConfig.java new file mode 100644 index 0000000..6a92280 --- /dev/null +++ b/src/main/java/io/jmnarloch/spring/request/correlation/log/MDCAutoConfig.java @@ -0,0 +1,24 @@ +package io.jmnarloch.spring.request.correlation.log; + +import io.jmnarloch.spring.request.correlation.api.RequestCorrelationInterceptor; +import org.slf4j.MDC; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class MDCAutoConfig { + @Bean + public RequestCorrelationInterceptor correlationLoggingInterceptor() { + return new RequestCorrelationInterceptor() { + @Override + public void afterCorrelationIdSet(String correlationId) { + MDC.put("correlationId", correlationId); + } + + @Override + public void cleanUp(String correlationId) { + MDC.remove("correlationId"); + } + }; + } +}