温馨提示×

ubuntu filebeat怎样过滤日志内容

小樊
36
2026-10-25 16:22:40
栏目: 智能运维

在 Ubuntu 上使用 Filebeat 过滤日志内容,通常有三种常见方式,分别适用于不同场景:


一、使用 Filebeat 自带 processors(推荐,轻量)

Filebeat 支持在 filebeat.yml 中使用 processors 对日志做过滤、丢弃、字段处理。

1️⃣ 丢弃含有某些关键字的日志(drop_event)

filebeat.inputs:
- type: log
  paths:
    - /var/log/app/*.log
  processors:
    - drop_event.when.contains:
        message: "health check"

➡ 含义:日志内容包含 health check 的直接丢弃,不发送到 ES/Logstash。


2️⃣ 只保留某些日志(相反逻辑)

processors:
  - drop_event.when.not.contains:
      message: "ERROR"

3️⃣ 按正则表达式过滤

processors:
  - drop_event.when.regexp:
      message: "^DEBUG"

4️⃣ 只采集特定字段(include_fields)

processors:
  - include_fields:
      fields: ["message", "timestamp"]

5️⃣ 删除字段(exclude_fields)

processors:
  - exclude_fields:
      fields: ["agent.version"]

二、在输入层做简单过滤(exclude_lines / include_lines)

适用于行级别简单过滤:

filebeat.inputs:
- type: log
  paths:
    - /var/log/app.log
  exclude_lines: ['^INFO']
  include_lines: ['^ERROR', '^WARN']

⚠ 注意:

  • exclude_lines / include_lines 只支持 正则表达式
  • 对多行日志不友好

三、使用 Logstash 做复杂过滤(Filebeat → Logstash)

如果过滤逻辑复杂(JSON 解析、条件判断、字段拆分),建议:

Filebeat → Logstash → ES

Logstash 示例

filter {
  if ([message] =~ /heartbeat/) {
    drop {}
  }

  if [message] =~ /ERROR/ {
    mutate {
      add_field => { "level" => "error" }
    }
  }
}

四、调试与验证

测试配置是否正确

sudo filebeat test config

查看 Filebeat 输出(不发送到 ES)

sudo filebeat -e -c filebeat.yml -d "*"

五、常见场景推荐方案

场景 推荐方式
丢弃 debug / heartbeat processors drop_event
只采集 ERROR include_lines
复杂日志解析 Logstash
性能优先 Filebeat processors

如果你能给我:

  • 日志示例
  • 想“保留”还是“丢弃”
  • 是否使用 Elasticsearch / Logstash

我可以直接帮你写一份 可用的 filebeat.yml。

0 踩