Docs Home / mongosh / Reference / Logs / Retention

Modify Log Retention Duration修改日志保留时间

You can modify how long MongoDB Shell log files are retained. A log cleanup process automatically deletes log files older than the specified retention period. By default, log files are retained for 30 days.您可以修改MongoDB Shell日志文件保留的时间。日志清理过程会自动删除超过指定保留期的日志文件。默认情况下,日志文件保留30天。

About this Task关于此任务

You can specify multiple criteria for log retention. For example, you can specify a maximum duration for log files of 60 days and a maximum log storage size of 2GB. Logs are deleted when any retention criterion is met, starting with the oldest logs.您可以为日志保留指定多个条件。例如,您可以指定日志文件的最长持续时间为60天,最大日志存储大小为2GB。当满足任何保留标准时,日志将被删除,从最旧的日志开始。

Before you Begin开始之前

To check the current log retention duration, run the following command:要检查当前日志保留时间,请运行以下命令:

config.get("logRetentionDays")

Steps步骤

To modify how long log files are retained, set the logRetentionDays configuration option. You can set configuration options in the configuration API or a configuration file.要修改日志文件的保留时间,请设置logRetentionDays配置选项。您可以在配置API或配置文件中设置配置选项。

Modify Log Duration with the Configuration API使用配置API修改日志持续时间

The following command uses the config API to set log retention to 60 days:以下命令使用config API将日志保留期设置为60天:

config.set("logRetentionDays", 60)
Setting "logRetentionDays" has been changed

Modify Log Duration with a Configuration File使用配置文件修改日志持续时间

The following configuration file sets log retention to 60 days:以下配置文件将日志保留期设置为60天:

mongosh:
logRetentionDays: 60

Disable Duration-Based Log Cleanup禁用基于持续时间的日志清理

To instruct MongoDB Shell to not delete logs based on file age, set logRetentionDays to Infinity. You can perform this action through the config API or configuration file. For example:要指示MongoDB Shell不根据文件年龄删除日志,请将logRetentionDays设置为Infinity。您可以通过配置API或配置文件执行此操作。例如:

config.set("logRetentionDays", Infinity)

Important

To prevent log storage from growing too large, always specify at least one log retention criterion.为防止日志存储增长得太大,请始终至少指定一个日志保留标准。