While working on Mulesoft I realized that sometimes its a requirement whereby one needs to log various types of inbound and outbound messages in different log files. This is really helpful you want to track huge load of messages coming in and going out of your mule flows.
This can be easily done in Mulesoft as the logging is mainly handled by Log4J implementation. So depending upon your Log4J configuration you can tweak your log setting just like any other java application.
In a following simple example we will see how the flow logs Inbound and Outbound messages in InboundMessage.log and OutboundMessage.log files.
Its a simple flow as shown in above image where inbound end point is http-connector .. we are doing some processing on the incoming message and then the payload is set for http-response. This http-response is outbound endpoint. One can use different types of endpoints from huge library of Mulesoft but the basic configuration remains the same.
This can be easily done in Mulesoft as the logging is mainly handled by Log4J implementation. So depending upon your Log4J configuration you can tweak your log setting just like any other java application.
In a following simple example we will see how the flow logs Inbound and Outbound messages in InboundMessage.log and OutboundMessage.log files.
Its a simple flow as shown in above image where inbound end point is http-connector .. we are doing some processing on the incoming message and then the payload is set for http-response. This http-response is outbound endpoint. One can use different types of endpoints from huge library of Mulesoft but the basic configuration remains the same.
As shown in the second image I have added a log4j config file.
Note: I am using mule 3.6.1 here so I am using Log4J2 ... but one can use mule Log4J with previous versions of mule.
Following is my Log4j config file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<File name="TraceFile" fileName="logs/appTrace.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
<!-- file for inbound endpoint -->
<File name="inboundLogs" fileName="logs/inboundLogs.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
<!-- file for outbound endpoint -->
<File name="outboundLogs" fileName="logs/outboundLogs.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<!-- Jusgt for the Trace -->
<Root level="trace">
<AppenderRef ref="TraceFile" level="trace" />
</Root>
<!-- Assign logger for inbound messages -->
<Logger name="com.mulemagic.demos.inboundlogs" level="debug">
<AppenderRef ref="inboundLogs" level="debug" />
</Logger>
<!-- Assign logger for outbound messages -->
<Logger name="com.mulemagic.demos.outboundlogs" level="debug">
<AppenderRef ref="outboundLogs" level="debug" />
</Logger>
</Loggers>
</Configuration>
In the above config file, I have mentioned two <File> tags under <Appenders> tag. Each File tag represents one log file with the messaging pattern.The <Loggers> section will add unique logger name for every file and this is where we will mark our loggers or direct messages to the already mentioned log files using logger name.
Now lets tie the inbound and outbound messages to these unique <Logger> in our mule config files ..Following is the exact Mule flow shown in the above image.
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="loggingexampleFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<logger level="DEBUG" category="com.mulemagic.demos.inboundlogs" message="Incoming message #[message.inboundProperties.'http.query.params']" doc:name="Logger" doc:description="This logger logs Inbound messages"/>
<set-payload value="#[message.inboundProperties.'http.query.params']" doc:name="Set Payload"/>
<logger level="DEBUG" category="com.mulemagic.demos.outboundlogs" message="Outgoing Message #[payload]" doc:name="Logger" doc:description="This logger logs Outbound messages"/>
</flow>
Lets run the application and from browser I am sending a header parameter ..
http://localhost:8081/?state=BC
Now once u run the application, check the log folder in the project base directory.
In the InboundLogs.log we can see
2015-06-23 18:54:14,241 DEBUG c.m.d.inboundlogs [[loggingexample].HTTP_Listener_Configuration.worker.01] Incoming message ParameterMap{[state=[BC]]}
And in OutboundLogs.log file we can see
2015-06-23 18:54:14,249 DEBUG c.m.d.outboundlogs [[loggingexample].HTTP_Listener_Configuration.worker.01] Outgoing Message ParameterMap{[state=[BC]]}
This is how we were able to successfully log Inbound and Outbound messages in respective files.
Really cool post, highly informative and professionally written and I am glad to be a visitor of this perfect blog, thank you for this rare info!
ReplyDeleteRegards mulesoft training in hyderabad
Thanks Nasreen
DeleteVery Good informative Article Mulesoft Online Training Hyderabad
ReplyDeleteThanks for your information and very good information. Keep posting.
ReplyDeletemulesoft training in bangalore
mulesoft online training hyderabad
ReplyDeleteThanks for delivering a good stuff, Explanation is good, Nice Article.
Mulesoft Training in Hyderabad
Mulesoft Training in Bangalore
Mulesoft Training Hyderabad
Best Mulesoft Online Training
Best Mulesoft Training
Mulesoft Course Online
Mulesoft Training and Certification
Mulesoft Architecture Certification
Mulesoft Course
Mulesoft Developer Certification
Mulesoft Self Learning
Mulesoft Online Training in India