温馨提示×

ubuntu filebeat如何过滤日志

小樊
34
2026-10-19 18:12:48
栏目: 智能运维

在 Ubuntu 上使用 Filebeat 过滤日志,通常有以下几种常见方式,按使用频率和场景从简单到复杂说明:


一、使用 processors 在 Filebeat 端过滤(推荐)

Filebeat 本身支持 processors,可以在采集阶段就过滤或处理日志。

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

例如:忽略包含 DEBUG 的日志

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

2️⃣ 按字段或正则过滤

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

3️⃣ 只保留特定日志

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

二、使用 include_lines / exclude_lines

适合简单文本过滤。

只采集包含 ERROR 或 WARN 的行

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

排除某些行

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

✅ 优点:简单
❌ 缺点:不能做复杂逻辑


三、使用 exclude_files 过滤文件

filebeat.inputs:
- type: log
  paths:
    - /var/log/app/*
  exclude_files: ['.gz$', 'access.log']

四、使用 fields + 条件输出(按日志类型分流)

filebeat.inputs:
- type: log
  paths:
    - /var/log/nginx/access.log
  fields:
    log_type: nginx_access

然后在 output 中判断:

output.elasticsearch:
  hosts: ["localhost:9200"]
  indices:
    - index: "nginx-access-%{+yyyy.MM.dd}"
      when.equals:
        fields.log_type: nginx_access

五、复杂过滤:结合 Logstash(不推荐纯 Filebeat)

如果过滤逻辑非常复杂(JSON 解析、多条件、字段重组):

  • Filebeat 只负责采集
  • 使用 Logstash filter
filter {
  if [message] =~ "ERROR" {
    mutate { add_tag => ["error"] }
  }
}

六、常用调试命令(Ubuntu)

# 测试配置
sudo filebeat test config

# 查看生效配置
sudo filebeat export config

# 前台运行看日志
sudo filebeat -e -c /etc/filebeat/filebeat.yml

七、典型示例(综合)

filebeat.inputs:
- type: log
  paths:
    - /var/log/app/*.log
  exclude_lines: ['^DEBUG']
  processors:
    - drop_event.when.contains:
        message: "heartbeat"

如果你能告诉我:

  • 日志格式(plain / JSON)
  • 想过滤什么(关键字 / 级别 / 文件)
  • 输出到哪里(ES / Kafka / Logstash)

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

0 踩