Enhance MuleSoft Efficiency Using MDC Logging

Enhance MuleSoft Efficiency Using MDC Logging

Introduction

In the realm of MuleSoft integration, efficiency is paramount. As organizations navigate complex digital landscapes, the ability to streamline processes and optimize performance becomes a competitive advantage. One powerful tool in achieving this optimization is MDC (Mapped Diagnostic Context) logging. MDC logging enables developers to enhance the efficiency of their MuleSoft applications by providing contextual information, facilitating easier troubleshooting, and improving scalability.

In this blog, we will delve into the significance of MDC logging in MuleSoft integrations, exploring its benefits and best practices for implementation. Let us unlock the potential of MDC logging to elevate the performance of your MuleSoft solutions. 

Key Features of MDC

  • MDC is an extension or feature of a logging framework that enhances the capability of loggers by associating contextual information with individual log messages. 
  • It allows developers to map key-value pairs of contextual data to a thread-local context, which persists throughout the lifecycle of a logging event within the same thread. 
  • MDC enables the enrichment of log messages with dynamic contextual information such as user IDs, session IDs, transaction IDs, or any other relevant metadata. 
  • The contextual information stored in the MDC can be included automatically in log messages generated by loggers, providing additional insights into the circumstances surrounding each logged event. 

Operations in Tracing Module 

  1. Set Logging variables 
  2. Clear logging variables 
  3. Remove logging variables 

Scope in Tracing Module

  1. With correlationID

Steps to configure MDC logging in Mule Application 

Create Mule Projects: Create a mule project in Anypoint studio. 

Step:1 Integrate the "MDC Connector" into your Mule Palette by searching for it on the exchange. Download and add it to your project. 

MDC Connector

 Step:2 Tracing Module dependency will be added to your Application’s pom file.

Tracing Module dependency

Step:3 Modify the Pattern Layout to MDC within the log4j2.xml file situated in the src/main/resources directory of your project. However, simply incorporating this change won't display logs on the console. Therefore, console logging must also be implemented. 

<?xml version="1.0" encoding="utf-8"?> 

<Configuration> 

   <Appenders> 

        <RollingFile name="file" fileName="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}tracing-module.log"             filePattern="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}tracing-module-%i.log"> 

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

            <SizeBasedTriggeringPolicy size="10 MB"/> 

            <DefaultRolloverStrategy max="10"/> 

        </RollingFile> 

        <Console name="Console" target="SYSTEM_OUT"> 

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

       </Console> 

    </Appenders> 

    <Loggers> 

        <AsyncLogger name="org.mule.service.http" level="WARN"/> 

        <AsyncLogger name="org.mule.extension.http" level="WARN"/> 

        <AsyncLogger name="org.mule.runtime.core.internal.processor.LoggerMessageProcessor" level="INFO"/> 

        <AsyncRoot level="INFO"> 

           <AppenderRef ref="file"/> 

            <AppenderRef ref="Console"/> 

        </AsyncRoot> 

    </Loggers> 

</Configuration> 

Output Before MDC Context

Flow without MDC Connector
Output without MDC Context 

Output After Adding MDC Context

Flow using Set and Remove logging variable
Output for Set variable logging variable 
Inserting image...
Output for Remove variable logging variable 
Flow using With CorrelationID,Set and Clear logging Variable
Output using With CorrelationID
Output using Set logging variable
Output using two Set logging variable
Output using Clear logging variable

Conclusion

In conclusion, MDC (Mapped Diagnostic Context) logging in MuleSoft integration brings invaluable contextual insights to log messages. By enhancing log entries with dynamic data such as user IDs and session IDs, MDC facilitates streamlined troubleshooting, scalability, and security enforcement. Although modifying the Pattern Layout in log4j2.xml is essential, it alone doesn't suffice for console logging. The incorporation of MDC not only optimizes performance but also strengthens the application's monitoring and auditing capabilities.

Ultimately, MDC logging empowers developers to navigate complexities, ensuring resilient integration solutions that align with evolving business needs, making it a crucial component in modern MuleSoft deployments. 

Mulecraft Footer