温馨提示×

centos filebeat怎样过滤

小樊
52
2026-09-15 01:35:53
栏目: 智能运维

在 CentOS 上使用 Filebeat 过滤日志,通常是通过 Filebeat 自身的 processors 或结合 Logstash / Elasticsearch 的过滤 来完成。下面按常见场景给你说明。


一、Filebeat 自身过滤(最常用)

Filebeat 支持在 filebeat.yml 中使用 processors 做过滤。

1️⃣ 丢弃某些日志(drop_event)

例如:过滤掉包含 DEBUG 的日志

filebeat.inputs:
- type: log
  paths:
    - /var/log/app/*.log

processors:
  - drop_event.when.contains:
      message: "DEBUG"

2️⃣ 只保留某些日志(keep)

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

3️⃣ 按字段过滤(如 level)

processors:
  - drop_event.when.not.equals:
      level: "error"

二、使用条件(when)组合过滤

processors:
  - drop_event.when:
      or:
        - contains.message: "test"
        - regexp.message: "^\\s*$"

三、多行日志过滤(如 Java 异常)

filebeat.inputs:
- type: log
  paths:
    - /var/log/app/app.log
  multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'
  multiline.negate: true
  multiline.match: after

四、只采集某些文件(inputs 层过滤)

filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/access.log
  exclude_files: ['.gz$']

五、字段处理(非完全过滤)

删除字段

processors:
  - drop_fields:
      fields: ["agent.version", "ecs.version"]

添加字段

processors:
  - add_fields:
      target: ''
      fields:
        env: production

六、Filebeat → Logstash 过滤(更强大)

如果过滤逻辑复杂,推荐在 Logstash 中做:

filter {
  if [message] =~ "DEBUG" {
    drop {}
  }
}

七、验证配置是否正确

filebeat test config
filebeat test output

重启 Filebeat:

systemctl restart filebeat

八、常见场景总结

需求 方法
丢弃 DEBUG 日志 drop_event
只采集 ERROR drop_event.when.not
忽略空行 regexp
多行日志 multiline
复杂过滤 Logstash

如果你能告诉我:

  • 日志格式(示例)
  • 想过滤什么(关键字 / 级别 / 文件)
  • 是否使用 ES / Logstash

我可以给你一份 可直接用的 filebeat.yml 配置。

0 踩