MongoDB has always output log entries as plaintext.
Starting in MongoDB 4.4,
mongos instances now output all log messages in structured JSON format. This includes log output sent to the
stdout (standard out) log destinations, as well as the output of the
The documentation includes Log Parsing Examples using the
jq command line utility, but what if we want to
tail the logs and produce a similar result as to what was present prior to the introduction of structured logging?
1 2 3 m 4.4.6-ent mkdir data mongod --dbpath data --logpath data/mongodb.log --fork
Tailing the log now (
tail -n 30 data/mongodb.log) will show the structured log output that is the default in MongoDB 4.4+, however using
jq we can reformat (and colourize!!!) the output using one of the following:
1 2 # Windows tail -f data\mongodb.log | jq-win64 --compact-output -r -C ".msg |= sub(\"\\n\";\"\") | .t.\"$date\"+\" \"+.c+\" [\"+.ctx+\"] \"+.msg, .attr | select(.!=null)
1 2 # Linux/OSX tail -f data/mongodb.log | jq --compact-output -r -C '.msg |= sub("\n";"") | .t."$date"+" "+.c+" ["+.ctx+"] "+.msg, .attr | select(.!=null)'
This makes visually consuming the logs a lot easier! Some log messages, more commonly seen with increased Logging Verbosity may contain escape sequences (ex:
To render these escape sequences on screen while also tailing the logs in follow (
-f) mode try the following:
1 2 # Linux/OSX stdbuf -o0 tail -f mongod.log | stdbuf -o0 jq -r -C '.msg |= sub("\n";"") | .t."$date"+" "+.c+" ["+.ctx+"] "+.msg, .attr | select(.!=null)' | sed 's/\\n/\n/g; s/\\t/\t/g'
I personally find this a lot easier when monitoring a node’s logs while troubleshooting.
Let me know if you find this useful :)