Expand my Community achievements bar.

Using MDC to log out a user journey trace path in AEM

Avatar

Level 3

Hi all,

I am relatively new to concept of using SL4J and Apache Sling Commons Logging Framework in AEM.

I need to try and write a SlingFilter in AEM that puts a MDC Key say a uuid into the request chain and then in one of my OSGI Servlets - I need to be able to log the MDC Key out as the user progresses through the journey.

Is there a sample that I can refer to or a series of configuration steps that I can try out to achieve this?

To add specifics to above, I am using AEM 6.2 and in the felix console, I made a configuration as below:

Apache Sling Logger Logging Configuration:

Log Level: Debug

Log File: logs/project-myapp-name-debug.log

Message Pattern: %d{dd.MM.yyyy HH:mm:ss.SSS} *%p* [%X{UUID}] [%t] %c %msg%n

Logger: com.myappbrand.myapp-parent-package

Additiv : I left the checkbox as unchecked

---------------

Also in my Custom Sling Filter named LoggingFilter I have added these lines to add a UUID per request:

@SlingFilter(order = -700, scope = SlingFilterScope.REQUEST)
public class LoggingFilter implements Filter {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    
    private static final String HEADERS_TO_TRY = "X-Forwarded-For";

    @Override
    public void doFilter(final ServletRequest request, final ServletResponse response,
            final FilterChain filterChain) throws IOException, ServletException {

        final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
        UUID uuidNo = java.util.UUID.randomUUID();
        logger.debug("Adding UUID as: {}", uuidNo);
        MDC.put("UUID", uuidNo.toString());

        filterChain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig filterConfig) {}

    @Override
    public void destroy() {}

}

My expectation was that once the above is done, I would see the UUID appear in all my application specific DEBUG log statements in configured log file, however the same does not happen.

Any pointers towards a probable reason or correction to my expected result are greatly appreciated.

Thanks,

Hemant

1 Reply