Rotate Log Files

Overview

When used with the —logpath option or systemLog.path setting,mongod and mongos instances reporta live account of all activity and operations to a log file.When reporting activity data to a log file, by default, MongoDB only rotates logsin response to the logRotate command, or when themongod or mongos process receives a SIGUSR1signal from the operating system.

MongoDB’s standard log rotation approach archives the currentlog file and starts a new one. To do this, the mongod ormongos instance renames the current log file by appending aUTC timestamp to the filename, in ISODate format. It thenopens a new log file, closes the old log file, and sends all new logentries to the new log file.

You can also configure MongoDB to support the Linux/Unixlogrotate utilityby setting systemLog.logRotate or—logRotate to reopen. With reopen, mongodor mongos closes the log file, andthen reopens a log file with the same name, expecting that anotherprocess renamed the file prior to rotation.

Finally, you can configure mongod to send log data to thesyslog using the —syslog option. In this case, you cantake advantage of alternate logrotation tools.

See also

For information on logging, see theProcess Logging section.

Default Log Rotation Behavior

By default, MongoDB uses the—logRotate rename behavior.With rename, mongod ormongos renames the current log file by appending a UTCtimestamp to the filename, opens a new log file, closes the old log file,and sends all new log entries to the new log file.

Start a mongod instance.

  1. mongod -v --logpath /var/log/mongodb/server1.log

You can also explicitly specify —logRotate rename.

List the log files

In a separate terminal, list the matching files:

  1. ls /var/log/mongodb/server1.log*

The results should include one log file, server1.log.

Rotate the log file.

Rotate the log file by issuing the logRotate commandfrom the admin database in a mongo shell:

  1. db.adminCommand( { logRotate : 1 } )

View the new log files

List the new log files to view the newly-created log:

  1. ls /var/log/mongodb/server1.log*

There should be two log files listed: server1.log, which is thelog file that mongod or mongos made when itreopened the log file, and server1.log.<timestamp>, the renamedoriginal log file.

Rotating log files does not modify the “old” rotated log files. Whenyou rotate a log, you rename the server1.log file to includethe timestamp, and a new, empty server1.log file receives allnew log input.

Log Rotation with —logRotate reopen

New in version 3.0.0.

Log rotation with —logRotate reopen closes and opensthe log file following the typical Linux/Unix log rotate behavior.

Start a mongod instance, specifying the reopen —logRotate behavior.

  1. mongod -v --logpath /var/log/mongodb/server1.log --logRotate reopen --logappend

You must use the —logappend option with—logRotate reopen.

List the log files

In a separate terminal, list the matching files:

  1. ls /var/log/mongodb/server1.log*

The results should include one log file, server1.log.

Rotate the log file.

Rotate the log file by issuing the logRotate commandfrom the admin database in a mongo shell:

  1. db.adminCommand( { logRotate : 1 } )

You should rename the log file using an external process, followingthe typical Linux/Unix log rotate behavior.

Syslog Log Rotation

With syslog log rotation, mongod sends log data to the syslograther than writing it to a file.

Starting in version 4.2, MongoDB includes the component in its log messages to syslog.

Start a mongod instance with the —syslog option

  1. mongod --syslog

Do not include —logpath. Since —syslog tellsmongod to send log data to the syslog, specifying a—logpath will causes an error.

To specify the facility level used when logging messages to the syslog,use the —syslogFacility option orsystemLog.syslogFacility configuration setting.

Rotate the log.

Store and rotate the log output using your systems default logrotation mechanism.

Forcing a Log Rotation with SIGUSR1

For Linux and Unix-based systems, you can use the SIGUSR1 signalto rotate the logs for a single process, as in the following:

  1. kill -SIGUSR1 <mongod process id>