In Mule 4, to log a variable, the following approach can be taken:
<logger level="INFO" doc:name="My Var" message="#[vars.myVar]" category="values"/>
The expression inside #[ ] is an expression uses the DataWeave Expression Language.
To create more complex expressions to log variables, the function write() can be used:
<logger level="INFO" doc:name="My Var" message="#[' myVar = $(write(vars.myVar))']" category="values"/>
This will provide logging output as demonstrated:
org.mule.runtime.core.internal.processor.LoggerMessageProcessor: myVar = "the value" as String {class: "java.lang.String";, encoding: "UTF-8", mimeType: "application/java", raw: "the value" as String {class: "java.lang.String";}}
Note the following:
The write function prints the variable myVar and its metadata (encoding, mime type, ray vaule, Java class)
The expression inside the #[ ] is a String. When we want to evaluate an expression inside a String we use $( ). For example this String: '$(1 + 1)' evaluates to '2'