MuleSoft MUnit: Logger Issues
Mule 4 uses DataWeave as its default expression language. That affects not only transformations, but also expressions embedded in components such as loggers.
A subtle issue can appear when a logger works correctly while running the application but fails during an MUnit test. The problem is caused by mixing literal text and an embedded expression in a way that MUnit evaluates differently.
Original logger
The following logger prints the HTTP request method correctly when the application runs:
<logger level="INFO" message="Request Method: #[attributes.method]" />
Application output
Request Method: GET
MUnit error
When the same code is executed in MUnit, the logger expression can fail with an expression parsing 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)
********************************************************************************
Rewrite the message as a DataWeave expression
Instead of combining literal text with an embedded expression, construct the complete logger message as one DataWeave expression:
<logger level="INFO" message="#['Request Method: ' ++ attributes.method]" />
Application output
Request Method: GET
MUnit output
Request Method: GET
Takeaway
When a Mule 4 logger behaves differently under MUnit, check whether the entire message is expressed as valid DataWeave. Building the message with DataWeave concatenation keeps evaluation consistent between the application runtime and the test.