Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

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

hemantbellani-O
Level 3
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