如何编写结构化日志以便 GCP 日志查看器使用消息字段?
How to write structured logs so that message field is used by GCP Log Viewer?
问题
有没有办法从 Cloud Functions 编写结构化日志,以便消息字段自动显示为 GCP 日志查看器中的主要消息?
使用 this test,我发现有时会使用名为 message 的字段。例如,给定这些日志:
{"severity":"trace","time":"2020-03-09T12:21:13.660044125-07:00","message":"Trace Basic"}
{"severity":"debug","time":"2020-03-09T12:21:13.660047625-07:00","message":"Debug Basic"}
{"severity":"info","time":"2020-03-09T12:21:13.660049425-07:00","message":"Info Basic"}
{"severity":"warn","time":"2020-03-09T12:21:13.660051425-07:00","message":"Warn Basic"}
{"severity":"error","time":"2020-03-09T12:21:13.660053225-07:00","message":"Error Basic"}
{"severity":"info","time":"2020-03-09T12:21:13.660055125-07:00","message":"One line message"}
{"severity":"info","time":"2020-03-09T12:21:13.660057025-07:00","message":"Line 1 of 2\nLine 2 of 2 for zlog.Info"}
{"severity":"info","myIntField":532,"myStringField":"howdy","myMultilineStringField":"Line 1 of 2\nLine 2 of 2 for zlog.Info with fields","time":"2020-03-09T12:21:13.660059925-07:00","message":"With Fields Example"}
GCP 日志查看器将显示如下内容:
注意最后一个条目,它应该有一条消息 With Fields Example 而不是一条顶级消息 { "fields": { ... } }.
额外的细节
Cloud 运行 有一个 document describing special structured logging fields(即严重性和消息),Stackdriver 日志记录将自动获取并用于填充日志条目的 DEBUG/WARN/INFO/ERROR 图标和顶级消息在 Stackdriver 控制台中。
Special JSON fields in messages
When you provide a structured log as a JSON dictionary, some special
fields are stripped from the jsonPayload and are written to the
corresponding field in the generated LogEntry as described in the
documentation for special fields.
For example, if your JSON includes a severity property, it is removed
from the jsonPayload and appears instead as the log entry's severity.
The message property is used as the main display text of the log entry
if present. For more on special properties read the Logging Resource
section below.
corresponding document for Cloud Functions没有关于特殊字段的信息。
这是 Google Cloud Platform 日志记录中的错误,已修复。
问题
有没有办法从 Cloud Functions 编写结构化日志,以便消息字段自动显示为 GCP 日志查看器中的主要消息?
使用 this test,我发现有时会使用名为 message 的字段。例如,给定这些日志:
{"severity":"trace","time":"2020-03-09T12:21:13.660044125-07:00","message":"Trace Basic"}
{"severity":"debug","time":"2020-03-09T12:21:13.660047625-07:00","message":"Debug Basic"}
{"severity":"info","time":"2020-03-09T12:21:13.660049425-07:00","message":"Info Basic"}
{"severity":"warn","time":"2020-03-09T12:21:13.660051425-07:00","message":"Warn Basic"}
{"severity":"error","time":"2020-03-09T12:21:13.660053225-07:00","message":"Error Basic"}
{"severity":"info","time":"2020-03-09T12:21:13.660055125-07:00","message":"One line message"}
{"severity":"info","time":"2020-03-09T12:21:13.660057025-07:00","message":"Line 1 of 2\nLine 2 of 2 for zlog.Info"}
{"severity":"info","myIntField":532,"myStringField":"howdy","myMultilineStringField":"Line 1 of 2\nLine 2 of 2 for zlog.Info with fields","time":"2020-03-09T12:21:13.660059925-07:00","message":"With Fields Example"}
GCP 日志查看器将显示如下内容:
注意最后一个条目,它应该有一条消息 With Fields Example 而不是一条顶级消息 { "fields": { ... } }.
额外的细节
Cloud 运行 有一个 document describing special structured logging fields(即严重性和消息),Stackdriver 日志记录将自动获取并用于填充日志条目的 DEBUG/WARN/INFO/ERROR 图标和顶级消息在 Stackdriver 控制台中。
Special JSON fields in messages
When you provide a structured log as a JSON dictionary, some special fields are stripped from the jsonPayload and are written to the corresponding field in the generated LogEntry as described in the documentation for special fields.
For example, if your JSON includes a severity property, it is removed from the jsonPayload and appears instead as the log entry's severity. The message property is used as the main display text of the log entry if present. For more on special properties read the Logging Resource section below.
corresponding document for Cloud Functions没有关于特殊字段的信息。
这是 Google Cloud Platform 日志记录中的错误,已修复。