On this page本页内容
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设置一起使用时,mongod和mongos实例将所有活动和操作的实时帐户报告到日志文件中。When reporting activity data to a log file, by default, MongoDB only rotates logs in response to the 在向日志文件报告活动数据时,默认情况下,MongoDB仅在响应logRotate command, or when the mongod or mongos process receives a SIGUSR1 signal from the operating system. logRotate命令或mongod或mongos进程从操作系统接收到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. mongod或mongos实例通过在文件名后面附加一个UTC时间戳,以ISODate格式重命名当前日志文件。It then opens a new log file, closes the old log file, and sends all new log entries to the new log file.然后,它打开一个新日志文件,关闭旧日志文件,并将所有新日志条目发送到新日志文件。
You can also configure MongoDB to support the Linux/Unix 您还可以通过将logrotate utility by setting systemLog.logRotate or --logRotate to reopen. systemLog.logRotate或--logrotate设置为reopen,将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.reopen,mongod或mongos关闭日志文件,然后重新打开具有相同名称的日志文件,希望其他进程在旋转之前重命名该文件。
Finally, you can configure 最后,您可以配置mongod to send log data to the syslog using the --syslog option. mongod,使用--syslog选项将日志数据发送到syslog。In this case, you can take advantage of alternate log rotation tools.在这种情况下,您可以利用备用日志旋转工具。
For information on logging, see the Process Logging section.有关日志记录的信息,请参阅流程日志记录部分。
By default, MongoDB uses the 默认情况下,MongoDB使用--logRotate rename behavior. --logRotate rename行为。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.rename,mongod或mongos通过在文件名后附加UTC时间戳来重命名当前日志文件,打开新日志文件,关闭旧日志文件,并将所有新日志条目发送到新日志文件。
mongod instance.mongod实例。mongod -v --logpath /var/log/mongodb/server1.log
You can also explicitly specify 您还可以显式指定--logRotate rename.--logRotate rename。
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设置,审核日志将以与服务器日志相同的方式旋转。
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,这是mongod或mongos重新打开日志文件时生成的日志文件;以及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将接收所有新的日志输入。
--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日志旋转行为关闭并打开日志文件。
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.--logappend选项与--logRotate reopen重新打开一起使用。
Rotate the log file by issuing the 通过从logRotate command from the admin database in mongosh:mongosh中的admin数据库发出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日志旋转行为。
With syslog log rotation, 通过syslog日志轮换,mongod sends log data to the syslog rather than writing it to a file.mongod将日志数据发送到syslog,而不是将其写入文件。
Starting in version 4.2, MongoDB includes the component in its log messages to 从版本4.2开始,MongoDB将该组件包含在其到syslog.syslog的日志消息中。
mongod instance with the --syslog option--syslog选项启动mongod实例mongod --syslog
Do not include 不包括--logpath. --logpath。Since 因为--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 要指定将消息记录到syslog时使用的设施级别,请使用--syslogFacility option or systemLog.syslogFacility configuration setting.--syslogFacility选项或systemLog.syslogFacility配置设置。
SIGUSR1SIGUSR1强制日志旋转For Linux and Unix-based systems, you can use the 对于基于Linux和Unix的系统,可以使用SIGUSR1 signal to rotate the logs for a single process.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