简体   繁体   English

使用 Fluentd 进行日志记录 - 为什么 json 日志文件的输出显示为 textpayload(而不是 jsonpayload)?

[英]Logging with Fluentd - why is the output of json log file appearing as textpayload (not jsonpayload)?

I am new to Fluentd.我是 Fluentd 的新手。 I'm using stackdriver in GKE and I am customizing the Fluentd configuration in GKE to make some changes to the logs.我在 GKE 中使用 stackdriver 并且我正在 GKE 中自定义 Fluentd 配置以对日志进行一些更改。

In my configuration file for the logs of my containers I have:在我的容器日志的配置文件中,我有:

<source>
  type tail
  format json
  time_key time
  path /var/log/containers/*.log
  pos_file /var/log/gcp-containers.log.pos
  time_format %Y-%m-%dT%H:%M:%S.%N%Z
  tag reform.*
  read_from_head true
</source>

The logs of some containers are json objests, but I see their output as textpayload (when I enable the built-in Fluentd on GKE they appear as jsonpayload).某些容器的日志是 json 对象,但我将它们的输出视为 textpayload(当我在 GKE 上启用内置 Fluentd 时,它们显示为 jsonpayload)。

I don't understand what could cause this.我不明白是什么导致了这种情况。 I would appreciate any advice.我将不胜感激任何建议。

I'm not sure if this answer will cover your case, but it may save few hours of investigation to someone like it could have to me.我不确定这个答案是否会涵盖您的情况,但它可能会为我这样的人节省几个小时的调查时间。

Although format parameter is now deprecated and replaced with <parse> , it does support json parsing.尽管格式参数现在已弃用并替换为<parse> ,但它确实支持 json 解析。

The crucial thing here is JSON object structure .这里的关键是 JSON 对象结构
First nginx access log example that I've found do NOT work with Stackdriver without modification:首先nginx的访问日志的例子,我发现与为Stackdriver工作无需修改:

log_format json_combined escape=json '{ "time_local": "$time_local", '
 '"remote_addr": "$remote_addr", '
 '"remote_user": "$remote_user", '
 '"request": "$request", '
 '"status": "$status", '
 '"body_bytes_sent": "$body_bytes_sent", '
 '"request_time": "$request_time", '
 '"http_referrer": "$http_referer", '
 '"http_user_agent": "$http_user_agent" }';

And this doesn't work because JSON is single level and Stackdriver requires it to be nested at least once under a single key.这不起作用,因为 JSON 是单级的,Stackdriver 要求它在单个键下至少嵌套一次。 (At least in this example with fluentd agent, not sure for other cases) (至少在这个使用 fluentd agent 的例子中,其他情况不确定)

This can be any key like accessLog in WORKING example below.这可以是任何键,例如下面WORKING 示例中的accessLog

log_format json_combined escape=json '{ "accessLog": { "time_local": "$time_local", '
 '"remote_addr": "$remote_addr", '
 '"remote_user": "$remote_user", '
 '"request": "$request", '
 '"status": "$status", '
 '"body_bytes_sent": "$body_bytes_sent", '
 '"request_time": "$request_time", '
 '"http_referrer": "$http_referer", '
 '"http_user_agent": "$http_user_agent" } }';

If using httpRequest on the first JSON level be warned that it has different purpose .如果在第一个 JSON 级别使用httpRequest ,请注意它具有不同的用途

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

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