Output of the mongod command is in JSON. How to make it more readable?

In the very first lecture of “Chapter 1: The Mongod”, the output of the mongod command shown in the video was pretty readable. But in my local machine, when I ran this command mongod --port 27018 --dbpath ./Desktop/data/db/I get the below output.
{“t”:{"$date":“2020-11-24T21:43:26.604+05:30”},“s”:“I”, “c”:“CONTROL”, “id”:23285, “ctx”:“main”,“msg”:“Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols ‘none’”}
{“t”:{"$date":“2020-11-24T21:43:26.609+05:30”},“s”:“W”, “c”:“ASIO”, “id”:22601, “ctx”:“main”,“msg”:“No TransportLayer configured during NetworkInterface startup”}
{“t”:{"$date":“2020-11-24T21:43:26.609+05:30”},“s”:“I”, “c”:“NETWORK”, “id”:4648602, “ctx”:“main”,“msg”:“Implicit TCP FastOpen in use.”}
{“t”:{"$date":“2020-11-24T21:43:26.609+05:30”},“s”:“I”, “c”:“STORAGE”, “id”:4615611, “ctx”:“initandlisten”,“msg”:“MongoDB starting”,“attr”:{“pid”:4028,“port”:27018,“dbPath”:"./Desktop/data/db/",“architecture”:“64-bit”,“host”:“Rahuls-MacBook-Air.local”}}

I am using macOS. How can I make this more readable?

In https://docs.mongodb.com/manual/reference/log-messages/ you can find the following:

Starting in MongoDB 4.4, mongod / mongos instances output all log messages in structured JSON format. Log entries are written as a series of key-value pairs, where each key indicates a log message field type, such as “severity”, and each corresponding value records the associated logging information for that field type, such as “informational”. Previously, log entries were output as plaintext.

The very nice thing is that you may play with the logs with utilities like https://stedolan.github.io/jq/ or even load them in mongodb itself and use normal query or aggregations. If you store the log in a file you may even read it with firefox to get something like

2 Likes

Something like this:

<your command> | jq

jq formats that into json.

2 Likes

Thank you @steevej-1495 and @Santiago_Miranda for the help. I have installed jq (using Homebrew). Now I’m using mongod | jq command. It produces the below output.
{
“t”: {
“$date”: “2020-11-25T00:13:28.150+05:30”
},
“s”: “I”,
“c”: “CONTROL”,
“id”: 23285,
“ctx”: “main”,
“msg”: “Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols ‘none’”
}
{
“t”: {
“$date”: “2020-11-25T00:13:28.154+05:30”
},
“s”: “W”,
“c”: “ASIO”,
“id”: 22601,
“ctx”: “main”,
“msg”: “No TransportLayer configured during NetworkInterface startup”
}

Readability of this output is definitely better than the previous one. And as @steevej-1495 has mentioned, I can also store the log in a file and open it in Firefox.

1 Like