All about Tracing Module and MDC Logging in MuleSoft

Gursimran Singh Ranhotra
Another Integration Blog
5 min readApr 8, 2024

Tracing module enables users to enhance the logs by adding, removing, and clearing all variables from the logging context for a given Mule event. It also enables you to modify the correlation ID during flow execution.

At the end of this article, you will learn how to Change the Correlation ID During Flow Execution and different operations of configuring the Logging Variables (Clear logging variables, Remove logging variables, and Set logging variables using MuleSoft as an iPaaS tool.

Why MDC Logging?

MDC stands for Mapped Diagnostic Context. It enriches logging and improves tracking by providing more context and information in the logs for the current Mule event.

By default, Mule logs two MDC entries: processor, which shows the location of the current event, and event, which shows the correlation ID of the event. The logging context exists for the entire execution of the corresponding event.

How to use MDC Logging

1. Add the Tracing Module to your Mule Project

2. Change Pattern Layout to MDC in log4j2.xml

Open log4j2.xml located at the src/main/resources of your project and replace [processor: %X{processorPath}; event: %X{correlationId}] with [%MDC] for Pattern Layout.

This change instructs Mule to automatically add the MDC context, which includes the correlation ID and the processor path.

Example log4j.xml File Configurations

Default log4j.xml file without MDC logging:

<PatternLayout pattern="%-5p %d [%t] [processor: %X{processorPath}; event: %X{correlationId}] %c: %m%n"/>

Updated log4j.xml file with MDC logging:

<PatternLayout pattern="%-5p %d [%t] [%MDC] %c: %m%n"/>

3. To see the variable in the console as well, kindly add the Console logging in Appenders tag. Also, add this reference to the AsyncRoot tag.

Example Log4j2.xml:

Here, I’m attaching the entire flow covered in this blog for better understanding-

Operations:

1. Changing the Correlation ID During Flow Execution with the Tracing Module

For these scenarios, the Tracing module With CorrelationID scope enables you to modify the correlation ID during the execution of that scope.

  1. Here, I’m wrapping the 2 loggers and one transform message inside the “With correlation Id” scope.

On execution, you can observe that the original correlation ID (“gursimran1234) is changed to “123” for all processors that are wrapped inside the “With Correlation Id” Scope.

Pfa the Logs below:

Note:
When an HTTP Request is received, the request is inspected for the “X-Correlation-Id” header.

•If the “X-Correlation-Id” header is present, HTTP connector uses this as the Correlation Id.
• If the “X-Correlation-Id” header is not present, a Correlation Id is randomly generated.

In this scenario, I am using the below custom correlationId:

2. Configure Logging Variables:

Here, I would describe Configuring the Clear logging variables, Removing logging variable, and the Set logging variable Tracing module operations.

  1. Set Logging Variable:

With set logging Variable, you can log variables based on the request payload and attributes:

In my flow, I am configuring its value to payload.orderId which propagates to value 123.

Thus, in the logs, you can see that OrderVar is getting printed alongside the correlationId.

I am again setting a variable that prints the variable customerVar along with it.

Final Logs:

Thus, currently, both the variables are printed successfully.

2. Remove Logging Variables

Removes a variable from the event logging context. In the below example, I’m removing the below logging variable (customerVar).

Thus, we can see that by executing this, the variable customerVar is removed from logs.

3. Clear Logging Variables

This Clears all the current logging variables.

As we have only one variable left now, we can see that it also gets removed after executing the Clear Logging Variables operation.

Note: This operation does not remove the correlation ID nor the current event processor because those variables are managed by the Mule runtime engine.

Thanks for reading!
Please feel free to comment and follow me for more helpful articles and insights in the future.

--

--