Docs Home / mongosh / Reference / Logs

Specify Log File Location指定日志文件位置

You can specify where MongoDB Shell writes log files. By default, MongoDB Shell saves the log for each session to your user's .mongodb/mongosh directory, which depends on your operating system:您可以指定MongoDB Shell写入日志文件的位置。默认情况下,MongoDB Shell会将每个会话的日志保存到用户的.mongodb/mongosh目录中,这取决于操作系统:

Operating System操作系统Default Log Location默认日志位置
macOS and Linux~/.mongodb/mongosh/<LogID>_log
Windows%UserProfile%/AppData/Local/mongodb/mongosh/<LogID>_log

About this Task关于此任务

To view the current log file location, use the config API to return the logLocation value:要查看当前日志文件位置,请使用config API返回logLocation值:

config.get("logLocation")

After you modify your log file location, you must start a new MongoDB Shell session for the change to take effect.修改日志文件位置后,必须启动新的MongoDB Shell会话才能使更改生效。

Log File Location日志文件位置

Starting in MongoDB Shell 2.4.0, you can use the log.getPath() command to view the current log file location. For example:从MongoDB Shell 2.4.0开始,您可以使用log.getPath()命令查看当前日志文件位置。例如:

log.getPath()

Example output:输出示例:

/Users/jane.doe/.mongodb/mongosh/c2961dbd6b73b052671d9df0_log

The hexadecimal value in the path is the MongoDB Shell log identifier for the current session.路径中的十六进制值是当前会话的MongoDB Shell日志标识符。

Log File Names日志文件名

If you modify the default log file location, log files have a mongosh_ prefix before the session ID. For example, the log for session ID 67be0c0eb6227e211a1979e8 is saved as mongosh_67be0c0eb6227e211a1979e8_log.如果修改默认日志文件位置,则日志文件在会话ID之前会有mongosh_前缀。例如,会话ID 67be0c0eb6227e211a1979e8的日志会另存为mongosh_67be0c0eb6227e11a1979e8_log

If you use the default log file location, the file name does not include the mongosh_ prefix. For example, the log for session ID 67be0c0eb6227e211a1979e8 is saved as 67be0c0eb6227e211a1979e8_log.如果使用默认日志文件位置,则文件名不包括mongosh_前缀。例如,会话ID 67be0c0eb6227e211a1979e8的日志保存为67be0c0eb6227e11a1979e8_log

Steps步骤

To change the log file location, modify the logLocation configuration option. You can modify configuration options with the configuration API or a configuration file.要更改日志文件位置,请修改logLocation配置选项。您可以使用配置API或配置文件修改配置选项。

Important

Specify logLocation as an absolute filepath.logLocation指定为绝对文件路径。

Modify Log Location with the API使用API修改日志位置

The following command uses the config API to set the logLocation setting to /path/to/log/directory:以下命令使用config API将logLocation设置设置为/path/to/log/directory

config.set("logLocation", "/path/to/log/directory")
Setting "logLocation" has been changed

Modify Log Location with a Configuration File使用配置文件修改日志位置

The following configuration file sets the logLocation setting to /path/to/log/directory:以下配置文件logLocation设置设置为/path/to/log/目录:

mongosh:
logLocation: "/path/to/log/directory"