Mule logger element

0

Logger element

Below we specify simplest logger element. This will use the default values for category and the message to be logged. Default category of logger would be LoggerMessageProcessor. Default logger level would be debug. Message would be the mule message of the event containing all the payload as well as all other properties of the message.

<logger/>

The above logger message processor will translate to the below java code.

Log logger = LogFactory.getLog(LoggerMessageProcessor.class);
if (logger.isDebugEnabled()) {
    logger.debug(event.getMessage());
}

Message

One can also pass custom message using message attribute. Any expression included in message will be parsed first and then logged.

<logger level="INFO" message="Payload is #[payload]" />

Category and level

By default category is LoggerMessageProcessor. The category can be changed using category attribute. We can also specify the log level. Below we have logged the message at warning level.

<logger level="WARN" category="LoggerTest" />

Nested logger element

Logger element can be also be added at any nested level. Below we use it within the foreach element.

<foreach collection="#[['one' : '1', 'two':'2']]">
			<logger level="ERROR" message="message-#[counter]: #[payload]" category="LoggerNestedTest" />
			<vm:outbound-endpoint path="out" />
</foreach>

Mule context with logger element

Here is the complete mule context with the logger elements.

muleContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:spring="http://www.springframework.org/schema/beans" xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
	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/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd">
	<flow name="loggerWithoutMsg">
		<vm:inbound-endpoint path="in2" />
		<logger level="WARN" category="LoggerTest" />
		<vm:outbound-endpoint path="out" />
	</flow>
	<flow name="loggerWithMsg">
		<vm:inbound-endpoint path="in1" />
		<logger level="INFO" message="Payload is #[payload]" />
		<vm:outbound-endpoint path="out" />
	</flow>
	<flow name="loggerNested">
		<vm:inbound-endpoint path="in3"/>
		<foreach collection="#[['one' : '1', 'two':'2']]">
			<logger level="ERROR" message="message-#[counter]: #[payload]" category="LoggerNestedTest" />
			<vm:outbound-endpoint path="out" />
		</foreach>
	</flow>
</mule>

In order to trigger the flows, we send messages to each of the inbound endpoints in1, in2 and in3.

MuleLoggerExample:

package com.javarticles.mule;

import java.io.IOException;

import org.mule.api.MuleContext;
import org.mule.api.MuleException;
import org.mule.api.context.MuleContextBuilder;
import org.mule.api.context.MuleContextFactory;
import org.mule.config.DefaultMuleConfiguration;
import org.mule.config.spring.SpringXmlConfigurationBuilder;
import org.mule.context.DefaultMuleContextBuilder;
import org.mule.context.DefaultMuleContextFactory;
import org.mule.module.client.MuleClient;

public class MuleLoggerExample {
    public static void main(String[] args)
            throws IOException, MuleException, InterruptedException {
        DefaultMuleConfiguration dmc = new DefaultMuleConfiguration();
        dmc.setId("muleexample");
        dmc.setWorkingDirectory("/esb/mule");
        SpringXmlConfigurationBuilder configBuilder = new SpringXmlConfigurationBuilder(
                "muleContext.xml");
        MuleContextBuilder contextBuilder = new DefaultMuleContextBuilder();
        contextBuilder.setMuleConfiguration(dmc);
        MuleContextFactory contextFactory = new DefaultMuleContextFactory();
        MuleContext ctx = contextFactory.createMuleContext(configBuilder,
                contextBuilder);
        ctx.start();
        try {
            MuleClient muleClient = new MuleClient(ctx);
            muleClient.dispatch("vm://in1", "Test1", null);
            muleClient.dispatch("vm://in2", "Test2", null);
            muleClient.dispatch("vm://in3", "Test3", null);
            Thread.sleep(3000);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ctx.dispose();
        }
    }
}

Logs the message within the event. The message is logged in based on the category LoggerTest passed in.

Output:

[WARN ] 2018-05-04 22:23:06.840 [loggerWithoutMsg.stage1.02] LoggerTest - 
org.mule.DefaultMuleMessage
{
  id=9db89b00-4fbb-11e8-8e41-a0afbd832eee
  payload=java.lang.String
  correlationId=
  correlationGroup=-1
  correlationSeq=-1
  encoding=UTF-8
  exceptionPayload=

Message properties:
  INVOCATION scoped properties:
  INBOUND scoped properties:
    MULE_ENDPOINT=vm://in2
    MULE_ORIGINATING_ENDPOINT=endpoint.vm.in2
    MULE_SESSION=rO0ABXNyACNvcmcubXVsZS5zZXNzaW9uLkRlZmF1bHRNdWxlU2Vzc2lvbi7rdtEW7GGKAwAFWgAFdmFsaWRMAA1mbG93Q29uc3RydWN0dAAmTG9yZy9tdWxlL2FwaS9jb25zdHJ1Y3QvRmxvd0NvbnN0cnVjdDtMAAJpZHQAEkxqYXZhL2xhbmcvU3RyaW5nO0wACnByb3BlcnRpZXN0AA9MamF2YS91dGlsL01hcDtMAA9zZWN1cml0eUNvbnRleHR0ACdMb3JnL211bGUvYXBpL3NlY3VyaXR5L1NlY3VyaXR5Q29udGV4dDt4cAFwdAAkOWRiODliMDEtNGZiYi0xMWU4LThlNDEtYTBhZmJkODMyZWVlc3IAJWphdmEudXRpbC5Db2xsZWN0aW9ucyRTeW5jaHJvbml6ZWRNYXAbc/kJS0s5ewMAAkwAAW1xAH4AA0wABW11dGV4dAASTGphdmEvbGFuZy9PYmplY3Q7eHBzcgAkb3JnLm11bGUudXRpbC5DYXNlSW5zZW5zaXRpdmVIYXNoTWFwndHZ72dFzgADAAB4cHcMP0AAAAAAABAAAAAAeHEAfgAJeHB4
  OUTBOUND scoped properties:
    MULE_CORRELATION_GROUP_SIZE=-1
    MULE_CORRELATION_SEQUENCE=-1
  SESSION scoped properties:
}

Logs the message passed in.

Output:

[INFO ] 2018-05-04 22:23:06.842 [loggerWithMsg.stage1.02] LoggerMessageProcessor - Payload is Test1

Logs the message within the foreach loop.

Output:

[ERROR] 2018-05-04 22:23:06.842 [loggerNested.stage1.02] LoggerNestedTest - message-1: 2
[ERROR] 2018-05-04 22:23:06.842 [loggerNested.stage1.02] LoggerNestedTest - message-2: 1

Download the source code

This was an example about mule <logger> element.

You can download the source code here: muleLoggerExample.zip
Share.

Comments are closed.