MuleSoft MUnit : Logger Issues

If you are using Mule 4, then you might have noticed that DataWeave is the default expression language in Mule 4. DataWeave is very powerful. DataWeave 2.0 also features many improvements, which are covered in the DataWeave 2.0 section of Mule 4 documentation.

Have you tried running MUnit test cases with Mule 4? MUnits complain about loggers if they are not written as DataWeave expressions. Seriously? No documentation on this? I think it makes sense to update loggers and write them as DataWeave expressions. Let me give an example and explain whats going on with Mule 4 and Munits.

In my Mule4 code, I have following logger which will print the HTTP method value. This works without any issue when I run the application.

Original Code:

<logger level="INFO" message="Request Method: #[attributes.method]" />

Application Output:

Request Method: GET

But when I run MUnit, this logger throws an error.

MUnit Error:

********************************************************************************
Message : "Script 'Request Method: #[attributes.method] ' has errors:
Invalid input "Method: ", expected Namespace, Attribute<'@('(Name:Value)+')'>, ~ or Function Call (line 1, column 9):

1| Request Method: #[attributes.method]
^^^^^^^ at 1 : 1″ evaluating expression: “Request Method: #[attributes.method]”.
Error type : MULE:EXPRESSION
Element : logApiRequest/processors/2 @ mule-newton-api:common.xml:95
Element XML : <logger level=”INFO” message=”Request Method: #[attributes.method]”></logger>

(set debug level logging or ‘-Dmule.verbose.exceptions=true’ for everything)
********************************************************************************

If we update this logger using DataWeave expression as shown below, then MUnit works without any issues.

Updated Code:

<logger level="INFO" message="#['Request Method: ' ++ attributes.method]" />

Application Output:

Request Method: GET

MUnit Output:

Request Method: GET

 

1 thought on “MuleSoft MUnit : Logger Issues

  1. midniter Reply

    Hello Out of Memo,

    How about when you need to log the payload?

    Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *