On this page本页内容
getLog¶getLog is an administrative command that returns the most recent 1024 logged mongod events. getLog does not
read log data from the mongod log file. It instead reads data from a RAM cache of logged mongod events. To run getLog, use the db.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.
The getLog command has the following syntax:语法如下所示:
The possible values for getLog are:
| Value | |
|---|---|
* |
Returns a list of the available values to the getLog command. |
global |
Returns the combined output of all recent log entries. |
startupWarnings |
Returns log entries that may contain errors or warnings from MongoDB’s log from when the current process started. If mongod started without warnings, this filter may return an empty array. |
Note
Starting in MongoDB 4.4, the getLog command no longer accepts the rs value, as this categorization of message type has been deprecated. Instead, log messages are now always identified by their component -
including REPL for replication messages. See Filtering by Component for log parsing examples that filter on the component field.
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 eventsdb.adminCommand() response document, containing status and timestamp information.Starting in MongoDB 4.2, getLog truncates any event that contains more than 1024 characters. In earlier versions, getLog truncates after 512 characters.
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.
mongo Shell¶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.
mongo Shell 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 the mongo shell. 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 the mongo shell 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: