Database Manual / Administration / Management

Rotate Log Files旋转日志文件

Overview概述

When used with the --logpath option or systemLog.path setting, mongod and mongos instances report a live account of all activity and operations to a log file. 当与--logpath选项或systemLog.path设置一起使用时,mongodmongos实例会将所有活动和操作的实时帐户报告到日志文件中。When reporting activity data to a log file, by default, MongoDB only rotates logs in response to the logRotate command, or when the mongod or mongos process receives a SIGUSR1 signal from the operating system. 当向日志文件报告活动数据时,默认情况下,MongoDB仅在响应logRotate命令时,或者当mongodmongos进程从操作系统接收到SIGUSR1信号时,才轮换日志。Both server logs and audit logs may be rotated with the logRotate command, either together or independently.服务器日志和审计日志都可以通过logRotate命令一起或独立地轮换。

MongoDB's standard log rotation approach archives the current log file and starts a new one. MongoDB的标准日志轮换方法将当前日志文件存档并启动一个新的日志文件。To do this, the mongod or mongos instance renames the current log file by appending a UTC timestamp to the filename, in ISODate format. It then opens a new log file, closes the old log file, and sends all new log entries to the new log file.为此,mongodmongos实例以ISODate格式在文件名后附加UTC时间戳,重命名当前日志文件。然后,它打开一个新的日志文件,关闭旧日志文件,并将所有新日志条目发送到新日志文件。

You can also configure MongoDB to support the Linux/Unix logrotate utility by setting systemLog.logRotate or --logRotate to reopen. 您还可以通过设置systemLog.logRotate--logrotate重新打开来配置MongoDB以支持Linux/Unix logrotate实用程序。With reopen, mongod or mongos closes the log file, and then reopens a log file with the same name, expecting that another process renamed the file prior to rotation.通过重新打开,mongodmongos会关闭日志文件,然后重新打开一个同名的日志文件,期望另一个进程在旋转之前重命名该文件。

Finally, you can configure mongod to send log data to the syslog using the --syslog option. In this case, you can take advantage of alternate log rotation tools.最后,您可以配置mongod,使用--syslog选项将日志数据发送到syslog。在这种情况下,您可以利用其他原木轮换工具。

Note

logRotate isn't a replicated command. You must connect to each instance of a replica set and run logRotate to rotate the logs for replica set members.logRotate不是复制命令。您必须连接到副本集的每个实例,并运行logRotate以轮换副本集成员的日志。

To rotate the log files, you must perform one of these steps:要旋转日志文件,您必须执行以下步骤之一:

  • Send a SIGUSR1 signal to the mongod or mongos process.mongodmongos进程发送SIGUSR1信号。
  • Run the MongoDB logRotate command.运行MongoDB logRotate命令。
  • Run the Linux/Unix logrotate utility.运行Linux/Unix logrotate实用程序。

See the examples later on this page.请参阅本页稍后的示例。

Tip

For information on logging, see the Process Logging section.有关日志记录的信息,请参阅进程日志记录部分。

Default Log Rotation Behavior默认日志旋转行为

By default, MongoDB uses the --logRotate rename behavior. With rename, mongod or mongos renames the current log file by appending a UTC timestamp to the filename, opens a new log file, closes the old log file, and sends all new log entries to the new log file.默认情况下,MongoDB使用--logRotate rename行为。通过renamemongodmongos通过在文件名后附加UTC时间戳来重命名当前日志文件,打开一个新日志文件,关闭旧日志文件,并将所有新日志条目发送到新日志文件。

1

Start a mongod instance.启动一个mongod实例。

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

You can also explicitly specify --logRotate rename.您还可以显式指定--logRotate rename

2

List the log files列出日志文件

In a separate terminal, list the matching files:在单独的终端中,列出匹配的文件:

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

The results should include one log file, server1.log.结果应包括一个日志文件server1.log

3

Rotate the log file.旋转日志文件。

Rotate the log file by issuing the logRotate command from the admin database in mongosh:通过在mongosh中从admin数据库发出logRotate命令来旋转日志文件:

db.adminCommand( { logRotate : "server" } )

