- Reference >
- Database Commands >
- Diagnostic Commands >
- getLog
getLog¶
-
getLog
¶ getLog
is an administrative command that returns the most recent 1024 loggedmongod
events.getLog
does not read log data from themongod
log file. It instead reads data from a RAM cache of loggedmongod
events. To rungetLog
, use thedb.adminCommand()
method.Starting in MongoDB 4.4,
getLog
returns log data in escaped Relaxed Extended JSON v2.0 format. Previously, log data was returned as plaintext.
Syntax¶
The getLog
command has the following syntax:
Output¶
If specified *
, then the command returns a document with the
names of the other acceptable values.
Otherwise, the command returns a document that includes the following fields:
totalLinesWritten
field that contains the number of log eventslog
field that contains an array of log events- A
db.adminCommand()
response document, containing status and timestamp information.
Behavior¶
Line Truncation¶
Starting in MongoDB 4.2, getLog
truncates any event
that contains more than 1024 characters. In earlier versions,
getLog
truncates after 512 characters.
Character Escaping¶
Starting in MongoDB 4.4, getLog
returns log data in
escaped Relaxed Extended JSON v2.0 format, using the following
escape sequences to render log output as valid JSON:
Character Represented | Escape Sequence |
---|---|
Quotation Mark (" ) |
\" |
Backslash (\ ) |
\\ |
Backspace (0x08 ) |
\b |
Formfeed (0x0C ) |
\f |
Newline (0x0A ) |
\n |
Carriage return (0x0D ) |
\r |
Horizontal tab (0x09 ) |
\t |
Control characters not listed above are escaped with \uXXXX
where
“XXXX” is the unicode codepoint in hexadecimal. Bytes with invalid
UTF-8 encoding are replaced with the unicode replacement character
represented by \ufffd
.
Filtering¶
Within mongosh
¶
getLog
output can be filtered to make results more
readable or to match on specific criteria.
The following operation prints just the log
field (which contains
the array of all recent log events), and removes
character escaping from each log
message:
This operation presents getLog
output in the same format as
the MongoDB log file
.
Outside mongosh
with jq
¶
When working with MongoDB structured logging, the third-party jq command-line utility is a useful tool that allows for easy pretty-printing of log entries, and powerful key-based matching and filtering.
jq
is an open-source JSON parser, and is available for
Linux, Windows, and macOS.
To use jq
with getLog
output, you must use the
--eval
option to mongosh
.
The following operation uses jq
to filter on the REPL
component to present only those log
messages associated with replication:
Be sure to provide any necessary connection-specific parameters to
mongosh
as needed, such as --host
or --port
.
See Parsing Structured Log Messages for more examples of filtering log output
using jq
. The jq
syntax presented in each linked example can be
used with the above mongo --eval
operation with minor adjustment.
For example, the following syntax adapts the linked “Counting Unique
Messages” example for use with getLog
:
Examples¶
Retrieve Available Log Filters¶
The following operation, run from mongosh
,
returns the available log filters for passing to getLog
:
The operation returns the following document:
Retrieve Recent Events from Log¶
The following operation, run from mongosh
,
retrieves the most recent global
events for the
mongod
:
The operation returns a document similar to the following: