简体   繁体   English

如何将 JSON 从 GCE 容器 VM 记录到 Stackdriver?

[英]How to log JSON from a GCE Container VM to Stackdriver?

I'm currently using GCE Container VMs (not GKE) to run Docker Containers which write their JSON formated log to the Console.我目前正在使用 GCE 容器虚拟机(不是 GKE)来运行 Docker 容器,这些容器将其 JSON 格式的日志写入控制台。 The Log Information is automatically collected and stored in Stackdriver.日志信息会自动收集并存储在 Stackdriver 中。

Problem: Stackdriver displays the data -field of the jsonPayload as text - not as JSON.问题: jsonPayloadjsonPayloaddata字段显示为文本 - 而不是 JSON。 It looks like the quotes of the fields within the payload are escaped and therefore not recognized as JSON structure.看起来有效负载中字段的引号被转义,因此不被识别为 JSON 结构。

I used both, logback-classic (like explained here ) and slf4j/log4j (using JSONPattern) to generate JSON output (which looks fine), but the output is not parsed correctly.我同时使用了 logback-classic(就像这里解释的那样)和 slf4j/log4j(使用 JSONPattern)来生成 JSON 输出(看起来不错),但是输出没有正确解析。

I assume that, I have to configure somewhere that the output is JSON structured, not plain text.我假设,我必须在某处配置输出是 JSON 结构的,而不是纯文本。 So far I haven't found an option where to do this when using a Container VM.到目前为止,我还没有找到在使用容器 VM 时执行此操作的选项。

What does your logger output into stdout?你的记录器输出什么到标准输出?

You shouldn't create a jsonPayload field yourself in your log output.您不应在日志输出中自己创建jsonPayload字段。 That field gets automatically created when your logs get parsed and meet certain criteria.当您的日志被解析并满足某些条件时,该字段会自动创建。

Basically, write your log message into a message field of your JSON output and any additional data as additional fields.基本上,将您的日志消息写入 JSON 输出的message字段,并将任何附加数据作为附加字段写入。 Stackdriver strips all special fields from your JSON payload and if there is nothing left then your message will end up as textPayload otherwise you'll get a jsonPayload with your message and other fields. Stackdriver 从您的 JSON 负载中textPayload所有特殊字段,如果没有任何内容,那么您的消息将以textPayload结束,否则您将获得带有消息和其他字段的jsonPayload

Full documentation is here: https://cloud.google.com/logging/docs/structured-logging完整文档在这里: https : //cloud.google.com/logging/docs/structured-logging

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM