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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

How are you handling the data revolution? We want your take on what's real, what's hype, and what's next in the world of data engineering.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Serverless vs Containers: Choosing the Right Architecture for Your Application
  • Before You Microservice Everything, Read This
  • Power BI Embedded Analytics — Part 3: Power BI Embedded Demo
  • DGS GraphQL and Spring Boot

Trending

  • Zero-Trust AI: Applying Cybersecurity Best Practices to AI Model Development
  • Parallel Data Conflict Resolution in Enterprise Workflows: Pessimistic vs. Optimistic Locking at Scale
  • Containerizing AI: Hands-On Guide to Deploying ML Models With Docker and Kubernetes
  • The Evolution of Software Integration: How MCP Is Reshaping AI Development Beyond Traditional APIs

Mule 3.9: Separate Mule Info and Error Logs via Log4J2 Configuration

In this article, I will explain how to separate the Mule application info and error logs into a separate log file.

By 
Enrico Rafols Dela Cruz user avatar
Enrico Rafols Dela Cruz
·
Updated Aug. 31, 18 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
15.9K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, I will explain how to separate the Mule application info and error logs into a separate log file. For instance, we have a Mule application that runs every 10 seconds to read an input file located on the local path /appdata/temp/input/persons.csv.

Image title

LOG4J2.XML Configuration

To separate the Mule INFO and ERROR logs, we need to define 2 separate RollingRandomAccessFile tags under Appenders in our log4j2.xml file located in src/main/resources. One for INFO logs and another for ERROR logs. We can set the filename of the log file as well as the log category (INFO, ERROR, WARN, DEBUG) that the file should contain which can be configured using ThresholdFilter Level. Lastly, we need to add the two RollingRandonAccessFile configuration to the loggers tag under AsyncRoot.

<?xml version="1.0" encoding="utf-8"?>
<Configuration>
    <Properties>
<Property name="baseDir">${sys:mule.home}${sys:file.separator}logs${sys:file.separator}${sys:hostName}</Property>
<Property name="infoFileName">mule-sample-interface-info</Property>
<Property name="errorFileName">mule-sample-interface-error</Property>
</Properties>


    <Appenders>
<RollingRandomAccessFile name="info_file"
fileName="${baseDir}-${infoFileName}.log" filePattern="${baseDir}-${infoFileName}-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="%d [%t] ${sys:hostName} %-5p %c - %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="30 MB" />
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<Filters>
<ThresholdFilter level="INFO" />
</Filters>
</RollingRandomAccessFile>
<RollingRandomAccessFile name="error_file"
fileName="${baseDir}-${errorFileName}.log" filePattern="${baseDir}-${errorFileName}-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="%d [%t] ${sys:hostName} %-5p %c - %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="30 MB" />
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
<Filters>
<ThresholdFilter level="ERROR" />
</Filters>
</RollingRandomAccessFile>
</Appenders>

    <Loggers>    
<!-- Http Logger shows wire traffic on DEBUG -->
<AsyncLogger name="org.mule.module.http.internal.HttpMessageLogger" level="WARN"/>

<!-- JDBC Logger shows queries and parameters values on DEBUG -->
<AsyncLogger name="com.mulesoft.mule.transport.jdbc" level="WARN"/>

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

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

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

        <!-- Mule classes -->
        <AsyncLogger name="org.mule" level="INFO"/>
        <AsyncLogger name="com.mulesoft" level="INFO"/>

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

        <AsyncRoot level="INFO">
            <AppenderRef ref="info_file" />
            <AppenderRef ref="error_file" />   
        </AsyncRoot>
    </Loggers>
</Configuration>

Mule Application Code

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

<mule xmlns:validation="http://www.mulesoft.org/schema/mule/validation" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"
xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/validation http://www.mulesoft.org/schema/mule/validation/current/mule-validation.xsd">
    <file:connector name="FILE_INBOUND_CONNECTOR_CONFIG" autoDelete="false" streaming="true" validateConnections="true"   doc:name="File" fileAge="30000"/>
    <validation:config name="Validation_Configuration" doc:name="Validation Configuration"/>
    <flow name="mule-sample-interfaceFlow">
        <poll doc:name="Run every 10 seconds">
            <fixed-frequency-scheduler frequency="10000"/>
            <logger message="#['\n'+message.rootId] - Start Of The Process" level="INFO" doc:name="Logger Start"/>
        </poll>
        <message-properties-transformer scope="invocation" doc:name="Message Properties Set Path and Filename Variable">
            <add-message-property key="targetPath" value="/appdata/temp/input/"/>
            <add-message-property key="targetFilename" value="persons"/>
        </message-properties-transformer>
               <scripting:transformer doc:name="Groovy Read File">
            <scripting:script engine="Groovy"><![CDATA[def filenameWildcard = flowVars.targetFilename + "*"
def endpointBuilder = muleContext.endpointFactory.getEndpointBuilder('file://'+flowVars.targetPath+'?connector=FILE_INBOUND_CONNECTOR_CONFIG')
endpointBuilder.addMessageProcessor(new org.mule.routing.MessageFilter(new org.mule.transport.file.filters.FilenameWildcardFilter(filenameWildcard)))
def inboundEndpoint = endpointBuilder.buildInboundEndpoint()  
inboundEndpoint.request(60000)]]></scripting:script>
        </scripting:transformer>
        <validation:is-not-null config-ref="Validation_Configuration" message="No Input File on the source path." value="#[payload]" doc:name="Validation"/>
        <byte-array-to-string-transformer doc:name="Byte Array to String"/>
        <logger message="#['\nContent:\n'+payload]" level="INFO" doc:name="Logger File Content"/>
        <logger message="#['\n'+message.rootId] - End Of The Process" level="INFO" doc:name="Logger End"/>
    </flow>
</mule>

Test Result

The Mule Application was deployed and ran as expected.

Image title

Image title

2 log files were generated after the deployment of the Mule application.

Image title

The mule-sample-interface-info only contains info logs of the Mule application.

Image title

The mule-sample-interface-error has no error logs since there's no error occurring during the Mule application execution.

Image title

Removing the input file should cause an error and that will be logged in the mule-sample-interface-error file.

Image title

Image title

Image title

application

Opinions expressed by DZone contributors are their own.

Related

  • Serverless vs Containers: Choosing the Right Architecture for Your Application
  • Before You Microservice Everything, Read This
  • Power BI Embedded Analytics — Part 3: Power BI Embedded Demo
  • DGS GraphQL and Spring Boot

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: