- MongoDB Tutorials >
- Rotate Log Files
Rotate Log Files¶
Overview¶
Log rotation archives the current log file and starts a new one.
Specifically, log rotation renames the current log file by appending
the filename with a timestamp, [1] opens a new log
file, and finally closes the old log. MongoDB will only rotate logs,
when you use the logRotate command, or issue the process
a SIGUSR1 signal as described in this procedure.
See also
For information on logging, see the Process Logging section.
Procedure¶
The following steps create and rotate a log file:
Start a
mongodwith verbose logging, with appending enabled, and with the following log file:In a separate terminal, list the matching files:
For results, you get:
Rotate the log file using one of the following methods.
From the
mongoshell, issue thelogRotatecommand from theadmindatabase:This is the only available method to rotate log files on Windows systems.
From the UNIX shell, rotate logs for a single process by issuing the following command:
From the UNIX shell, rotate logs for all
mongodprocesses on a machine by issuing the following command:
List the matching files again:
For results you get something similar to the following. The timestamps will be different.
The example results indicate a log rotation performed at exactly
11:30 pmonNovember 24th, 2011 UTC, which is the local time offset by the local time zone. The original log file is the one with the timestamp. The new log isserver1.logfile.If you issue a second
logRotatecommand an hour later, then an additional file would appear when listing matching files, as in the following example:This operation does not modify the
server1.log.2011-11-24T23-30-00file created earlier, whileserver1.log.2011-11-25T00-30-00is the previousserver1.logfile, renamed.server1.logis a new, empty file that receives all new log output.
| [1] | MongoDB renders this timestamp in UTC (GMT) and formatted as ISODate. |