DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

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.

Akil Shadab user avatar by
Akil Shadab
·
May. 28, 17 · Tutorial
Like (4)
Save
Tweet
Share
6.83K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Brief Overview of the Spring Cloud Framework
  • How Observability Is Redefining Developer Roles
  • Fraud Detection With Apache Kafka, KSQL, and Apache Flink
  • Microservices Discovery With Eureka

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: