定义
logRotateThe
logRotatecommand 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.You must issue the
logRotatecommand against the admin database.
兼容性
此命令可用于以下环境中托管的部署:
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
重要
This command is not supported in MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.
语法
该命令具有以下语法:
db.adminCommand( { logRotate: <integer or string>, comment: <string> } )
命令字段
该命令接受以下字段:
字段 | 类型 | 说明 |
|---|---|---|
| 整数或字符串 | 要轮换的一个或多个日志,按以下方式进行:
|
| 字符串 | 可选。日志轮换时服务器记录到日志文件和审核文件的消息。 |
您也可以通过向 mongod 进程发送 SIGUSR1 信号来轮换日志。
例如,如果正在运行的 mongod 实例的进程 ID (PID) 为 2200,则以下命令在 Linux 上为该实例轮转日志文件:
kill -SIGUSR1 2200
限制
Your
mongodinstance needs to be running with the--logpath [file]option in order to uselogRotate必须启用审核功能才能轮换审核日志。
行为
The systemLog.logRotate setting or --logRotate option specify logRotate's behavior.
When systemLog.logRotate or --logRotate are set to rename, logRotate renames the existing log file by appending the current timestamp to the filename. 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.
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. 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.
示例
下面的示例同时轮换服务器日志和审核日志:
db.adminCommand( { logRotate: 1 } )
以下示例仅轮换审核日志,并在轮换时向日志文件提供自定义消息:
db.adminCommand( { logRotate: "audit", comment: "Rotating audit log" } )