DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • MuleSoft Integration With RabbitMQ
  • Managing Dynamic Application Properties in MuleSoft for CloudHub Applications
  • Resilient Kafka Consumers With Reactor Kafka
  • Enabling Business Transaction Monitoring for App Connect Enterprise

Trending

  • Why Good Models Fail After Deployment
  • Zone-Free Angular: Unlocking High-Performance Change Detection With Signals and Modern Reactivity
  • Improving DAG Failure Detection in Airflow Using AI Techniques
  • Optimizing High-Volume REST APIs Using Redis Caching and Spring Boot (With Load Testing Code)
  1. DZone
  2. Data Engineering
  3. Databases
  4. MDC Logging With MuleSoft Runtime 4.4

MDC Logging With MuleSoft Runtime 4.4

With MDC Logging, logs can be enriched by providing more information about the event in the logs.

By 
Jitendra Bafna user avatar
Jitendra Bafna
·
Oct. 30, 21 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
20.6K Views

Join the DZone community and get the full member experience.

Join For Free

MDC stands for Mapped Diagnostic Context. Mapped Diagnostic Context enriched the logs by providing more information about the event in the logs. By default, Mule logs two entries: processor which shows the location of current events, events which shows the correlation Id of the event.

Mule Runtime 4.4 introduced Tracing module and enables you to add more information to the logs by by adding, removing, and clearing variables from the logging context for a given Mule event.


Prerequisites

There are 2 main prerequisites for using MDC logging

  • Add Tracing Module to your Mule Project
  • Change Pattern Layout to MDC in log4j2.xml
XML
 
<PatternLayout pattern="%-5p %d [%t] [%MDC] %c: %m%n"/>


Change Pattern Layout in log4j2.xml

Open log4j2.xml located at the src/main/resources of your project.

Replace [processor: %X{processorPath}; event: %X{correlationId}]  with [%MDC] for Pattern Layout.

XML
 
   <Appenders>
        <RollingFile name="file" fileName="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}mdc-logging-example.log"
                 filePattern="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}mdc-logging-example-%i.log">
            <PatternLayout pattern="%-5p %d [%t] [%MDC] %c: %m%n"/>
            <SizeBasedTriggeringPolicy size="10 MB"/>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
    </Appenders>

Configure the Set Logging Variables

  • To set logging variables, first you need to install Tracing Module in Anypoint Studio.
  • Drag and Drop Set Logging variables to your mule application.
  • Set variable name and value. This can be any id, query parameters, headers or any other information which help you searching or group related events.

In configuration xml, set logging variable will look like as shown below

XML
 
<tracing:set-logging-variable doc:name="Set logging variable" 
 doc:id="64dabc52-4712-4789-9dc9-f84b5827c86e" variableName="uuid" 
 value="#[attributes.queryParams.uuid]"/>

After executing the flow, the output logs are:

 
INFO  2021-10-28 20:28:11,395 [[MuleRuntime].uber.28: [mule-logging-application].mule-logging-applicationFlow.CPU_LITE @3ae64de5] [{correlationId=20211028202811, uuid=1234, processorPath=mule-logging-applicationFlow/processors/2}] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Start Transaction

Example

XML
 
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:tracing="http://www.mulesoft.org/schema/mule/tracing" xmlns:http="http://www.mulesoft.org/schema/mule/http"
	xmlns="http://www.mulesoft.org/schema/mule/core"
	xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/tracing http://www.mulesoft.org/schema/mule/tracing/current/mule-tracing.xsd">
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="7a1a2ee2-bac5-47f8-a528-002f07839486" >
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<configuration doc:name="Configuration" doc:id="4bcc6cce-e71d-463a-ac41-71b9c67ecf81" correlationIdGeneratorExpression='#[now() as String{format:"yyyyMMddHHmmss"}]' />
	<flow name="mdc-logging-exampleFlow" doc:id="b875539a-9276-4302-822c-b871c01c850b" >
		<http:listener doc:name="Listener" doc:id="f7b5cd1d-4f14-44ca-887a-9c7ca9837dbb" config-ref="HTTP_Listener_config" path="/test"/>
		<tracing:set-logging-variable doc:name="Set logging variable" doc:id="64dabc52-4712-4789-9dc9-f84b5827c86e" variableName="uuid" value="#[attributes.queryParams.uuid]"/>
		<tracing:set-logging-variable doc:name="Set logging variable" doc:id="4afdf594-b217-4063-8e1e-023dffdc0a18" variableName="Test" value="1234343"/>
		<logger level="INFO" doc:name="Logger" doc:id="a9dc5218-e5cf-4913-8dbb-23999a90400e" message="#['Start of the flow']"/>
		<logger level="INFO" doc:name="Logger" doc:id="655909d8-7133-42db-a49a-ff7130a6d8b7" message="#[payload]"/>
	</flow>
</mule>


Check this video showing how to configure MDC logging in your Mule application.

Now, you know how to configure MDC logging for your Mule Application.

Event MuleSoft application XML Drops (app) Correlation (projective geometry) Database Flow (web browser)

Opinions expressed by DZone contributors are their own.

Related

  • MuleSoft Integration With RabbitMQ
  • Managing Dynamic Application Properties in MuleSoft for CloudHub Applications
  • Resilient Kafka Consumers With Reactor Kafka
  • Enabling Business Transaction Monitoring for App Connect Enterprise

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook