Format "getLog" and other Relaxed Extended JSON CLI output

Hello all -
In the M103 course, the db.adminCommand({“getLog”: “global”}) command is covered, but the output in the video looks different than the latest mongo output. The mongo docs say " Starting in MongoDB 4.4, getLog returns log data in escaped [Relaxed Extended JSON v2.0](…link about extended JSON format) format. Previously, log data was returned as plaintext."

Does anyone know ways in the mongo cli or easily-added other tooling to format this output for easier reading by the human eye? Something like .pretty() for json?

Hi Eric welcome to the community

In the MongoDB documentation for getLog it looks like you can use JQ to pretty pring the output of db.adminCommand({“getLog”: “global”}). It would look something like this:

mongo --port <port> --quiet --eval "db.adminCommand( { getLog:'global'} ).log.forEach(x => {print(x)})" | jq

if your DB has authentication you will need to add --authenticationDatabase and -u if you have authentication enabled.


You can also use jq to format your mongod.log file on the server as well:

cat mongod.log | jq
2 Likes