When I use the log module to log something, the log message prints escape characters instead of interpreting it. For instance, consider the following code.
import ballerina/log;
public function main() {
json jsonObject = {
"name": "John Doe",
"age": 30,
"city": "New York"
};
log:printInfo("Hello this is your json object: " + jsonObject.toJsonString());
}
When I run the above code, it logs the following.
time = 2023-11-09T15:15:10.935+05:30 level = INFO module = org/test message = "Hello this is your json object: {\"name\":\"John Doe\", \"age\":30, \"city\":\"New York\"}"
How can I make the program log the line without escape characters?
Since the JSON object is passed to the message
parameter, it is converted to a JSON string, as the message
parameter expects to print a string as expected by the user. Since the use case is to print the JSON as it is (i.e., print the JSON as key-value pairs), the JSON parameter should be provided as a payload
parameter, as shown below.
import ballerina/log;
public function main() {
json jsonObject = {
"name": "John Doe",
"age": 30,
"city": "New York"
};
log:printInfo("Hello this is your json object", payload = jsonObject);
}
The above code generates the following output:
time=2024-01-08T11:53:30.462+05:30 level=INFO module="" message="Hello this is your json object" payload={"name":"John Doe","age":30,"city":"New York"}