If auditing is enabled, you can specify 1 to logRotate (instead of server) to rotate both the server and audit logs at the same time, if desired. 如果启用了审核,如果需要,可以将1指定为logRotate(而不是server),以同时轮换服务器和审核日志。The audit log will be rotated in the same fashion as the server log, according to the --logRotate setting.根据--logRotate设置,审核日志将以与服务器日志相同的方式轮换。

Note

You can't run this command on an arbiter for an authenticated replica set.您无法在经过身份验证的副本集的仲裁器上运行此命令。

4

View the new log files查看新日志文件

List the new log files to view the newly-created log:列出新的日志文件以查看新创建的日志:

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

There should be two log files listed: server1.log, which is the log file that mongod or mongos made when it reopened the log file, and server1.log.<timestamp>, the renamed original log file.应该列出两个日志文件:server1.log,这是mongodmongos重新打开日志文件时创建的日志文件,以及server1.log.<timestamp>,重命名的原始日志文件。

Rotating log files does not modify the "old" rotated log files. When you rotate a log, you rename the server1.log file to include the timestamp, and a new, empty server1.log file receives all new log input.旋转日志文件不会修改“旧”的旋转日志文件。当您旋转日志时,您会重命名server1.log文件以包含时间戳,一个新的空server1.log文件会接收所有新的日志输入。

Log Rotation with --logRotate reopen使用--logRotate reopen记录旋转

Log rotation with --logRotate reopen closes and opens the log file following the typical Linux/Unix log rotate behavior.使用--logRotate reopen的记录旋转会按照典型的Linux/Unix日志旋转行为关闭并打开日志文件。

1

Start a mongod instance, specifying the reopen --logRotate behavior.启动一个mongod实例,指定reopen --logRotate行为。

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

You must use the --logappend option with --logRotate reopen.您必须在--logRotate reopen时使用--logappend选项。

2

List the log files列出日志文件

In a separate terminal, list the matching files:在单独的终端中,列出匹配的文件:

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

The results should include one log file, server1.log.结果应包括一个日志文件server1.log

3

Rotate the log file.旋转日志文件。

Rotate the log file by issuing the logRotate command from the admin database in mongosh:通过在mongosh中从管理数据库发出logRotate命令来旋转日志文件:

db.adminCommand( { logRotate : "server" } )

You should rename the log file using an external process, following the typical Linux/Unix log rotate behavior.您应该按照典型的Linux/Unix日志轮换行为,使用外部进程重命名日志文件。

Syslog Log RotationSyslog日志轮换

With syslog log rotation, mongod sends log data to the syslog rather than writing it to a file.通过syslog日志轮换,mongod将日志数据发送到syslog,而不是将其写入文件。

MongoDB includes the component in its log messages to syslog.MongoDB在其发送给syslog的日志消息中包含了该组件

1

Start a mongod instance with the --syslog option使用--syslog选项启动mongod实例

mongod --syslog

Do not include --logpath. 不要包含--logpathSince --syslog tells mongod to send log data to the syslog, specifying a --logpath will causes an error.由于--syslog告诉mongod将日志数据发送到syslog,因此指定--logpath将导致错误。

To specify the facility level used when logging messages to the syslog, use the --syslogFacility option or systemLog.syslogFacility configuration setting.要指定将消息记录到syslog时使用的设施级别,请使用--syslogFacility选项或systemLog.syslogFacility配置设置。

2

Rotate the log.旋转日志。

Store and rotate the log output using your systems default log rotation mechanism.使用系统默认日志旋转机制存储和旋转日志输出。

Forcing a Log Rotation with SIGUSR1使用SIGUSR1强制日志旋转

For Linux and Unix-based systems, you can use the SIGUSR1 signal to rotate the logs for a single process.对于基于Linux和Unix的系统,您可以使用SIGUSR1信号来轮换单个进程的日志。

For example, if a running mongod instance has a process ID (PID) of 2200, the following command rotates the log file for that instance on Linux:例如,如果一个正在运行的mongod实例的进程ID(PID)为2200,则以下命令将在Linux上轮换该实例的日志文件:

kill -SIGUSR1 2200