Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Splitting Log Files Based on Log Levels With Mule/Java/J2EE

DZone's Guide to

Splitting Log Files Based on Log Levels With Mule/Java/J2EE

Learn how to split up log files in your Mule project based on log levels, such as debug and error, with this quick tutorial.

· Integration Zone
Free Resource

Share, secure, distribute, control, and monetize your APIs with the platform built with performance, time-to-value, and growth in mind. Free 90-day trial of 3Scale by Red Hat

In this article, I will show you how to edit the Log4j configuration file of a Mule project to split the logs into multiple files based on log levels. The same can also be achieved in JAVA/J2EE.

In this example, we will split the log files for two log levels (debug, error).

Tools Used: Anypoint Studio version 6.2.3 for Windows.

Let's Start

Step 1: Inside the Mule project -> src/main/resources. Double click on log4j2.xml file to open in Anypoint Studio.

log4j2.xml

Step 2: Create multiple RollingFile configs within the Apenders tag. I will create two for this example, as we are going to generate only two log files. You can create as many as needed per the log levels.

Also, you can name the Rolling file config as per your needs; for this, I will be using "debugLogFile" and "errorLogFile."

<RollingFile name="debugLogFile" 
             fileName="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}${applicationName}-debugLogFile.log" 
             filePattern="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}archive${sys:file.separator}${applicationName}-debugLogFile-%d{yyyy-MM-dd-HH:mm:ss:SSS}.log.gz">
 <RollingFile name="errorLogFile" 
              fileName="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}${applicationName}-errorLogFile.log" 
              filePattern="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}archive${sys:file.separator}${applicationName}-errorLogFile-%d{yyyy-MM-dd-HH:mm:ss:SSS}.log.gz">

Step 3: Add Filters within the Rolling file to restrict the logs flowing into a particular log file.

<Filters>
<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL" />
<ThresholdFilter level="FATAL" onMatch="DENY" onMismatch="NEUTRAL" />
</Filters>

Step 4: Control logging by using a reference to the correct Rolling file.

<AsyncLogger name="org.mule" level="error">
<AppenderRef ref="errorLogFile" level="error"/>
</AsyncLogger>

<AsyncLogger name="com.mulesoft" level="debug">
<AppenderRef ref="debugLogFile" level="debug"/>
</AsyncLogger>

Voila! Your multiple logging solution is ready. 

Here is the complete log4j2.xml code:

<?xml version="1.0" encoding="utf-8"?>
<Configuration>
    <Properties>
      <Property name="applicationName">split-log-file</Property>
    </Properties>

    <Appenders>
      <RollingFile name="debugLogFile" fileName="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}${applicationName}-debugLogFile.log" 
                   filePattern="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}archive${sys:file.separator}${applicationName}-debugLogFile-%d{yyyy-MM-dd-HH:mm:ss:SSS}.log.gz">
        <PatternLayout pattern="%d [%t] %-5p %c - %m%n" />
        <SizeBasedTriggeringPolicy size="50 MB"/>
        <Filters>
        <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL" />
            <ThresholdFilter level="FATAL" onMatch="DENY" onMismatch="NEUTRAL" />
        </Filters>
      </RollingFile>
      <RollingFile name="errorLogFile" fileName="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}${applicationName}-errorLogFile.log" 
                   filePattern="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}archive${sys:file.separator}${applicationName}-errorLogFile-%d{yyyy-MM-dd-HH:mm:ss:SSS}.log.gz">
        <PatternLayout pattern="%d [%t] %-5p %c - %m%n" />
        <SizeBasedTriggeringPolicy size="10 MB"/>
      </RollingFile>
    </Appenders>

    <Loggers>
        <!-- CXF is used heavily by Mule for web services -->
        <AsyncLogger name="org.apache.cxf" level="WARN">
        <AppenderRef ref="errorLogFile" level="ERROR"/>
        </AsyncLogger>

        <!-- Apache Commons tend to make a lot of noise which can clutter the log-->
        <AsyncLogger name="org.apache" level="WARN">
        <AppenderRef ref="errorLogFile" level="ERROR"/>
        </AsyncLogger>

        <!-- Reduce startup noise -->
        <AsyncLogger name="org.springframework.beans.factory" level="WARN">
        <AppenderRef ref="errorLogFile" level="ERROR"/>
        </AsyncLogger>

        <!-- Mule classes -->
        <AsyncLogger name="org.mule" level="debug">
        <AppenderRef ref="errorLogFile" level="ERROR"/>
        </AsyncLogger>
        <AsyncLogger name="com.mulesoft" level="debug">
        <AppenderRef ref="errorLogFile" level="ERROR"/>
        </AsyncLogger>
<AsyncLogger name="org.mule.module.http*" level="WARN">
<AppenderRef ref="errorLogFile" level="ERROR"/>
</AsyncLogger>

        <!-- Reduce DM verbosity -->
        <AsyncLogger name="org.jetel" level="WARN">
        <AppenderRef ref="errorLogFile" level="ERROR"/>
        </AsyncLogger>
        <AsyncLogger name="Tracking" level="WARN">
        <AppenderRef ref="errorLogFile" level="ERROR"/>
        </AsyncLogger>

<!-- Sending normal debug logs to debugLogFile -->
        <AsyncRoot level="debug">
            <AppenderRef ref="debugLogFile" />
        </AsyncRoot>
    </Loggers>

</Configuration>

Now, Run your project and go to Workspace -> .mule -> logs folder. You will see two log files created based on the log levels and as per the name you configured.

Image title

Let's call it a day!

Explore the core elements of owning an API strategy and best practices for effective API programs. Download the API Owner's Manual, brought to you by 3Scale by Red Hat

Topics:
mule ,java ,j2ee ,mulesoft ,logging ,integration ,tutorial

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}