Get your MongoDB Log rolling

1 minute read time.

As we grow our businesses, we tend to focus in on areas that will help our businesses succeed. When it comes to IT and data maintenance one tool that we rely heavily on is MongoDB, which is a huge advantage when used correctly.

When learning the tools available within MongoDB that would benefit any business you often find yourself looking at the logs. Logs have many uses, they can point you to existing issues or errors, areas to spend more resources and even just to confirm that everything is working as expected.

Todays focus is specifically on the size of your mongoDB log file.

MongoDB logs can be found in D:\Sage\SafeX3\MongoDB\logs\mongodb.log (Or you equivalent mongoDB install location, in my case it’s on D:\ )

When it comes to very large mongoDB log files, there are two considerations:

  • To immediately rotate the log file, so a new empty log is created straight away, you can follow these steps:

To trigger a log rotate it is not necessary to restart the windows service, you can use the following MongoDB shell command:
db.adminCommand({ logRotate: 1 })

or you can run it directly via the mongo executable:
mongo --host server --port portNumber --eval "db.adminCommand({ logRotate: 1 })"

Having the ability to generate a fresh log at the time of testing can eliminate needless hours scrolling through logs that seem to never end.

  • The dbMessage collection by default (until MongoDB 3.6) will create a log file entry if a command takes more than 100ms (which is the majority of commands)

To reduce the amount of logging, you can change the logging threshold for the "dbMessage" messages

  • Edit the mongodb.conf file

Uncomment the two lines shown below, and change the value from 100 to 10000 (i.e. 10 seconds)

operationProfiling:
slowOpThresholdMs: 10000

  • For this change to take effect you will need to restart MongoDB service

Using this feature is beneficial when looking into performance, were there seems to be a lag in response from MongoDB, increasing this value will capture only those issues where a command is taking longer than normal to execute.

Please check the mongoDB documentation|https://docs.mongodb.com/v3.6/reference/configuration-options/ and https://docs.mongodb.com/v3.4/tutorial/rotate-log-files/ for more details.

By implementing these changes into your mongoDB environment it will allow you to trace and troubleshoot issues by providing more precise logging with increased accuracy.