我目前正在寻找最简单的 JSON 日志消息,这些消息可以简单地将严重性和消息从在 Kubernetes Engine 中运行并使用托管 Fluentd 守护程序的容器写入 Stackdriver 日志记录。
基本上我正在编写单行 JSON 条目,如下所示。
{"severity": "DEBUG", "message": "I'm a debug entry"}
{"severity": "ERROR", "message": "I'm an error entry"}
这些最终会在 Stackdriver 日志记录中得到以下结果。
我的结论是 Fluentd 将日志行识别为 JSON,但我不明白的是严重性如何没有正确设置到日志条目中。我是例如缺少一些需要到位的必填字段?
最佳答案
根据您提供的信息,我猜 fluentd 正在将您的整个 JSON 作为 jsonpayload 作为 logEntry 传递。并从环境变量中提供日志名称、资源类型和其他所需信息。
最后,Stackdriver 收到的内容必须如下所示:
{
"logName": "projects/[YOUR PROJECT ID]/logs/[KUBERNETES LOG]",
"entries": [
{
"jsonPayload": {
"message": "I'm an ERROR entry",
"severity": "ERROR"
},
"resource": {
"labels": {
"project_id": "[YOUR PROJECT ID]",
"instance_id": "[WHATEVER]",
"zone": "[YOUR ZONE]"
},
"type": "gce_instance"
}
}
]
}
因此,您实际上是在 Stackdriver 上获取 JSON 有效负载的内容,但由于严重性定义为 外面 JSON 有效负载,或者,如果您想在内部执行此操作,则必须使用
"severity": enum([NUMERICAL VALUE])
The numerical values of each log level are:
Enums
DEFAULT (0) The log entry has no assigned severity level.
DEBUG (100) Debug or trace information.
INFO (200) Routine information, such as ongoing status or performance.
NOTICE (300) Normal but significant events, such as start up, shut down, or a configuration change.
WARNING (400) Warning events might cause problems.
ERROR (500) Error events are likely to cause problems.
CRITICAL (600) Critical events cause more severe problems or outages.
ALERT (700) A person must take an action immediately.
EMERGENCY (800) One or more systems are unusable.
所以,包括字段
"severity": enum(500)
应该将条目记录为 ERROR 而不是回退到默认 INFO。
关于kubernetes - 用于 Google Kubernetes Engine 中 Stackdriver 日志记录的 JSON 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49283233/