Mule 3.1 introduces a very useful new <logger> element that makes it easy to inspect the content and properties of your messages in Mule while building or debugging a flow. It’s also perfect for logging errors, info messages etc. Mule has always supported logging with the <log-component> but while working with the new orchestration capabilities of Mule 3 flows, we found a real need for fine-grained logging. With the new message processor architecture, this was a snap and we were able to put together the new functionality in an afternoon. In this blog post I’ll take you on a quick tour of what you can do with it with some examples.
Let’s start with the basics and work through some examples:
Where can I use <logger>?
- Anywhere in a <flow>
- Within inbound and outbound endpoint elements (or within their optional <response> elements)
- Within the <inbound> section of a <service> after the endpoints.
- Within the <async-reply> section of a <service> after the endpoints.
How do I log a message at a certain point in my flow or service?
This is done by simply adding the <logger> element where you require it, with the message you want to log:
This will log the message specified using the default DEBUG level, so you will see this in your log only if you have your log level set at DEBUG or TRACE level.
What if I want to escalate the severity of the message?
Simply add the ‘level’ attribute, if you are using the Mule IDE editor, you will see a list of the valid options: ERROR, WARN, INFO, DEBUG and TRACE.
The above example uses the wire tap configured with an expression filter to log our message using the ERROR level only when the incoming payload does not contain a certain string. Using an expression filter like this allows you to generate a log message only for specific conditions.
Thats great, but how can log specific information rather than the whole MuleMessage?
In order to log your message payload, headers or a single value from within your payload <logger> leverages Mule Expressions. Rather than specifying a single expression for logging you can embed as many expressions as you required in your log ‘message’ as required. This allows you to both give some context to whats being logged as well as enables you to log multiple things at once.
This is just a very simple example, by embedding Mule Expressions there is no limit to what you can output in your log message. For example, if the message payload is XML, you could use an XPath expression to log a specific element of the message. Make sure you take a look at the expression reference guide.
Can I use my own log Category rather than the default org.mule.api.processor.LoggerMessageProcessor?
Yes, just configure the category you would like used with the ‘category’ attribute.
The <logger> can be used today by downloading Mule 3.1.0-RC2. I hope that you find it useful. Please provide feedback on your use cases for <logger> and/or improvement requests either via blog comments or by opening a JIRA.