fluentd 格式化 format(六)

警告
本文最后更新于 2020-06-01 16:18,文中内容可能已过时。

format 部分可以位于部分中。

format 部分需要 @type 参数来指定格式化程序插件的类型。 fluentd 内置了一些有用的格式化程序插件。安装第三方插件时也可以使用

1
2
3
<format>
  @type json
</format>

下面是一些内置的格式化插件:

  • @type:指定插件类型
  • time_type:时间类型

    • 默认值:float
    • 可选值:float, unixtime, string
      • float: 纪元 + 纳秒 (例如:1510544836.154709804)
      • unixtime: 纪元 (例如:1510544815)
      • string: 使用由 time_format、本地时间或时区指定的格式
  • time_format:时间格式

    • 默认值:nil
  • localtime:如果为真,使用本地时间。否则,使用 UTC

    • 默认值:true
  • utc:如果为真,使用 UTC。否则,使用本地时间

    • 默认值:false
  • timezone:指定时区

    • 默认值:nil
    • 可用的时区格式:
      1. [+-] HH:MM (例如:+09:00)
      2. [+-] HHMM (例如:+0900)
      3. [+-] HH (例如:+09)
      4. Region/Zone (例如:Asia/Tokyo)
      5. Region/Zone/Zone (例如:America/Argentina/Buenos_Aires)

json 格式化插件将事件转换为 json。默认情况下,json 格式化程序结果不包含标签和时间字段。

可用参数:

下面的配置是从 /var/log/test.log 文件中读取内容,并通过 stdout 插件打印到屏幕中

我们先来看一下不使用 format 的显示结果

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<source>
    @type tail
    tag   test.aa
    path  /var/log/test.log
    pos_file /tmp/test.log.pos
    <parse **>
      @type none
    </parse>
</source>

<match **>
  @type stdout
 # <format>
 #   @type json
 #   add_newline false
 # </format>
</match>

模拟生成日志:echo 'test line 1' >> /var/log/test.log

19451-utsp60u6qw.png
19451-utsp60u6qw.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<source>
    @type tail
    tag   test.aa
    path  /var/log/test.log
    pos_file /tmp/test.log.pos
    <parse **>
      @type none
    </parse>
</source>

<match test.**>
  @type stdout
  <format>
    @type json
    add_newline false
  </format>
</match>

启动服务 td-agent -c demo2.conf

模拟生成日志:echo 'test line 1' >> /var/log/test.log, 可以看到日志只保留了 json 格式的部分

72820-tetd7qoj5d.png
72820-tetd7qoj5d.png

请我喝杯水
SoulChild 微信号 微信号
SoulChild 微信打赏 微信打赏
0%