1

I'm using HandlerInterceptorAdapter to generate a correlation id for each service request. Here is the configuration I'm using. I'm trying to append the correlation id on my console log as shown in the expected output. But somehow it was not generating the log pattern as shown below.

log4j2.xml

<?xml version="1.0" encoding="UTF-8" ?>
<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
        </Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %X{correlationId} %-5level %logger{36} - %msg%n"/>
        </Console>
        <ApplicationInsightsAppender name="aiAppender"/>
    </Appenders>
    <Loggers>
        <Root level="trace">
            <AppenderRef ref="Console" level = "info" />
            <AppenderRef ref="aiAppender" level= "info" />
        </Root>
    </Loggers>
</Configuration>

CorrelationInterceptor.java

 public class CorrelationInterceptor extends HandlerInterceptorAdapter{

                 private static final String CORRELATION_ID_LOG_VAR_NAME = "correlationId";
                 private static final String CORRELATION_ID_HEADER_NAME = "Correlation-Id";
   
                   
   @Override
    public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response,
                             final Object handler) throws Exception {
        final String correlationId = getCorrelationIdFromHeader(request);
        System.out.println("correlationId:{}"+correlationId);
        MDC.put(CORRELATION_ID_LOG_VAR_NAME, correlationId);
       
        System.out.println("correlationId:{}"+MDC.get(CORRELATION_ID_LOG_VAR_NAME));
        return true;
    }
   
    @Override
    public void afterCompletion(final HttpServletRequest request, final HttpServletResponse response,
                                final Object handler, final Exception ex) throws Exception {
    System.out.println("correlationId:{}"+MDC.get(CORRELATION_ID_LOG_VAR_NAME));
        MDC.remove(CORRELATION_ID_LOG_VAR_NAME);
        System.out.println("correlationId:{}"+MDC.get(CORRELATION_ID_LOG_VAR_NAME));
    }

    private String getCorrelationIdFromHeader(final HttpServletRequest request) {
        String correlationId = request.getHeader(CORRELATION_ID_HEADER_NAME);
        if (IpUtil.isNullOrEmpty(correlationId)) {
            correlationId = generateUniqueCorrelationId();
        }
        return correlationId;
    }
   
    private String generateUniqueCorrelationId() {
        return UUID.randomUUID().toString();
    }

}

Output:

2020-12-29 17:59:42.026  INFO 31140 --- [nio-8080-exec-1] filter.RequestHeaderFilter       : No correlationId found In Header. Generated :29e624b7-3ab6-4a1c-8c6a-4da342de4858 
2020-12-29 17:59:42.027  INFO 31140 --- [nio-8080-exec-1] filter.RequestHeaderFilter       : Found coorelationId in Header : 29e624b7-3ab6-4a1c-8c6a-4da342de4858

Expected Output:

2020-12-30 13:59:01.352 [http-nio-8080-exec-3] ea7aa31a-1e26-42e0-b7a6-ef0562ba5c4f INFO  helper.GraphServiceHelper - CATEGORIES : [13], SUB_CATEGORIES : [0]
2020-12-30 13:59:01.353 [http-nio-8080-exec-3] ea7aa31a-1e26-42e0-b7a6-ef0562ba5c4f INFO  helper.GraphServiceHelper - locationType :club
João Dias
  • 16,277
  • 6
  • 33
  • 45
TechGeek
  • 480
  • 1
  • 8
  • 22
  • did you solve ? – DarthVader Aug 10 '21 at 18:58
  • 1
    @DarthVader Oh yes!! Actually the same code snippet is working. But, Intelliji has some issue printing that Unique Id on console. When I deploy the application, I was able to see the same code is generating it. – TechGeek Aug 10 '21 at 19:38

0 Answers0