Get hold of Mule ESB

Top Trending Posts

#6. Mule Logger Component

“Logs are like car insurance. Nobody wants to pay for it, but when something goes wrong everyone wants the best available” – Pablo Kraan

Hilarious, but true!

Logging is an essential part of any application development. It includes logging various type of messages such as error messages, status notifications, exceptions, etc. In Mule, a logger can be added anywhere in a flow, and it can be configured to log anything: any string, any Mule expression, or any combination of strings and Mule expressions.

Let’s first create a variable. Open the Anypoint studio(or Eclipse) and then the config xml file. Create a flow and drag a Logger component into it. We’ll take the following setup as an example:

In the above flow, there is one HTTP connector(a listener), one logger component. Using above flow we’ll learn how to configure the flow in a way that whatever value is given to the logger, will be printed on the console of the IDE.

This is configuration window for the logger component with the following parameters:

Field

Value

Description

XML

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=”This is logger Component”

Level

Select a level from the listed options:

ERROR

WARN

INFO

DEBUG

TRACE

Specify the level at which the message should be logged.By default, Mule Studio will not log messages at the DEBUG or TRACE level to the console unless you create and configure a log4j.properties file in classpath likr src/main/resources to lower the log level.

level=”ERROR”

Category

Optional String

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

The xml for the component looks like: <loggermessage="This is logger Component"level="ERROR"doc:name="Logger"/>

After the execution of this flow (i.e. running the server, and hitting the port, on which the http listener is configured, by a web-browser), it will show “This is logger Component” message on the console.
The logger can hold any user defined value like a Boolean, a String, an Integer, or values directly extracted from mule message or payload like #[message.payload] or #[message.inboundProperties.’http.query.params’.configID].

For Logger Component, it allstarts with the method process of LoggerMessageProcessor class. It is used to logs the current element of a value evaluated from it using an expression evaluator.
By default the current messages is logged using the DEBUG level to the ‘org.mule.api.processor.LoggerMessageProcessor’ category. The level and category can both be configured to suit your needs.LoggerMessageProcessor implements an interface MessageProcessor which is used to Processes Mule Events. Implementations that do not mutate the Mule Event or pass it on to another MessageProcessor should return the Mule Event they receive. Implementation of method is given below:

In this, log(event) gets called. This method fetches log level of the logger and according to that it prints the log. Supported log levels by logger component are:

ERROR

Error is used to log all unhandled exceptions. This is typically logged inside a catch block at the boundary of your application.

WARN

Warning is often used for handled ‘exceptions’ or other important log events. For example, if your application requires a configuration setting but has a default in case the setting is missing, then the warning level could be used to log the missing configuration setting.

INFO

This is default log level of logger component. The Information level is typically used to output information that is useful to the running and management of your system.

DEBUG

Designates fine-grained informational events that are most useful to debug an application.

TRACE

This level gives more detailed information than the DEBUG level and sits on top of the hierarchy.

In this, error(Object) method gets invoked of SLF4JLocationAwareLog class. This method Converts the input parameter to String and then delegates to the wrapped org.slf4j.Logger instance. Implementation of the method is given below: