Video Transcript (English)
MongoDB maintains a running log of events, including entries such as incoming connections, commands run, and issues encountered.
We can use log messages to diagnose issues like slow queries, to help with tuning performance, and to monitor deployments.
In this video, you’ll learn how to access the MongoDB logs in a self-managed deployment.
Let’s get started. First, we’ll need to learn how to access the logs.
One way to access the logs is by opening the log file at the default path or the path specified in the configuration file.
In case you don’t know, or have forgotten where that is, you can check the path in the MongoDB Shell by running the serverCmdLineOpts() method.
You can access the logs by then opening the file located at the path shown.
Another way to access the logs is by using the `show` helper in the MongoDB Shell.
The MongoDB logging system uses tags to group and identify messages by name.
We will run `show logs` to display a list of accessible tag names. Here, we see that we have access to global and startupWarnings.
When we run `show log global`, we see the combined output of all recent log entries.
If we don’t include the tag name, then the show log command will default to global.
If we want to limit the output to log entries that contain the tag `startupWarnings`, then we would run “show log startupWarnings”.
Now that you know how to access the logs, let’s learn more about the MongoDB log format.
Starting in MongoDB 4.4, mongod instances output all log messages in structured JSON format.
This means that log entries are written as a series of key-value pairs. Each key indicates a log message field type, and each corresponding value records the associated logging information for that field type.
To learn more about structured JSON, let’s examine one of the log entries with a startupWarning tag.
First, there’s a timestamp that indicates when the event occurred.
Next, `s` represents the severity level of the message. The three most severe levels are Fatal, Error, and Warning, which indicate that something is wrong. The severity level here is I, for informational. Informational entries are displayed only when the log verbosity is set to 0. The verbosity level of a log determines the amount of Informational and Debug messages that MongoDB outputs. 0 is MongoDB's default verbosity level.
`c` is the full component string for the log message, which indicates the category of the logged event. In this case, it is STORAGE, which means that this entry is related to storage activities.
`id` is the unique identifier for each type of log statement.
`ctx` tells us the name of the thread that caused the log message.
`msg` is the actual log output message that is passed from the server or driver. In this case, it says, "Using the XFS filesystem is strongly recommended with the WiredTiger storage engine" and points to a resource.
This message indicates that we should probably investigate the file system we’re using and consider using XFS, because it performs better with MongoDB.
Finally, `tags` show any tag strings that are related to a log message. In this case, the one tag listed indicates that this is a startup warning.
Here’s a full list of key-value pairs that you might see in a log message. Log messages include only the necessary keys.
For example, if there are no associated attributes for a log message, that key will not be included.
It’s also important to know that log files can quickly grow in volume. Rotating log files can prevent them from consuming too much disk space.
MongoDB does not automatically rotate the logs, but a system administrator can do so by using the logRotate admin command.
To learn more about log rotation, see MongoDB’s documentation.
Now let’s recap what you learned about log messages in MongoDB:
MongoDB maintains a running log of events that we can use to diagnose issues, tune performance, and monitor deployments.
We can access the logs via the log path for a deployment, or by using the show log helper in the MongoDB Shell.
Log messages are output in structured JSON format, a series of key-value pairs that contain information about each log event.