Nav

Logger Component Reference

Use the Logger to log messages such as error messages, status notifications, or exceptions. You can add a logger anywhere in a flow, and you can configure it to log anything: any string, any Mule expression, or any combination of strings and Mule expressions.

Configuration

logger-icon-big

Properties:

logger

Field Value Description Example

Display Name

Logger

Customize to display a unique name for the logger in your application.

doc:name="Logger"

Message

String or Mule expression

Specify what Mule should log. By default, messages are logged to the console in Mule Studio.

message="Current payload is #[payload]"

Level

Select a level from the listed options:

  • ERROR

  • WARN

  • INFO

  • DEBUG

  • TRACE

Specify the level at which the message should be logged.

You can create this file automatically through Studio by right-clicking on your project in the package explorer, and selecting Mule > Create Log4j Configuration. Then you can find a log4j2.xml file in your project’s src/main/resources folder and edit it through Studio.

level="INFO"

Category

Optional . String

Optionally specify a category name and configure it in the log4j2.xml file to behave per your use case. For example, you can route log messages based on category or set log levels based on category.

category="MyCustomCategory"


    
            
         
1
2
3
4
5
# A logger set to monitor message processing status
<logger category="monitoring" message="Message #[payload.id] processed successfully" level="INFO" doc:name="Monitoring Logger"/>

# A logger set to record the processing time of a flow
<logger category="performance" message="Message #[payload.id] took #[flowVars['processingTime']] milliseconds to process" level="INFO" doc:name="Performance Logger"/>
Element Description

logger

Use the Logger to log messages such as error messages, status notifications, or exceptions to the application’s log file.

Element Attribute Description

message

Specify what Mule should log. Supports expressions.

level

Select one of the following levels: ERROR, WARN, INFO, DEBUG, or TRACE. If no level attribute is set, the logger logs at the INFO level.

category

Optional. Specify categories to route log entries according to business needs. Configure the categories in your log4j2.xml file.

doc:name

Customize to display a unique name for the logger in your application.

Note: Attribute not required in Mule Standalone configuration.

Rather than specifying a single Mule expression in your logger message, you can embed as many expressions as you required for your use case. This allows you to give some context to what is being logged, and enables you to log multiple things at once.

For example:


              
           
1
<logger message="Current payload is #[payload] and the message ID is #[message.id]" level="INFO" doc:name="Logger"/>
Keep in mind that the act of logging implies a toString() function, that in some cases may alter or consume the message.

Configuring Custom Logging Settings

By default, the logger is set to log messages asynchronously and at a level greater than or equal to INFO, and thus discards log messages at the DEBUG or TRACE level.

As from Mule runtime 3.6.0, log4j was replaced by log4j2 as the backend tool for managing loggings. This implies some backwards compatibility issues as the necessary configuration files in this new framework are different. Log4j2 also allows for asynchronous logging, which wasn’t previously available, and Mule now implements this by default, as it implies a substantial improvement in performance.

If you need to use synchronized logging, change where to send the logs, adjust the logging level, or define custom categories, you can configure these properties in an XML file that specifies how the logger behaves, otherwise, you are using the default properties.

For instructions on how to edit these settings, see Logging in Mule.

Logger Example

The following example displays the message in the Set Payload in a browser while logging the message to the Anypoint Studio console.

logger-flow


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" 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.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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="logger-example-Flow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <set-payload value="Hello MuleSoft!" doc:name="Set Payload"/>
        <logger message="#[message]" level="INFO" doc:name="Logger"/>
    </flow>
</mule>

See Also