菜单
开源

logfmt

注意

Promtail 已被弃用,并将在 2026 年 2 月 28 日之前处于长期支持 (LTS) 状态。Promtail 将于 2026 年 3 月 2 日达到生命周期结束 (EOL)。您可以在此处找到迁移资源。

logfmt 阶段是一个解析阶段,它将日志行读取为 logfmt 格式,并允许将数据提取到标签中。

Schema

yaml
logfmt:
  # Set of key/value pairs for mapping of logfmt fields to extracted labels. The YAML key will be
  # the key in the extracted data, while the expression will be the YAML value. If the value
  # is empty, then the logfmt field with the same name is extracted.
  mapping:
    [ <string>: <string> ... ]

  # Name from extracted data to parse. If empty, uses the log message.
  [source: <string>]

此阶段使用 go-logfmt unmarshaler,这意味着非字符串类型(如数字或布尔值)将解析为相应类型。提取的数据可以包含非字符串值,且此阶段不做任何类型转换;下游阶段需要根据需要执行正确的类型转换。有关如何执行此操作,请参阅template 阶段

如果提取的值是复杂类型,则其值将提取为字符串。

示例

使用日志行

对于给定的管道

yaml
- logfmt:
    mapping:
      timestamp: time
      app:
      duration:
      unknown:

给定以下日志行

time=2012-11-01T22:08:41+00:00 app=loki level=WARN duration=125 message="this is a log line" extra="user=foo""

在提取的数据集合中将创建以下键值对

  • timestamp: 2012-11-01T22:08:41+00:00
  • app: loki
  • duration: 125

使用提取的数据

对于给定的管道

yaml
- logfmt:
    mapping:
      extra:
- logfmt:
    mapping:
      user:
    source: extra

以及给定的日志行

time=2012-11-01T22:08:41+00:00 app=loki level=WARN duration=125 message="this is a log line" extra="user=foo"

第一个阶段将在提取的数据集合中创建以下键值对

  • extra: user=foo

第二个阶段将从提取的数据中将 extra 的值解析为 logfmt 格式,并将以下键值对附加到提取的数据集合中

  • user: foo