对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

getLog(数据库命令)

getLog

getLog是一条管理命令,用于返回最近 记录的1024 mongodgetLog事件。 mongod不从mongod 日志文件中读取日志数据。相反,它会从记录的 事件的RAM缓存中读取数据。要运行getLog ,请使用db.adminCommand() 方法。

getLog以转义的宽松扩展JSON2 v.0 格式返回日志数据。以前,日志数据以明文形式返回。

此命令可用于以下环境中托管的部署:

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

注意

所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令

该命令具有以下语法:

db.adminCommand(
{
getLog: <value>
}
)

The possible values for getLog are:

说明

*

Returns a list of the available values to the getLog command.

global

返回所有最近日志条目的组合输出。

startupWarnings

返回日志条目,其中可能包含当前进程启动时 MongoDB 日志中出现的错误或警告。如果 mongod 启动时没有出现警告,则此筛选器可能会返回一个空数组。

注意

The getLog command no longer accepts the rs value, as this categorization of message type has been deprecated. Instead, log messages are now always identified by their component - including REPL for replication messages. See Filtering by Component for log parsing examples that filter on the component field.

如果指定*,则命令返回一个包含其他可接受值名称的文档。

否则,该命令会返回包含以下字段的文档:

  • totalLinesWritten 字段,包含日志事件数

  • log 字段,包含日志事件数组

  • 包含状态和时间戳信息的 db.adminCommand() 响应文档

getLog 截断任何包含超过 1024 个字符的事件。

getLog以转义的宽松扩展JSON2 v.0 格式返回日志数据,并使用以下转义序列将日志输出呈现为有效的JSON:

所代表的字符
转义序列

