DataWeave Logging — Print Logs From DataWeave2.0
Hi Muleys,
This article is about printing the logs directly in the output console(after deployment) without using an additional logger component.
We know how to print the logs using the logger component but to print specific values or fields directly from DataWeave/Transform Message component will be more helpful and Dev-Friendly as Developers can print the specific values what they want. It is useful to print only required information rather than printing the whole data which may contain sensitive or PII information.
Example: This example shows how to log the result of the CandidateInfo without changing any original values in the DataWeave.
DataWeave Code
xxxxxxxxxx
2.0
output application/json
var a = [{name: "Max", email: "abc@.xyzcom", phone: "91 0808907890", company: "MuleSoft"},{name: "Mule", email: "abc@xyz.com", phone: "91 0808907890", company: "MuleSoft"}]
---
a map
{
"CandidateName": $.name,
"Contact": $.email,
"Company": $.company,
"Info": log("CandidateInfo" , $.name ++ " is working at " ++ $.company)
}
Console Output
xxxxxxxxxx
INFO 2020-10-30 18:41:07,700 [[MuleRuntime].uber.02: [dwllogger].dwlloggerFlow2.CPU_INTENSIVE ] [processor: ; event: 5f43bb20-1ab1-11eb-8bbb-8c164585008e] org.mule.weave.v2.model.service.DefaultLoggingService$: CandidateInfo - "Max is working at MuleSoft"
INFO 2020-10-30 18:41:07,703 [[MuleRuntime].uber.02: [dwllogger].dwlloggerFlow2.CPU_INTENSIVE ] [processor: ; event: 5f43bb20-1ab1-11eb-8bbb-8c164585008e] org.mule.weave.v2.model.service.DefaultLoggingService$: CandidateInfo - "Mule is working at MuleSoft"
Expression Output
xxxxxxxxxx
[
{
"CandidateName": "Max",
"Contact": "abc@.xyzcom",
"Company": "MuleSoft",
"Info": "Max is working at MuleSoft"
},
{
"CandidateName": "Mule",
"Contact": "abc@xyz.com",
"Company": "MuleSoft",
"Info": "Mule is working at MuleSoft"
}
]
Conclusion
Using Log Function in DataWeave/Transform Message component, we filtered out the sensitive value and logged what we want, directly in the output Console.
Happy Learning!