温馨提示×

centos filebeat怎样过滤日志

小樊
45
2026-09-02 21:44:15
栏目: 智能运维

在 CentOS 上使用 Filebeat 过滤日志,常见做法有 3 种,按使用频率和复杂度从简单到高级说明:


一、使用 include_lines / exclude_lines(最基础)

适合:按行内容简单过滤

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

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

示例:排除 DEBUG 日志

  exclude_lines: ['DEBUG']

✅ 优点:简单
❌ 缺点:只能按行匹配,不能做复杂逻辑


二、使用 processors(推荐,官方主推)

适合:字段过滤、丢弃日志、添加字段

1️⃣ 丢弃包含某些关键字的日志

processors:
  - drop_event.when.contains:
      message: "health check"

2️⃣ 按日志级别过滤

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

3️⃣ 只保留 ERROR / WARN

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

4️⃣ 删除不需要的字段(减少传输量)

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

✅ 优点:灵活、性能好
✅ 推荐在 CentOS 生产环境使用


三、使用 fields / tags + Logstash(复杂场景)

适合:需要复杂解析、多条件过滤

Filebeat 中打标签

filebeat.inputs:
- type: log
  paths:
    - /var/log/nginx/access.log
  tags: ["nginx"]

Logstash 中过滤

filter {
  if "nginx" in [tags] {
    grok {
      match => { "message" => "%{COMBINEDAPACHELOG}" }
    }
  }
}

四、CentOS 上常见操作命令

修改配置

vi /etc/filebeat/filebeat.yml

检查配置

filebeat test config

重启 Filebeat

systemctl restart filebeat
systemctl status filebeat

五、常见过滤场景速查

需求 方式
不要 DEBUG exclude_lines
只要 ERROR include_lines
丢弃某类日志 drop_event
修改字段 add_fields
多条件过滤 when.or / when.and

如果你能告诉我:

  • 日志路径
  • 日志格式(示例)
  • 想“保留”还是“丢弃”什么

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

0