Configuring Log Rotation on MongoDB

Ambience 2020+ requires MongoDB for the storing of its system data. The example below illustrates how to automate the rotation of the MongoDB logs in a Unix environment using the system’s logrotate utility.

  1. Create a ‘mongodb’ file in the /logrotate.d directory, e.g. :
    vi /etc/logrotate.d/mongodb

  2. Insert the following in the mongodb file, replace ‘/var/log/mongodb/mongodb.log’ with your own MongoDB logging path and log file, e.g.

 /var/log/mongodb/mongodb.log {
          	kill -SIGUSR1 $(cat /var/lib/mongodb/mongod.lock)
  1. From the above configuration:
  • The MongoDB logs are rotated daily.
  • The previous logs are archived using the compress option.
  • The missingok option ensures the log rotation script is not disrupted if the mongodb.log file is not found in the log directory.
  • The sharedscripts option tells the logrotate utility to run the script under postrotate once, endscript indicates the end of the script.
  • The ‘kill -SIGUSR1 $(cat /var/lib/mongodb/mongod.lock)’ command rotates the MongoDB log.
  • The rotated MongoDB logs will have a date and timestamp appended to the file name, e.g. mongod.log.2021-08-11T08-06-29