温馨提示×

CentOS下Filebeat如何过滤日志

小樊
61
2026-08-03 21:42:41
栏目: 智能运维

在 CentOS 下使用 Filebeat 过滤日志,通常是通过 Filebeat 配置(filebeat.yml) 中的 processors 或 include_lines / exclude_lines 来实现的。下面按常见场景给你一个完整说明。


一、Filebeat 过滤日志的几种方式

✅ 方式一:使用 include_lines / exclude_lines(最常用)

适合:按关键字过滤整行日志

示例:只采集包含 ERROR 的日志

filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/app/*.log
  include_lines: ['ERROR']

示例:排除包含 DEBUG 的日志

filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/app/*.log
  exclude_lines: ['DEBUG']

✅ 支持 正则表达式

exclude_lines: ['^INFO', '^DEBUG']

✅ 方式二:使用 processors(更强大,推荐)

适合:

  • 删除字段
  • 按条件丢弃事件
  • 修改字段
  • 解析 JSON / 时间戳

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

示例:丢弃包含 health check 的日志

processors:
  - drop_event:
      when:
        regexp:
          message: "health check"

2️⃣ 只保留 ERROR 日志

processors:
  - drop_event:
      when:
        not:
          regexp:
            message: "ERROR"

3️⃣ 删除指定字段(减少传输量)

processors:
  - drop_fields:
      fields: ["ecs", "agent", "host"]
      ignore_missing: true

✅ 方式三:多条件过滤(高级)

processors:
  - drop_event:
      when:
        and:
          - regexp:
              message: "ERROR"
          - contains:
              message: "timeout"

二、过滤 JSON 日志(常见)

如果日志是 JSON 格式:

filebeat.inputs:
- type: log
  paths:
    - /var/log/app/*.json
  json.keys_under_root: true
  json.add_error_key: true

然后按字段过滤:

processors:
  - drop_event:
      when:
        equals:
          level: "DEBUG"

三、完整示例(CentOS 常用)

filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/access.log
  exclude_lines: ['200']

processors:
  - drop_event:
      when:
        regexp:
          message: "health check"

output.elasticsearch:
  hosts: ["localhost:9200"]

四、重启 Filebeat(CentOS 7/8)

systemctl restart filebeat
systemctl status filebeat

五、调试建议(非常重要)

✅ 先用 stdout 测试,避免误删数据:

output.console:
  pretty: true

确认过滤正确后再切回 Elasticsearch / Logstash。


六、总结

需求 推荐方式
简单行过滤 include_lines / exclude_lines
复杂条件 processors + drop_event
JSON 日志 json 解析 + processors
减少数据量 drop_fields

如果你愿意,可以把 你的日志样例 + 期望过滤规则 发出来,我可以直接帮你写一份可用的 filebeat.yml ✅

0 踩