Google Stackdriver日志条目中的logging.googleapis.com/severity字段与日志级别
时间:2024-02-27
浏览:380
作者:佚名
golang学习网今天将给大家带来《Google Stackdriver日志条目中的logging.googleapis.com/severity字段与日志级别》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到等等知识点,如果你是正在学习Golang或者已经是大佬级别了,都非常欢迎也希望大家都能给我建议评论哈~希望能帮助到大家!
我有一个通用日志包,其中包含 stackdriver 的日志 - 获取上下文、严重性等并将其转换为 logentry,即:
func log(ctx context.context, severity severity, format string, a ...interface{}) {
log.println(entry{severity: severity, message: fmt.sprintf(format, a...), component: "arbitrary-property", trace: gettraceid(ctx)})
}
入口结构看起来像这样
type entry struct {
message string `json:"message"`
severity severity `json:"severity,omitempty"`
trace string `json:"logging.googleapis.com/trace,omitempty"`
// logs explorer allows filtering and display of this as `jsonpayload.component`.
component string `json:"component,omitempty"`
}
此软件包是全局软件包,可用于 cloud run 和 compute engine 中的多种服务。
compute engine 中的日志是通过 google ops agent 提取的
如果我希望日志严重性与 ops 代理一起使用,我需要根据文档使用此 json 键 logging.googleapis.com/severity:https://cloud.google.com/stackdriver/docs/solutions/agents/ops -代理/配置#特殊字段
否则,严重性仍然是 jsonpayload 的一部分,并且严重性不会应用于日志条目。
但这与此处的文档冲突:https://cloud.google.com/logging/docs/reference/v2/rest/v2/logentry
这里说 json 字段应该是简单的 severity ,没有前缀 logging.googleapis.com/ 。
如果我使用这个结构
type entry struct {
...
severity severity `json:"severity,omitempty"`
}
如果我使用此结构,它无法与 ops agent 一起使用
type Entry struct {
...
Severity Severity `json:"logging.googleapis.com/severity,omitempty"`
}
它适用于 ops agent,但不适用于 cloud run(我只是将 logging.googleapis.com/severity 视为 jsonpayload 的一部分,严重性不适用)。
这是预期的行为吗?是否有解决方法,无需创建两个包?
正确答案
这是预期的行为。对于这种特定情况没有解决方法,因此您需要构建两个包。
在 Cloud Run 中,您可以阅读 here 关于消息中的特殊 JSON 字段,当您提供结构化日志作为 JSON 字典时,一些特殊字段将从 jsonPayload 中剥离,并写入生成的 LogEntry 中的相应字段,如下所示special fields 的文档中进行了描述。
例如,如果您的 JSON 包含 severity 属性,它会从 jsonPayload 中删除,并显示为日志条目的 severity。 message 属性用作日志条目的主要显示文本(如果存在)。有关特殊属性的更多信息,请阅读 Logging Resource 部分。
文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《Google Stackdriver日志条目中的logging.googleapis.com/severity字段与日志级别》文章吧,也可关注golang学习网公众号了解相关技术文章。
本文内容来源于互联网,如有侵权请联系删除。
近期热门
站内热门
最新文章
热门专题