引号 (")

\"

Backslash (\)

\\

退格 (0x08)

\b

进纸 (0x0C)

\f

换行符 (0x0A)

\n

回车 (0x0D)

\r

Horizontal tab (0x09)

\t

上面未列出的控制字符使用 \uXXXX 进行转义,其中 "XXXX" 是十六进制的 unicode 代码点。具有无效 UTF-8 编码的字节将替换为 \ufffd 表示的 unicode 替换字符。

getLog 输出可以进行过滤,以使结果更具可读性或与特定条件相匹配。

以下操作仅打印 log 字段(其中包含所有最近日志事件的数组),并删除每条日志消息中的字符转义

db.adminCommand( { getLog:'global'} ).log.forEach(x => {print(x)})

This operation presents getLog output in the same format as the MongoDB log file.

注意

getLog仅显示最近1024 mongod记录的 事件,并且不能替代MongoDBlog file

在使用 MongoDB 结构化日志时,您可以使用第三方 jq 命令行实用程序轻松美观地打印日志条目,并提供强大的基于键的匹配和过滤功能。

jq 是一个开源 JSON 解析器,用于 Linux、Windows 和 macOS。

To use jq with getLog output, you must use the --eval option to mongosh. The following operation uses jq to filter on the REPL component to present only those log messages associated with replication:

mongosh --quiet --eval "db.adminCommand( { getLog:'global'} ).log.forEach(x => {print(x)})" | jq -c '. | select(.c=="REPL")'

Be sure to provide any necessary connection-specific parameters to mongosh as needed, such as --host or --port.

See Parsing Structured Log Messages for more examples of filtering log output using jq. The jq syntax presented in each linked example can be used with the above mongo --eval operation with minor adjustment. For example, the following syntax adapts the linked "Counting Unique Messages" example for use with getLog:

mongosh --quiet --eval "db.adminCommand( { getLog:'global'} ).log.forEach(x => {print(x)})" | jq -r ".msg" | sort | uniq -c | sort -rn | head -10

The following operation, run from mongosh, returns the available log filters for passing to getLog:

db.adminCommand( { getLog: "*" } )

该操作将返回以下文档:

{ "names" : [ "global", "startupWarnings" ], "ok" : 1 }

The following operation, run from mongosh, retrieves the most recent global events for the mongod:

db.adminCommand( { getLog : "global" } )

该操作返回类似于以下内容的文档:

{
"totalLinesWritten" : <num>,
"log" : [
"{\"t\":{\"$date\":\"2020-05-19T19:10:48.871+00:00\"},\"s\":\"I\", \"c\":\"STORAGE\", \"id\":4615611, \"ctx\":\"initandlisten\",\"msg\":\"MongoDB starting\",\"attr\":{\"pid\":12345,\"port\":27001,\"dbPath\":\"/var/lib/mongo\",\"architecture\":\"64-bit\",\"host\":\"server1.example.com\"}}",
"{\"t\":{\"$date\":\"2020-05-19T19:10:48.871+00:00\"},\"s\":\"I\", \"c\":\"CONTROL\", \"id\":23403, \"ctx\":\"initandlisten\",\"msg\":\"Build Info\",\"attr\":{\"buildInfo\":{\"version\":\"4.4.0\",\"gitVersion\":\"328c35e4b883540675fb4b626c53a08f74e43cf0\",\"openSSLVersion\":\"OpenSSL 1.1.1c FIPS 28 May 2019\",\"modules\":[],\"allocator\":\"tcmalloc\",\"environment\":{\"distmod\":\"rhel80\",\"distarch\":\"x86_64\",\"target_arch\":\"x86_64\"}}}}",
"{\"t\":{\"$date\":\"2020-05-19T19:10:48.871+00:00\"},\"s\":\"I\", \"c\":\"CONTROL\", \"id\":51765, \"ctx\":\"initandlisten\",\"msg\":\"Operating System\",\"attr\":{\"os\":{\"name\":\"CentOS Linux release 8.0.1905 (Core) \",\"version\":\"Kernel 4.18.0-80.11.2.el8_0.x86_64\"}}}",
"{\"t\":{\"$date\":\"2020-05-19T19:10:48.871+00:00\"},\"s\":\"I\", \"c\":\"CONTROL\", \"id\":21951, \"ctx\":\"initandlisten\",\"msg\":\"Options set by command line\",\"attr\":{\"options\":{\"config\":\"/etc/mongod.conf\",\"net\":{\"bindIp\":\"127.0.0.1\",\"port\":27001},\"processManagement\":{\"fork\":true,\"timeZoneInfo\":\"/usr/share/zoneinfo\"},\"replication\":{\"replSetName\":\"repl-shard1\"},\"sharding\":{\"clusterRole\":\"shardsvr\"},\"storage\":{\"dbPath\":\"/var/lib/mongo\",\"journal\":{\"enabled\":true},\"wiredTiger\":{\"engineConfig\":{\"cacheSizeGB\":0.1}}},\"systemLog\":{\"destination\":\"file\",\"logAppend\":true,\"path\":\"/var/log/mongodb/mongod.log\"}}}}",
"{\"t\":{\"$date\":\"2020-05-19T19:10:48.873+00:00\"},\"s\":\"I\", \"c\":\"STORAGE\", \"id\":22270, \"ctx\":\"initandlisten\",\"msg\":\"Storage engine to use detected by data files\",\"attr\":{\"dbpath\":\"/var/lib/mongo\",\"storageEngine\":\"wiredTiger\"}}",
"{\"t\":{\"$date\":\"2020-05-19T19:10:48.873+00:00\"},\"s\":\"I\", \"c\":\"STORAGE\", \"id\":22315, \"ctx\":\"initandlisten\",\"msg\":\"wiredtiger_open config\",\"attr\":{\"config\":\"create,cache_size=102M,session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress,compact_progress],\"}}",
"{\"t\":{\"$date\":\"2020-05-19T19:10:58.877+00:00\"},\"s\":\"I\", \"c\":\"CONNPOOL\", \"id\":22576, \"ctx\":\"ReplicaSetMonitor-TaskExecutor\",\"msg\":\"Connecting\",\"attr\":{\"hostAndPort\":\"server1.example.com:27001\"}}",
"{\"t\":{\"$date\":\"2020-05-19T19:10:58.877+00:00\"},\"s\":\"I\", \"c\":\"CONNPOOL\", \"id\":22576, \"ctx\":\"ReplicaSetMonitor-TaskExecutor\",\"msg\":\"Connecting\",\"attr\":{\"hostAndPort\":\"server2.example.com:27001\"}}",
"{\"t\":{\"$date\":\"2020-05-19T19:10:58.877+00:00\"},\"s\":\"I\", \"c\":\"CONNPOOL\", \"id\":22576, \"ctx\":\"ReplicaSetMonitor-TaskExecutor\",\"msg\":\"Connecting\",\"attr\":{\"hostAndPort\":\"server3.example.com:27001\"}}",
"{\"t\":{\"$date\":\"2020-05-19T19:10:58.877+00:00\"},\"s\":\"I\", \"c\":\"CONNPOOL\", \"id\":22576, \"ctx\":\"ReplicaSetMonitor-TaskExecutor\",\"msg\":\"Connecting\",\"attr\":{\"hostAndPort\":\"server4.example.com:27001\"}}",
"{\"t\":{\"$date\":\"2020-05-19T19:10:58.877+00:00\"},\"s\":\"I\", \"c\":\"CONNPOOL\", \"id\":22576, \"ctx\":\"ReplicaSetMonitor-TaskExecutor\",\"msg\":\"Connecting\",\"attr\":{\"hostAndPort\":\"server5.example.com:27001\"}}",
"{\"t\":{\"$date\":\"2020-05-19T19:10:58.877+00:00\"},\"s\":\"I\", \"c\":\"CONNPOOL\", \"id\":22576, \"ctx\":\"ReplicaSetMonitor-TaskExecutor\",\"msg\":\"Connecting\",\"attr\":{\"hostAndPort\":\"server6.example.com:27001\"}}",
"{\"t\":{\"$date\":\"2020-05-19T19:10:58.877+00:00\"},\"s\":\"I\", \"c\":\"NETWORK\", \"id\":23015, \"ctx\":\"listener\",\"msg\":\"Listening on\",\"attr\":{\"address\":\"/tmp/mongodb-27001.sock\"}}",
"{\"t\":{\"$date\":\"2020-05-19T19:10:58.877+00:00\"},\"s\":\"I\", \"c\":\"NETWORK\", \"id\":23015, \"ctx\":\"listener\",\"msg\":\"Listening on\",\"attr\":{\"address\":\"127.0.0.1\"}}",
"{\"t\":{\"$date\":\"2020-05-19T19:10:58.877+00:00\"},\"s\":\"I\", \"c\":\"NETWORK\", \"id\":23016, \"ctx\":\"listener\",\"msg\":\"Waiting for connections\",\"attr\":{\"port\":27001,\"ssl\":\"off\"}}",
"{\"t\":{\"$date\":\"2020-05-19T19:15:10.392+00:00\"},\"s\":\"I\", \"c\":\"NETWORK\", \"id\":22943, \"ctx\":\"listener\",\"msg\":\"connection accepted\",\"attr\":{\"remote\":\"127.0.0.1:35724\",\"sessionId\":67,\"connectionCount\":30}}",
"{\"t\":{\"$date\":\"2020-05-19T19:15:10.393+00:00\"},\"s\":\"I\", \"c\":\"NETWORK\", \"id\":51800, \"ctx\":\"conn67\",\"msg\":\"client metadata\",\"attr\":{\"remote\":\"127.0.0.1:35724\",\"client\":\"conn67\",\"doc\":{\"application\":{\"name\":\"MongoDB Shell\"},\"driver\":{\"name\":\"MongoDB Internal Client\",\"version\":\"4.4.0\"},\"os\":{\"type\":\"Linux\",\"name\":\"CentOS Linux release 8.0.1905 (Core) \",\"architecture\":\"x86_64\",\"version\":\"Kernel 4.18.0-80.11.2.el8_0.x86_64\"}}}}"
],
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(<ts>),
"electionId" : ObjectId(<id>)
},
"lastCommittedOpTime" : Timestamp(<ts>),
"$configServerState" : {
"opTime" : {
"ts" : Timestamp(<ts>),
"t" : Long(8)
}
},
"$clusterTime" : {
"clusterTime" : Timestamp(<ts>),
"signature" : {
"hash" : BinData(<bin>),
"keyId" : Long(0)
}
},
"operationTime" : Timestamp(<ts>)
}