Database Manual / Reference / Database Commands / Diagnostics

getLog (database command数据库命令)

Definition定义

getLog

getLog is an administrative command that returns the most recent 1024 logged events for a mongod or mongos instance. getLog是一个管理命令,它返回mongodmongos实例最近记录的1024个事件。getLog does not read log data from the log file. Instead, it reads data from a RAM cache of logged events from either mongod or mongos. 不从日志文件读取日志数据。相反,它从mongodmongos的记录事件的RAM缓存中读取数据。To run getLog, use the db.adminCommand() method.要运行getLog,请使用db.adminCommand()方法。

getLog returns log data in escaped Relaxed Extended JSON v2.0 format. Previously, log data was returned as plaintext.以转义的Relaxed Extended JSON v2.0格式返回日志数据。以前,日志数据以明文形式返回。

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The command has the following syntax:该命令具有以下语法:

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

Command Fields命令字段

The possible values for getLog are:getLog的可能值有:

ValueDescription描述
*Returns a list of the available values to the getLog command.getLog命令返回可用值的列表。
globalReturns the combined output of all recent log entries.返回所有最近日志条目的组合输出。
startupWarningsReturns log entries that may contain errors or warnings from MongoDB's log from when the current process started. If the process started without warnings, this filter may return an empty array.返回可能包含当前进程启动时MongoDB日志中的错误或警告的日志条目。如果进程在没有警告的情况下启动,则此筛选器可能会返回一个空数组。

Note

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. getLog命令不再接受rs值,因为这种消息类型的分类已被弃用。相反,日志消息现在总是由其组件标识的,包括复制消息的REPL。See Filtering by Component for log parsing examples that filter on the component field.有关在组件字段上筛选的日志解析示例,请参阅按组件筛选

Output输出

If specified *, then the command returns a document with the names of the other acceptable values.如果指定了*,则该命令将返回一个包含其他可接受值名称的文档。

Otherwise, the command returns a document that includes the following fields:否则,该命令将返回一个包含以下字段的文档:

  • totalLinesWritten field that contains the number of log events包含日志事件的数量的totalLinesWriten字段
  • log field that contains an array of log events包含日志事件数组的log字段
  • A db.adminCommand() response document, containing status and timestamp information.db.adminCommand()响应文档,包含状态和时间戳信息。

Behavior行为

Line Truncation线路截断

getLog truncates any event that contains more than 1024 characters.截断包含1024个以上字符的任何事件。

Character Escaping角色逃脱

getLog returns log data in escaped Relaxed Extended JSON v2.0 format, using the following escape sequences to render log output as valid JSON:以转义的Relaxed Extended JSON v2.0格式返回日志数据,使用以下转义序列将日志输出呈现为有效的JSON:

Character Represented代表字符Escape Sequence转义序列
Quotation Mark引号 (")\"
Backslash反斜杠 (\)\\
Backspace退格 (0x08)\b
Formfeed制表符 (0x0C)\f
Newline换行 (0x0A)\n
Carriage return回车 (0x0D)\r
Horizontal tab水平制表 (0x09)\t

Control characters not listed above are escaped with \uXXXX where "XXXX" is the unicode codepoint in hexadecimal. Bytes with invalid UTF-8 encoding are replaced with the unicode replacement character represented by \ufffd.上面未列出的控制字符用\uXXXX转义,其中“XXXX”是十六进制的unicode代码点。UTF-8编码无效的字节将被替换为\ufffd表示的unicode替换字符。

Filtering筛选

Within mongoshmongosh内部

getLog output can be filtered to make results more readable or to match on specific criteria.可以筛选输出,使结果更具可读性或符合特定标准。

The following operation prints just the log field (which contains the array of all recent log events), and removes character escaping from each log message:以下操作仅打印log字段(其中包含所有最近日志事件的数组),并从每条日志消息中删除转义字符

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

This operation presents getLog output in the same format as the MongoDB log file.此操作以与MongoDB日志文件相同的格式显示getLog输出。

Note

getLog only shows the most recent 1024 logged events, and is not a replacement for the MongoDB log file.仅显示最近记录的1024个事件,不能替代MongoDB日志文件

Outside mongosh with jqmongoshjq

When working with MongoDB structured logging, you can use the third-party jq command-line utility for easy pretty-printing of log entries, and powerful key-based matching and filtering.使用MongoDB结构化日志记录时,您可以使用第三方jq命令行实用程序轻松打印日志条目,以及强大的基于键的匹配和筛选。

jq is an open-source JSON parser, and is available for Linux, Windows, and macOS.是一个开源JSON解析器,适用于Linux、Windows和macOS。

To use jq with getLog output, you must use the --eval option to mongosh. 要在getLog输出中使用jq,必须使用mongosh--eval选项。The following operation uses jq to filter on the REPL component to present only those log messages associated with replication:以下操作使用jq对REPL组件进行筛选,仅显示与复制相关的日志消息:

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.请确保根据需要向mongosh提供任何必要的特定于连接的参数,例如--host--port

See Parsing Structured Log Messages for more examples of filtering log output using jq. 有关使用jq筛选日志输出的更多示例,请参阅解析结构化日志消息The jq syntax presented in each linked example can be used with the above mongo --eval operation with minor adjustment. 每个链接示例中显示的jq语法可以与上述mongo --eval操作一起使用,只需稍作调整。For example, the following syntax adapts the linked "Counting Unique Messages" example for use with getLog:例如,以下语法将链接的“计数唯一消息”示例调整为与getLog一起使用:

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

Examples示例

Retrieve Available Log Filters检索可用日志筛选器

The following operation, run from mongosh, returns the available log filters for passing to getLog:以下操作从mongosh运行,返回可用的日志筛选器以传递给getLog

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

The operation returns the following document:该操作返回以下文档:

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

Retrieve Recent Events from Log从日志中检索最近的事件

The following operation, run from mongosh, retrieves the most recent global events for a mongod or mongos instance:以下操作从mongosh运行,检索mongodmongos实例的最新全局事件:

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

The operation returns a document similar to the following:该操作返回一个类似于以下内容的文档:

{
"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>)
}