Docs HomeMongoDB Shell

Retrieve Shell Logs检索Shell日志

MongoDB Shell uses ndjson to store session logs. MongoDB Shell使用ndjson来存储会话日志。Starting in mongosh version 1.0.5, the log MongoDB Shell format is updated to match the MongoDB server log format. mongosh 1.0.5版本开始,日志MongoDB Shell格式进行了更新,以匹配MongoDB服务器日志格式。For details, see Log Messages.有关详细信息,请参阅日志消息

You can view or tail the logs for a MongoDB Shell session based on its log ID.您可以根据MongoDB Shell会话的日志ID查看或跟踪其日志。

Note

The MongoDB Shell redacts credentials from the command history and the logs.MongoDB Shell对命令历史记录日志中的凭据进行编辑。

View MongoDB Shell Logs查看MongoDB Shell日志

1

Find your Log ID.查找日志ID。

Example

MongoDB Shell displays the log ID each time you open the shell.每次打开外壳时,MongoDB外壳都会显示日志ID。

$ mongosh

Current Mongosh Log ID: c2961dbd6b73b052671d9df0
Connecting to: mongodb://127.0.0.1:27017
Using MongoDB: 4.2.8
Using Mongosh: 1.10.1
2

View the log for the session.查看会话的日志。

MongoDB Shell saves the log for each session to your user's .mongodb/mongosh directory:MongoDB Shell将每个会话的日志保存到用户的.mongodb/mongosh目录中:

~/.mongodb/mongosh/<LogID>_log

Run the following command to view the log for a session:运行以下命令以查看会话的日志:

cat ~/.mongodb/mongosh/<LogID>_log

Run the following command to tail the log for a session:运行以下命令以跟踪会话的日志:

tail -f ~/.mongodb/mongosh/<LogID>_log

MongoDB Shell saves the log for each session to your user's C:\Users\<username>\AppData\Local\ directory:MongoDB Shell将每个会话的日志保存到用户的C:\Users\<username>\AppData\Local\目录中:

%UserProfile%/AppData/Local/mongodb/mongosh/<LogID>_log

Run the following PowerShell command to view the log for a session:运行以下PowerShell命令以查看会话的日志:

Get-Content %UserProfile%/AppData/Local/mongodb/mongosh/<LogID>_log

Run the following PowerShell command to tail the log for a session:运行以下PowerShell命令以跟踪会话的日志:

Get-Content %UserProfile%/AppData/Local/mongodb/mongosh/<LogID>_log

View MongoDB Shell Command History查看MongoDB Shell命令历史记录

MongoDB Shell saves a history of all commands you've run across sessions. When a new command is issued, it is added to the beginning of the history file.MongoDB Shell保存您在会话中运行的所有命令的历史记录。当发出新命令时,它会被添加到历史文件的开头。

Open the following file in a text editor to view the MongoDB Shell command history:在文本编辑器中打开以下文件以查看MongoDB Shell命令历史记录:

Operating System操作系统Path to History File历史文件的路径
macOS and Linux~/.mongodb/mongosh/mongosh_repl_history
Windows%UserProfile%/.mongodb/mongosh/mongosh_repl_history

Log Retention日志保留

mongosh retains log files for 30 days. Log files older than 30 days are automatically deleted.将日志文件保留30天。超过30天的日志文件将自动删除。