jueves, 4 de septiembre de 2014

Mule logging practices using log4j

This is a very short introduction to use mule with log4j practices to identify and trace messages within the flows when running Mule on embedded and standalone modes.

1.- Use logger component in Mule by specifying the "category" attribute.
The "category" attribute may refer to either an use case, or a scenario or just the flow itself. This "category" attribute will be used afterwards to mark what you want to see in your custom traces.

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

<mule xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
version="CE-3.5.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">

<flow name="testFlow">
<logger level="INFO" message="This is a message in logs"
category="test.flow.yourkey"/>
<flow-ref name="DoSomething" />
</flow>

</mule>

When compiling and publishing into Mule, this file is going to be copied to [MULE HOME]/apps/[YOUR MULE APP] transparently.

2.- Setup your log4j configuration file in [YOUR MULE APP]/src/main/resources.

When compiling and publishing into Mule, this file is going to be copied to [MULE HOME]/apps/[YOUR MULE APP] transparently.

3.- Add your appender by specifying the proper path for mule logs using ${mule.home}.

The appender is going to specify the traces file path that you want to have afterwards.

For example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="false">

    <appender name="keyFileAppender" class="org.apache.log4j.RollingFileAppender">
        <param name="file" value="${mule.home}/logs/key.log" />
        <param name="maxBackupIndex" value="5" />
        <param name="maxFileSize" value="5MB" />
        <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="[%d{ISO8601}] [%t] [%-5p][%c:] %m%n" />
        </layout>
    </appender>
    ...

</log4j:configuration>

4.- Run your MULE app:

When you run Mule as embedded mode in Eclipse IDE, the ${mule.home} will be "\", so your traces will placed in c:\logs\... for windows, or \user\logs for unix. On the other hand, when you run Mule as standalone, the ${mule.home} will be properly specified and will match with the path of your Mule installation, so your traces will be placed in [MULE HOME]/logs/... as the other trace files in Mule.

How to check the proper behavior of your Mule app?

Normally, all traces are displayed to mule.log file, so this file may became messy if you are running or performing many flows. Therefore, if you want to see only your traces:

a) Add your custom logger in the log4j configuration file.
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="false">

    <appender name="keyFileAppender" class="org.apache.log4j.RollingFileAppender">
        <param name="file" value="${mule.home}/logs/key.log" />
        <param name="maxBackupIndex" value="5" />
        <param name="maxFileSize" value="5MB" />
        <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="[%d{ISO8601}] [%t] [%-5p][%c:] %m%n" />
        </layout>
    </appender>

    <appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="[%d{ISO8601}] [%t] %m%n" />
        </layout>
    </appender>

    <root>
        <priority value="INFO" />
        <appender-ref ref="keyFileAppender" />
        <appender-ref ref="consoleAppender" />
    </root>

    <logger name="test.flow.yourkey">
        <level value="TRACE" />
        <appender-ref ref="keyFileAppender" />
        <appender-ref ref="consoleAppender" />
    </logger>

</log4j:configuration>

The name of this new logger must match with the "category" attribute in logger component in the flows you want to see. If you restart your mule installation, then you will see all traces taking into consideration the level in the file specified in the appender.

Moreover, if you specify the additivity="false", these traces will not be displayed into the root level as well.

No hay comentarios:

Publicar un comentario