Commit 9795bf2d authored by Kishore Battula's avatar Kishore Battula
Browse files

Added mdc context on start of request

parent f0fa8b37
Pipeline #5716 passed with stages
in 13 minutes and 36 seconds
......@@ -21,7 +21,7 @@
<groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId>
<packaging>jar</packaging>
<version>0.0.18</version>
<version>0.0.19</version>
<name>core-lib-azure</name>
<properties>
......
package org.opengroup.osdu.azure.filters;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* MDC Filter for logging.
*/
@Component
@ConditionalOnProperty(value = "logging.mdccontext.enabled", havingValue = "true", matchIfMissing = false)
public class Slf4jMDCFilter implements Filter {
@Autowired
private DpsHeaders dpsHeaders;
/**
* Filter logic.
* @param servletRequest Request object.
* @param servletResponse Response object.
* @param filterChain Filter Chain object.
* @throws IOException
* @throws ServletException
*/
@Override
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse,
final FilterChain filterChain) throws IOException, ServletException {
MDC.setContextMap(getContextMap());
filterChain.doFilter(servletRequest, servletResponse);
MDC.clear();
}
/**
* Method to create context map for mdc.
* @return Context map.
*/
private Map<String, String> getContextMap() {
final Map<String, String> contextMap = new HashMap<>();
contextMap.put(DpsHeaders.CORRELATION_ID, dpsHeaders.getCorrelationId());
contextMap.put(DpsHeaders.DATA_PARTITION_ID, dpsHeaders.getPartitionId());
return contextMap;
}
}
......@@ -18,12 +18,12 @@
<Configuration packages="com.microsoft.applicationinsights.log4j.v2">
<Properties>
<Property name="LOG_PATTERN">
%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex
%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} correlation-id=%X{correlation-id} data-partition-id=%X{data-partition-id}: %m%n%ex
</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex"/>
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} correlation-id=%X{correlation-id} data-partition-id=%X{data-partition-id}: %m%n%ex"/>
</Console>
<ApplicationInsightsAppender name="aiAppender">
</ApplicationInsightsAppender>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment