目录

前言

解决方案

示例需求二:过滤日志中某个key不等于特定值的日志

示例需求二:过滤日志中包含特定字符串的日志

示例需求三:通过正则过滤日志

举三反更多

参考文档


前言

项目应用的日志采集通常会有以下需求:

  • 日志采集到 es 之前对日志通过 level 进行过滤
  • 过滤掉不要的日志再采集到es
  • 符合不同规则的日志写入es不同的索引(多个索引)

其实我们完全没必要在茫茫google、baidu中寻找答案,这些需求在官方文档中都能找到解决方案,这里我们如何过滤掉不要的日志再采集到es的配置进行讲解。

解决方案

        通过官方文档,可以知道output.elasticsearch中可以使用condition,而condition中支持equals、contains、regexp、range、or、and、not这7种逻辑,常见的常见都可以覆盖到,如果没有覆盖到可以考虑再加入Logstash进行更复杂的过滤,这个我们就是condition来实现过滤掉不要的日志再采集到es中。

示例需求二:过滤日志中某个key不等于特定值的日志

filebeat.prospectors:
- input_type: log
  paths:
    - /var/log/*
  json:
    keys_under_root: true

processors:
- drop_fields:
    fields: ["input_type", "offset", "beat", "type"]

output.elasticsearch:
  hosts: ['host:port']
  username: user
  password: pwd
  indices:
    - index: "filebeat-log-%{+yyyyMMdd}"
      when.not:
        equals:
          protocol: "tcp.http-status"

这里我们通过输出到es索引时添加when.not.equals,将JSON日志中protocol:"tcp.http-status"的日志不输出,从而达到过滤掉不要的日志再采集到es的效果。

示例需求二:过滤日志中包含特定字符串的日志

filebeat.prospectors:
- input_type: log
  paths:
    - /var/log/*
  json:
    keys_under_root: true

processors:
- drop_fields:
    fields: ["input_type", "offset", "beat", "type"]

output.elasticsearch:
  hosts: ['host:port']
  username: user
  password: pwd
  indices:
    - index: "filebeat-log-%{+yyyyMMdd}"
      when.not:
        contains:
          msg: "test"

 这里我们通过输出到es索引时添加when.not.contains,将JSON日志中msg包含:"test"的日志不输出,从而达到过滤掉不要的日志再采集到es的效果。

示例需求三:通过正则过滤日志

filebeat.prospectors:
- input_type: log
  paths:
    - /var/log/*
  json:
    keys_under_root: true

processors:
- drop_fields:
    fields: ["input_type", "offset", "beat", "type"]

output.elasticsearch:
  hosts: ['host:port']
  username: user
  password: pwd
  indices:
    - index: "filebeat-log-%{+yyyyMMdd}"
      when.regexp:
          msg: "foo.*"

这里我们通过输出到es索引时添加when.regexp,只输出JSON日志中msg匹配正则规则foo.*的日志,从而达到过滤掉不要的日志再采集到es的效果。 

举三反更多

这里就不再一一阐述了,请各位根据自己项目的实际日志采集需求翻阅官方文档查询需要的配置项和示例。

参考文档

Processors | Filebeat Reference [5.6] | Elastic

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