Entity view (Content)

Log Messages in Different Log Files

By charles
Apr. 5, 2016

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="">
<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 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="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>
 
Let's run the application and from the browser I'm sending a header parameter:
 
http://localhost:8081/?state=BC 
 
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.