logRotate

On this page本页内容

Definition定义

logRotate

The logRotate command is an administrative command that allows you to rotate the MongoDB server log and/or audit log to prevent a single logfile from consuming too much disk space.logRotate命令是一个管理命令,允许您旋转MongoDB服务器日志和/或审计日志,以防止单个日志文件占用过多的磁盘空间。

You must issue the logRotate command against the admin database in the form:必须对admin数据库发出logRotate命令,格式如下:

db.adminCommand({logRotate: <target>})

The logRotate command takes the following parameters:logRotate命令采用以下参数:

Parameter参数Type类型Description描述
<target>string

The log or logs to rotate, according to the following:要旋转的一个或多个日志,具体如下:

  • 1 -- Rotates both the server and audit logs旋转服务器和审核日志
  • server -- Rotates only the server log仅旋转服务器日志
  • audit -- Rotates only the audit log仅旋转审核日志
commentanyoptional A message logged by the server to the log file and audit file at time of log rotation. 在日志轮换时,服务器记录到日志文件和审核文件中的消息。

You may also rotate the logs by sending a SIGUSR1 signal to the mongod process.您还可以通过向mongod进程发送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

Limitations局限性

Behavior行为

The systemLog.logRotate setting or --logRotate option specify logRotate's behavior.systemLog.logRotate设置或--logRotate选项指定logRotate的行为。

When systemLog.logRotate or --logRotate are set to rename, logRotate renames the existing log file by appending the current timestamp to the filename. systemLog.logRotate--logRotate设置为rename时,logRotate通过将当前时间戳附加到文件名来重命名现有日志文件。The appended timestamp has the following form:附加的时间戳具有以下格式:

<YYYY>-<mm>-<DD>T<HH>-<MM>-<SS>

Then logRotate creates a new log file with the same name as originally specified by the systemLog.path setting to mongod or mongos.然后logRotate创建一个新的日志文件,其名称与systemLog.path设置为mongodmongos最初指定的名称相同。

When systemLog.logRotate or --logRotate are set to reopen, logRotate follows the typical Linux/Unix behavior, and simply closes the log file then reopens a log file with the same name. systemLog.logRotate--logRotate设置为reopen打开时,logRotate遵循典型的Linux/Unix行为,只需关闭日志文件,然后重新打开同名的日志文件。With reopen, mongod expects that another process renames the file prior to the rotation, and that the reopen results in the creation of a new file.对于reopenmongod期望在旋转之前另一个进程重命名文件,并且重新打开会导致创建新文件。

Examples示例

The following example rotates both the server log and the audit log:以下示例同时旋转服务器日志和审核日志:

db.adminCommand({logRotate: 1})

The following example rotates only the audit log, and provides a custom message to the log file at time of rotation:以下示例仅旋转审核日志,并在旋转时向日志文件提供自定义消息:

db.adminCommand({logRotate: audit, comment: "Rotating audit log"})
←  listIndexesreIndex →