While working on MuleSoft I realized that sometimes it's a requirement whereby one needs to log various types of inbound and outbound messages in different log files. This is really helpful if you want to track huge loads of messages coming in and going out of your MuleSoft 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 the following simple example we will see how the flow logs Inbound and Outbound messages in InboundMessage.log and OutboundMessage.log files.

[ field_blog_inline_images:0|Left ]

It's a simple flow as shown in the 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 the huge MuleSoft library, but the basic configuration remains the same.

[ field_blog_inline_images:1|Left ]

Note: I am using mule 3.6.1 here so I am using Log4J2, but feel free to use mule Log4J with previous versions of MuleSoft.
Following is my Log4j config file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
         <File name="TraceFile" fileName="logs/appTrace.log">
                        <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
         <!-- file for inbound endpoint -->
         <File name="inboundLogs" fileName="logs/inboundLogs.log">
                      <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
        <!-- file for outbound endpoint -->
        <File name="outboundLogs" fileName="logs/outboundLogs.log">
                       <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
        <!-- Jusgt for the Trace -->
        <Root level="trace">
               <AppenderRef ref="TraceFile" level="trace" />
       <!-- Assign logger for inbound messages -->
       <Logger name="com.mulemagic.demos.inboundlogs" level="debug">
                <AppenderRef ref="inboundLogs" level="debug" />
       <!-- Assign logger for outbound messages -->
       <Logger name="com.mulemagic.demos.outboundlogs" level="debug">
                <AppenderRef ref="outboundLogs" level="debug" />
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 a 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 let's tie the inbound and outbound messages to these unique <Logger> in our mule config files. Following is the exact MuleSoft flow shown in the above image.
<http:listener-config name="HTTP_Listener_Configuration" host="" 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"/>
Let's run the application and from the browser I'm sending a header parameter:
Now once you run the application, check the log folder in the project base directory.

[ field_blog_inline_images:2|Left ]

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.



Read Next
Technovation: Top Career Tips from our Mentors

Technovation: Top Career Tips from our Mentors

13 April, 2016|5 min
Get in touch and let’s get started.

We are a full service digital consultancy with experience and capacity to meet the needs of even the largest most complex of organizations in the world. Our services portfolio enables us to offer clients the best of experiences when working with our teams so as to make sure we keep the focus on their needs, customers and delivering tangible value to the business. End to end services; endless ideas. 

Three easy ways to get in touch. Complete the form to request a consultation, email us your inquiry, specs and/or RFP to contact@appnovation.com OR call one of our many, global office locations and tell us how we can help.