The Mule runtime connects different applications and formats, both inside and outside the corporate network. Beyond the exchange of information, a solid logging policy is essential for detecting errors and security threats early and for supporting audits and debugging.
Since version 3.6, Mule has used the Apache Log4j2 logging system, the successor to the popular and influential Log4j library that has been in use across the Java ecosystem for many years. Version 2 builds on the logging capabilities of its predecessor and includes significant improvements, particularly in performance.
The following sections show how to configure and implement log messages within a Mule ESB service.
Mule ESB Flow Logger
The <logger /> logging component lets you log payload and metadata during processing. With the log level, you set the logging for the Mule ESB application in a targeted way, according to need.
<logger message="Processing message with id #[message.id] and payload #[payload]" level="INFO" doc:name="Logger"/>Mule ESB Java Logger
Custom components get access to the logger through the LogFactory and can use it to emit log messages.
Log logger = LogFactory.getLog(getClass());
if (logger.isInfoEnabled())
{
logger.info("Processing event received.");
}Mule ESB Log4j2 Configuration
The logging behavior of a Mule ESB service is defined in the log4j2.xml configuration file. The basic configuration specifies the logging system, log pattern, loggers, and log levels.
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<Appenders>
<RollingFile name="file" fileName="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}service-experience-api.log"
filePattern="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}service-experience-api-%i.log">
<PatternLayout pattern="%d [%t] %-5p %c - %m%n" />
<SizeBasedTriggeringPolicy size="10 MB" />
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</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="file" />
</AsyncRoot>
</Loggers>
</Configuration>Conclusion
Mule ESB runtime components let developers apply logging of business or technical information during processing in a targeted way. The Mule logging framework is available to custom components as well. By using asynchronous loggers, Log4j2 achieves very high throughput even with many parallel log messages. Because the Log4j2 log output is standardized, Mule servers can be connected to log management systems. Thanks to Log4j2, Mule ESB as an integration solution fits into a company’s logging policies.