Lesson 2: MongoDB Logs on Self-Managed Instances / Learn
Code Summary: MongoDB Logs on Self-Managed Instances
The following sections explain how to locate logs in self-managed MongoDB instances, as well as check access permissions, access the log file, and retrieve log messages.
Default Location on Linux
On Linux, the MongoDB log file is found in the following directory by default and is named mongod.log:
/var/log/mongodb/mongod.log
Check Permissions of the Log File
To check the file permissions of the mongod.log file, use the ls command, which lists all files and subdirectories in the current directory. When ls is used with the -l option, long format is used. This option includes the user and group that can access the file.
> ls -l /var/log/mongodb/mongod.log
Access the Log File
To access the log file, prepend the sudo command to the directory name. The following example uses the head command to print the first five lines of the mongod.log file:
sudo head -5 /var/log/mongodb/mongod.log
If the log file is not in its default location, check the mongod.conf file to determine if an alternate path was provided. The following example uses the cat command to check the value of the systemLog.path property:
sudo cat /etc/mongod.conf
Retrieve Log Messages
To show recent global log messages from the RAM cache in mongosh, use the show log helper and provide it with one of the available filters, such as global or startupWarnings.
show log <type>
To view the available filters that can be provided to the show log helper, use the following helper command in mongosh:
show logs
The mongosh helper show log global internally calls the getLog command to return recent log messages from the RAM cache:
db.adminCommand( { getLog:'global'} )