- 4 minutes to read

Configuring Log4J

Unlock powerful, business-driven logging for your Mule ESB integrations with Nodinite and Log4J! This guide shows you how to configure Log4J for seamless log capture, filtering, and integration with Nodinite—empowering you to monitor, troubleshoot, and optimize your flows with confidence.

Mule messages and context are captured using the robust Log4J framework. This page describes the configuration steps required to enable advanced logging with Nodinite.

Tip

For even more flexibility and business value, consider using the Nodinite Logging Custom Connector instead of Log4J appenders.

The Log4J configuration file, log4j2.xml, is typically found in your Mule ESB project at:
Log4J_Studio
Default location of log4j2.xml in Mule ESB Studio.

You can also edit the Log4J configuration file after deployment. For example, it may be located here:
Log4J_Apps
Example of log4j2.xml location after deployment.

To filter and forward the right log files to Nodinite, add a custom appender <IMLogFileAppender> under the <Appenders> section.
The <Configuration> element must include the packages attribute set to se.integrationsoftware.integrationmanager.
Ensure the directory in the filePath attribute exists and that the Log-Agent service account has read/write permissions.

<?xml version="1.0" encoding="utf-8"?>
<Configuration packages="se.integrationsoftware.integrationmanager">
    <Appenders>
        <!--  IMLog File Appender -->
        <IMLogFileAppender name="file_IMLog" 
            filePath="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}IMLog${sys:file.separator}filetransfer-">
    		    		
            <ScriptFilter onMatch="ACCEPT" onMisMatch="DENY">
                <Script name="GroovyFilter" language="groovy"><![CDATA[
		            if (logEvent.getMessage().getFormattedMessage().contains("im_") ) {
		                return true;
		            }
		            return false;
                ]]></Script>
            </ScriptFilter>
    		
            <PatternLayout pattern="%d [%t] %-5p %c - %m%n" />
        </IMLogFileAppender>
    </Appenders>    
    <Loggers>
        <!-- Mule classes -->
        <AsyncLogger name="org.mule" level="INFO"/>
        <AsyncLogger name="com.mulesoft" level="INFO"/>
        
        <AsyncRoot level="INFO">
            <AppenderRef ref="file_IMLog" />
        </AsyncRoot>
    </Loggers>
</Configuration>

The class se.integrationsoftware.integrationmanager.IMLogFileAppender must also be included in your project.

JavaPackage_Studio
Java package structure for IMLogFileAppender class.

package se.integrationsoftware.integrationmanager;

import java.io.FileOutputStream;
import java.io.Serializable;
import java.util.UUID;
import java.util.concurrent.locks.*;
import org.apache.logging.log4j.core.*;
import org.apache.logging.log4j.core.appender.*;
import org.apache.logging.log4j.core.config.plugins.*;
import org.apache.logging.log4j.core.layout.PatternLayout;

@Plugin(name="IMLogFileAppender", category="Core", elementType="appender", printObject=true)
public final class IMLogFileAppender extends AbstractAppender {

    private static final long serialVersionUID = -2207607975964605180L;
	
    private final ReadWriteLock rwLock = new ReentrantReadWriteLock();
    private final Lock readLock = rwLock.readLock();

    private String filePath = "";

    private static long sequenceNumber = 0;
    
    protected IMLogFileAppender(String name, Filter filter, Layout<? extends Serializable> layout, final boolean ignoreExceptions) {
        super(name, filter, layout, ignoreExceptions);
    }

    protected IMLogFileAppender(String name, Filter filter, Layout<? extends Serializable> layout, final boolean ignoreExceptions, String filePath) {
        super(name, filter, layout, ignoreExceptions);
        
        this.filePath = filePath;
    }
    
    @Override
    public void append(LogEvent event) {
        readLock.lock();
        
        try {	
            final byte[] bytes = getLayout().toByteArray(event);
            
            UUID uuid = UUID.randomUUID();
            FileOutputStream fos = new FileOutputStream(filePath + String.format("%019d", sequenceNumber++) + "-" + uuid.toString() + ".log");
            fos.write(bytes);
            fos.close();           
        } catch (Exception ex) {
            if (!ignoreExceptions()) {
                throw new AppenderLoggingException(ex);
            }
        } finally {
            readLock.unlock();
        }
    }

    @PluginFactory
    public static IMLogFileAppender createAppender(
            @PluginAttribute("name") String name,
            @PluginElement("Layout") Layout<? extends Serializable> layout,
            @PluginElement("Filter") final Filter filter,
            @PluginAttribute("filePath") String filePath) {
    	
        if (name == null) {
            LOGGER.error("No name provided for IMLogFileAppender");
            return null;
        }
        
        if (layout == null) {	
            layout = PatternLayout.createDefaultLayout();
        }
        
        return new IMLogFileAppender(name, filter, layout, true, filePath);
    }
}

Next Step

Business Events
Logger