Video Transcript (English)
In this video, you’ll learn how to locate logs in self-managed deployments.
First, we need to ensure we have the necessary access permissions. Then we’ll locate log files generated by a self-managed MongoDB deployment. Finally, we’ll use the `show log` helper in mongosh to view recent events in a log. Let’s get started!
MongoDB can write logs to a log file, the Linux `syslog`, or the standard output. By default, logs are written to the log file unless a different destination is specified in the configuration.
On Linux, the log file is located in the `/var/log/mongodb` directory and is named `mongod.log` by default.
When we run `ls -l` on the file, it shows us that the `mongodb` user and `mongodb` group can access it.
To gain access to the log file, we can add our current user to the `mongodb` group, or use `sudo` when accessing the file. In this example, we'll use `sudo` to print the first 5 lines of the file to the terminal.
If the `mongod.log` file is not in its default location, we can check the MongoDB configuration file to find where it’s located. In this file, check the `systemLog.path` property to find where the log file is located.
Now that we know where the logs are located and how to access them, let's take a look at some recent log entries.
On a self-managed deployment or an M10-or-above Atlas cluster, we use the `show log` shell helper to return the most recent logged events from our MongoDB instance. More specifically, it returns the most recent 1024 entries, regardless of where they are stored.
To demonstrate, we’ll run a few commands in mongosh against a self-managed instance. Keep in mind that the process is the same on M10-or-above Atlas clusters.
First, let’s use the shell helper command `show logs`, to get a list of all available logs. Then, we can use `show log` to return a specific kind of log. If we don’t specify a log name to `show log` it returns the global log by default. Under the hood, this shell helper actually calls another admin command, `getLog`.
To use the adminCommand, we provide a document with the key, `getLog`, and the kind of log we want. For this example, we’ll get the most recent entries from the "global" log.
All logs, regardless of whether you use the log file, syslog, or the standard output, will be in JSON format.
Great work!
In this video, you learned how to verify access to logs and how locate them in self-managed deployments. You also learned how to use the `show log` helper to view recent events.