1、Filebeat 基本介绍
1.1 Filebeat 是什么
Filebeat 是用于 “转发” 和 “集中日志数据” 的 “轻量型数据采集器”;
Filebeat 会监视指定的日志文件路径,收集日志事件并将数据转发到 Elasticsearch、Logstash、Redis、Kafka 存储服务器。
1.2 Filebeat 主要组件
Filebeat 主要由两个核心组件组成:Input 和 Harvester。
- Input(输入):负责定位和识别需要读取的日志文件。可以使用文件的全路径、也可以是文件名称匹配等。
- Harvester(收割机):负责打开 Input 定义的这些文件,然后 逐行 读取文件内容。最后将内容发送到指定的输出端。
这两个组件协同工作,确保将日志文件尾部 最新的数据 发送到指定的输出端。
配置示例:
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/oldwang.log # 指定日志路径
- /var/log/*.log # 指定日志, 匹配方式
# - /var/log/**/*.log # 递归目录, 完成日志的匹配
1.3 Filebeat 工作流程
当 filebeat 启动后,filebeat 通过 Input 读取指定的日志路径,然后为该日志启动一个收割进程 harvester,每一个收割进程读取一个日志文件的新内容,并发送这些新的日志数据到处理程序 spooler,处理程序会集合这些事件,最后 filebeat 会发送集合的数据到你指定的位置。
2、Filebeat 安装配置
2.1 Filebeat 安装
- 安装 Filebeat
[root@web01 ~]$ dpkg -i filebeat-8.18.2-amd64.deb
[root@web01 ~]# rpm -ivh filebeat-8.18.2-x86_64.rpm
- 启动 Filebeat
[root@web01 ~]# systemctl enable --now filebeat.service
2.2 Filebeat 配置说明
# 定义 Filebeat 输入源
filebeat.inputs:
- type: log # 输入类型为日志文件
enabled: true # 启用该输入
paths:
- /var/log/nginx/access.log # 指定日志路径
tags: ["nginx-access"] # 为该日志添加标签, 可用于后续过滤或分析
- type: log
enabled: true
paths:
- /var/log/nginx/error.log # 指定日志路径
tags: ["nginx-error"] # 为该日志添加标签
# 定义输出,将日志发送到 Elasticsearch
output.elasticsearch:
hosts: ["elasticsearch:9200"] # 指定 Elasticsearch 地址
3、Filebeat 常用 Input 插件
3.1 Filebeat 从终端读取数据
- 配置 Filebeat 从终端读取数据,然后从终端输出消息
[root@web01 ~]# mkdir /etc/filebeat/conf.d
[root@web01 ~]$ vim /etc/filebeat/conf.d/01-stdin-output-console.yml
配置文件内容:
filebeat.inputs:
- type: stdin
enable: true
output.console:
pretty: true
enable: true
- 启动 Filebeat,并明确指定配置文件路径,然后输入一条测试内容
注意:基于如下方式启用 filebeat 的同时,需要将 filebeat 服务关闭。否则会报错:
Exiting: /var/lib/filebeat/filebeat.lock: data path already locked by another beat. Please make sure that multiple beats are not sharing the same data path (path.data)
[root@web01 ~]# systemctl stop filebeat.service
[root@web01 ~]$ filebeat -e -c /etc/filebeat/conf.d/01-stdin-output-console.yml
# 输入内容
hello,filebeat
- 结果返回
{
"@timestamp": "2025-07-26T04:37:46.332Z",
"@metadata": {
"beat": "filebeat",
"type": "_doc",
"version": "8.18.2"
},
"log": {
"offset": 0, <== Filebeat 当前从日志文件中读取到的位置
"file": {
"path": "" <== 日志来源文件路径 (空值, 因为此日志来自 stdin 输入而非文件)
}
},
"message": "hello,filebeat", <== 核心日志内容
"input": {
"type": "stdin" <== 表示输入来源为标准输入
},
"ecs": {
"version": "8.0.0"
},
"host": {
"name": "es-node4.wang.org" <== 产生日志的主机名信息
},
"agent": {
"id": "db23f4d1-8f1e-48ac-a632-b7411303f161",
"name": "es-node4.wang.org",
"type": "filebeat",
"version": "8.18.2",
"ephemeral_id": "ba6b6172-3cdf-4060-b43f-a018f79e5c42"
}
}
说明:
offset: 0表示 Filebeat 刚开始读取该文件,从第一个字节(起始位置)开始读取。如果offset: 1500,表示 Filebeat 已经读取了前 1500 字节,它会从第 1501 字节继续读取。
3.2 Filebeat 从网络中读取数据
在分布式系统或多服务架构中,直接从本地系统采集日志有时不太方便。为此,可以将应用程序日志通过 TCP 或 UDP 协议发送到特定网络端口,然后通过 Filebeat 接收此端口的日志数据。
3.2.1 监听 TCP 端口并输出到控制台
- 编写配置
[root@web01 ~]$ vim /etc/filebeat/conf.d/02-tcp-output-console.yml
配置文件内容:
filebeat.inputs:
- type: tcp # 指定类型为 TCP, 也可以是 UDP
enable: true # 启用该输入配置
host: "0.0.0.0:5656" # 监听网络地址及端口
max_message_size: "10M" # 限制单条消息最大为 10M. 默认值为 20MiB.
output.console:
pretty: true # 以格式化的美观方式输出日志
enable: true
- 启动 Filebeat 并应用上述配置
[root@web01 ~]$ filebeat -e -c /etc/filebeat/conf.d/02-tcp-output-console.yml
- 验证 Filebeat 是否成功监听了 5656 端口
[root@web01 ~]$ netstat -nltp
tcp6 0 0 :::5656 :::* LISTEN 64290/filebeat
3.2.2 监听 UDP 端口并输出到控制台
- 编写配置
[root@web01 ~]$ vim /etc/filebeat/conf.d/02-udp-output-console.yml
配置文件内容:
filebeat.inputs:
- type: udp # 指定类型为 UDP
enable: true # 启用该输入配置
host: "0.0.0.0:5658" # 监听网络地址及端口
max_message_size: "10M" # 限制单条消息最大为 10M. 默认值为 20MiB.
output.console:
pretty: true # 以格式化的美观方式输出日志
enable: true
- 启动 Filebeat 并应用上述配置
[root@web01 ~]$ filebeat -e -c /etc/filebeat/conf.d/02-udp-output-console.yml
- 验证 Filebeat 是否成功监听了 5658 端口
[root@web01 ~]$ netstat -nltup
udp6 0 0 :::5658 :::* 86005/filebeat
3.2.3 模拟发送数据
[root@web01 ~]$ echo "hello,tcp" | nc 192.168.80.154 5656 # TCP 协议方式
[root@web01 ~]$ echo "hello,udp" | nc -u 192.168.80.154 5658 # UDP 协议方式
3.2.4 查看接收数据
- TCP 输入返回结果
{
"@timestamp": "2025-07-26T05:00:32.381Z",
"@metadata": {
"beat": "filebeat",
"type": "_doc",
"version": "8.18.2"
},
"input": {
"type": "tcp" <== 当前日志的输入类型为 TCP
},
"ecs": {
"version": "8.0.0"
},
"host": {
"name": "es-node4.wang.org" <== 运行 Filebeat 代理的主机名
},
"agent": {
"type": "filebeat",
"version": "8.18.2",
"ephemeral_id": "f3cc997d-dfc0-4716-a9ae-c1da98e63532",
"id": "db23f4d1-8f1e-48ac-a632-b7411303f161",
"name": "es-node4.wang.org"
},
"message": "hello,tcp", <== 实际接收到的日志内容, 即通过 TCP 发送到 5656 端口的原始数据
"log": {
"source": {
"address": "192.168.80.151:44322" <== 发送日志的客户端 IP 地址
}
}
}
- UDP 输入返回结果
{
"@timestamp": "2025-07-26T05:08:43.767Z",
"@metadata": {
"beat": "filebeat",
"type": "_doc",
"version": "8.18.2",
"truncated": false
},
"message": "hello,udp\n", <== 实际接收到的日志内容, 即通过 UDP 发送到 5658 端口的原始数据
"log": {
"source": {
"address": "192.168.80.151:50754" <== 发送日志的客户端 IP 地址
}
},
"input": {
"type": "udp" <== 当前日志的输入类型为 UDP
},
"host": {
"name": "es-node4.wang.org" <== 运行 Filebeat 代理的主机名
},
"agent": {
"name": "es-node4.wang.org",
"type": "filebeat",
"version": "8.18.2",
"ephemeral_id": "af3b7435-9fc5-4c9b-882a-6a0e34ba1847",
"id": "db23f4d1-8f1e-48ac-a632-b7411303f161"
},
"ecs": {
"version": "8.0.0"
}
}
3.3 Filebeat 从文件中读取数据
注意:Filebeat 7.16.0 版本之后 Log 被废弃,官方建议使用 filestream 插件。
- 配置 Filebeat 从文件中读取数据
[root@web01 ~]# vim /etc/filebeat/conf.d/03-file-output-console.yml
配置文件内容:
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/test.log # 指定需要监控的日志文件
# - /var/log/*.log # 监控 /var/log 目录下所有 .log 结尾的文件
# - /var/log/**/*.log # 递归监控 /var/log 及其子目录下所有 .log 结尾的文件
output.console:
pretty: true
enable: true
- 启动 Filebeat
# 启动 Filebeat
[root@web01 ~]# filebeat -e -c /etc/filebeat/conf.d/03-file-output-console.yml
# 向 /var/log/test.log 文件追加内容
[root@web01 ~] echo "hello,filebeat01" >> /var/log/test.log
[root@web01 ~] echo "hello,filebeat02" >> /var/log/test.log
- 查看 Filebeat 接收到的数据
- 第一次输入返回结果
{
"@timestamp": "2025-07-26T16:21:41.320Z",
"@metadata": {
"beat": "filebeat",
"type": "_doc",
"version": "8.18.2"
},
"log": {
"offset": 0, <== 起始位置
"file": {
"path": "/var/log/test.log"
}
},
"message": "hello,filebeat01", <== 输入内容
"input": {
"type": "log"
},
"ecs": {
"version": "8.0.0"
},
"host": {
"name": "es-node4.wang.org"
},
"agent": {
"id": "db23f4d1-8f1e-48ac-a632-b7411303f161",
"name": "es-node4.wang.org",
"type": "filebeat",
"version": "8.18.2",
"ephemeral_id": "e793a9b2-afaa-41c5-8075-c1899f770c14"
}
}
- 第二次输入返回结果
{
"@timestamp": "2025-07-26T16:22:56.332Z",
"@metadata": {
"beat": "filebeat",
"type": "_doc",
"version": "8.18.2"
},
"log": {
"offset": 17, <== 断点续读
"file": {
"path": "/var/log/test.log"
}
},
"message": "hello,filebeat02", <== 输入内容
"input": {
"type": "log"
},
"ecs": {
"version": "8.0.0"
},
"host": {
"name": "es-node4.wang.org"
},
"agent": {
"id": "db23f4d1-8f1e-48ac-a632-b7411303f161",
"name": "es-node4.wang.org",
"type": "filebeat",
"version": "8.18.2",
"ephemeral_id": "e793a9b2-afaa-41c5-8075-c1899f770c14"
}
}
4、Filebeat 常用 Output
Filebeat 常用的 Output 参考地址:https://www.elastic.co/guide/en/beats/filebeat/current/configuring-output.html
4.1 Filebeat 输出至 ES 集群
- 配置 Filebeat 将日志采集,并输出到 Elasticsearch 集群
4.1.1 编写配置文件
[root@web01 ~]# vim /etc/filebeat/filebeat.yml
配置文件内容:
filebeat.inputs:
- type: log # 输入类型为日志文件
enabled: true # 启用该输入配置
paths: /var/log/test.log # 监控 /var/log/test.log 文件的日志
# 输出目标为 Elasticsearch 集群
output.elasticsearch:
hosts: ["192.168.80.151:9200","192.168.80.152:9200","192.168.80.153:9200"]
4.1.2 重启 Filebeat 服务
[root@web01 ~]# systemctl restart filebeat
4.1.3 向监控的日志文件追加新内容
[root@web01 ~]# echo "hello,filebeat03" >> /var/log/test.log
通过 Cerebro 观察新写入到 ES 的索引数据
索引默认格式说明
默认没有定义索引名称的情况下,索引默认格式为:.ds-{name}-{version}-{date}-{date}-{counter}
例如:.ds-filebeat-8.18.2-2099.05.01-000001
- name:名称为 filebeat
- version:Filebeat 版本号
- date:索引创建日期,格式为 年.月.日
- counter:自增的 6 位数计数器
4.2 Filebeat 自定义索引名称
默认 Filebeat 写入 ES 的索引名称为 .ds-filebeat-*,如果希望修改索引名称,则通过 index 关键字实现。
- 修改 Filebeat 配置文件
[root@web01 ~]# vim /etc/filebeat/filebeat.yml
配置文件内容:
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/test.log
output.elasticsearch:
hosts: ["192.168.80.151:9200","192.168.80.152:9200","192.168.80.153:9200"]
index: "test-log-%{[agent.version]}-%{+yyyy.MM.dd}" # 自定义索引名称
setup.ilm.enabled: false # 索引生命周期 ilm 功能默认开启, 开启情况下索引名称只能为 .ds-filebeat-*
setup.template.name: "test-log" # 定义模板名称
setup.template.pattern: "test-log-*" # 定义模板的 "匹配索引名称规则"
# 8.x 必须配置如下内容, 否则不会创建索引, 而会创建对应的索引数据流
setup.template.enabled: false # 关掉默认的模板配置
setup.template.overwrite: true # 允许覆盖已存在的同名模板
- 重启 Filebeat 产生新的索引
[root@web01 ~]# systemctl start filebeat
[root@web01 ~]# systemctl status filebeat
- 向监控的日志文件追加新内容
[root@web01 ~]# echo "hello,filebeat04" >> /var/log/test.log
[root@web01 ~]# echo "hello,filebeat05" >> /var/log/test.log
- 检查结果
- 通过 Cerebro 检查结果
- 通过 Kibana 检查结果,索引为
test-log-8.18.2--2025.07.27,索引模板为test-log
4.3 Filebeat 自定义索引分片
默认情况下 Filebeat 写入到 ES 的索引分片为 1,如果需要修改分片,可以通过以下两种方式:
方式 1:通过 Filebeat 配置文件
- 修改 filebeat 配置文件
[root@web01 ~]# vim /etc/filebeat/filebeat.yml
配置文件内容:
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/test.log
output.elasticsearch:
hosts: ["192.168.80.151:9200","192.168.80.152:9200","192.168.80.153:9200"]
index: "test-log-%{[agent.version]}-%{+yyyy.MM.dd}" # 自定义索引名称
setup.ilm.enabled: false # 索引生命周期 ilm 功能默认开启, 开启情况下索引名称只能为 .ds-filebeat-*
setup.template.name: "test-log" # 定义模板名称
setup.template.pattern: "test-log-*" # 定义模板的 "匹配索引名称规则"
# 8.x 必须配置如下内容, 否则不会创建索引, 而会创建对应的索引数据流
setup.template.enabled: false # 关掉默认的模板配置
setup.template.overwrite: true # 允许覆盖已存在的同名模板
setup.template.settings:
index.number_of_shards: 3 # 每个索引的主分片数量为 3
index.number_of_replicas: 1 # 每个主分片的副本分片数量为 1
- 删除索引、删除索引模板,最后重启 filebeat
systemctl restart filebeat
- 产生新的数据及索引
[root@web01 ~]# echo "hello,filebeat04" >> /var/log/test.log
[root@web01 ~]# echo "hello,filebeat05" >> /var/log/test.log
- 验证索引状态
方式 2:手动创建索引模板
- 修改 filebeat 配置文件
[root@web01 ~]# vim /etc/filebeat/filebeat.yml
配置文件内容:
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/test.log
output.elasticsearch:
hosts: ["192.168.80.151:9200","192.168.80.152:9200","192.168.80.153:9200"]
index: "test-log-%{[agent.version]}-%{+yyyy.MM.dd}" # 自定义索引名称
setup.ilm.enabled: false # 索引生命周期 ilm 功能默认开启, 开启情况下索引名称只能为 .ds-filebeat-*
setup.template.name: "test-log" # 定义模板名称
setup.template.pattern: "test-log-*" # 定义模板的 "匹配索引名称规则"
# 8.x 必须配置如下内容, 否则不会创建索引, 而会创建对应的索引数据流
setup.template.enabled: false # 关掉默认的模板配置
setup.template.overwrite: true # 允许覆盖已存在的同名模板
- 手动创建索引的模板,然后设定主分片数据和副本分片数量
PUT /_index_template/test-log // 索引模板名称
{
"index_patterns": ["test-log-*"], // 模板匹配并关联哪些索引;
"template": {
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 1
}
}
}
}
验证索引模板
删除此前的索引,然后重启服务,验证索引状态 (三分片, 一副本)
5、Filebeat 字段添加
5.1 使用 tags 添加标签
在 Filebeat 中,可以使用 tags 配置项为每条数据添加一个标签列表。例如:可以为访问日志添加 access_log 标签,为错误日志添加 error_log 标签。这些标签在后续的日志处理和分析阶段可以用于有效地标识和筛选数据。
- 配置文件编写
[root@web01 ~]# vim /etc/filebeat/conf.d/04-log-output-console.yml
配置文件内容:
filebeat.inputs:
- type: log
enabled: true
paths: /var/log/access.log
tags: ["system-access"] # 为输入日志添加标签
- type: log
enabled: true
paths: /var/log/error.log
tags: ["system-error"] # 为输入日志添加标签
output.console:
pretty: true
enabled: true
- 启动对应的 filebeat
[root@web01 ~]# filebeat -e -c /etc/filebeat/conf.d/04-log-output-console.yml
- 追加日志内容
[root@web01 ~]# echo test-access >> /var/log/access.log
[root@web01 ~]# echo test-error >> /var/log/error.log
- 验证对应的结果
- tags: system-access 结果
{
"@timestamp": "2025-07-27T07:22:52.136Z",
"@metadata": {
"beat": "filebeat",
"type": "_doc",
"version": "8.18.2"
},
"log": {
"file": {
"path": "/var/log/access.log" <== 日志路径
},
"offset": 0
},
"message": "test-access", <== 日志内容
"tags": [
"system-access" <== 日志标签
],
"input": {
"type": "log" <== 输入类型
},
"ecs": {
"version": "8.0.0"
},
"host": {
"name": "es-node4.wang.org"
},
"agent": {
"version": "8.18.2",
"ephemeral_id": "b5cc1683-2c57-47dc-811d-b1627c94d9ad",
"id": "db23f4d1-8f1e-48ac-a632-b7411303f161",
"name": "es-node4.wang.org",
"type": "filebeat"
}
}
- tags: system-error 结果
{
"@timestamp": "2025-07-27T07:22:52.136Z",
"@metadata": {
"beat": "filebeat",
"type": "_doc",
"version": "8.18.2"
},
"log": {
"file": {
"path": "/var/log/error.log"
},
"offset": 0
},
"message": "test-error",
"tags": [
"system-error"
],
"input": {
"type": "log"
},
"ecs": {
"version": "8.0.0"
},
"host": {
"name": "es-node4.wang.org"
},
"agent": {
"version": "8.18.2",
"ephemeral_id": "b5cc1683-2c57-47dc-811d-b1627c94d9ad",
"id": "db23f4d1-8f1e-48ac-a632-b7411303f161",
"name": "es-node4.wang.org",
"type": "filebeat"
}
}
5.2 使用 fields 添加字段
在 Filebeat 中,可以使用 fields 配置项为每条数据添加自定义字段。
- 配置文件编写
[root@web01 ~]# vim /etc/filebeat/conf.d/05-log-output-console.yml
配置文件内容:
filebeat.inputs:
- type: log
enabled: true
paths: /var/log/access.log
fields:
namespace: system-access # 自定义字段
fields_under_root: true # 若设置为 true 时, 则将 fields 添加的自定义字段放在顶级字段中, 默认为 false.
output.console:
pretty: true
console: true
- 启动对应的 filebeat
[root@web01 ~]# filebeat -e -c /etc/filebeat/conf.d/05-log-output-console.yml
[root@web01 ~]# echo test-access >> /var/log/access.log
- 查看 Filebeat 输出的结果
{
"@timestamp": "2025-07-27T07:45:22.216Z",
"@metadata": {
"beat": "filebeat",
"type": "_doc",
"version": "8.18.2"
},
"namespace": "system-access", <== 自定义字段内容
"ecs": {
"version": "8.0.0"
},
"host": {
"name": "es-node4.wang.org"
},
"agent": {
"version": "8.18.2",
"ephemeral_id": "4d716c9b-06eb-4a30-a511-e2d0ddf32f60",
"id": "db23f4d1-8f1e-48ac-a632-b7411303f161",
"name": "es-node4.wang.org",
"type": "filebeat"
},
"log": {
"offset": 24,
"file": {
"path": "/var/log/access.log"
}
},
"message": "test-access",
"input": {
"type": "log"
}
}
6、Filebeat 处理器
6.1 处理器 Processors
在 Filebeat 中,Processors 用于在数据发送到目标后端之前,可以对源数据执行预处理的组件。这些处理器可以执行各种数据转换任务,如修改、添加或删除字段等。常见的处理器包括:
- add_fields: 向数据中添加新的字段。
- drop_fields: 删除数据中的指定字段。
- rename: 重命名数据中的字段。
参考文档:https://www.elastic.co/guide/en/beats/filebeat/8.18/filtering-and-enhancing-data.html
6.2 删除消息中的内容
- 配置文件编写(过滤掉 DEBUG 开头的日志)
[root@web01 ~]# vim /etc/filebeat/conf.d/06-log-processors-output-console.yml
配置文件内容:
filebeat.inputs:
- type: log
enabled: true
paths: /var/log/test.log # 目标日志文件路径
processors:
- drop_event: # 丢弃符合条件的日志事件
when:
regexp:
message: "^DEBUG" # 匹配规则: message 字段以 DEBUG 开头
output.console:
pretty: true
console: true
排除 log.level: info 的日志配置
processors: - drop_event: when: equals: log.level: "info"
- 启动 filebeat 并写入测试内容
[root@web01 ~]# filebeat -e -c /etc/filebeat/conf.d/06-log-processors-output-console.yml
# 向 test.log 写入 4 条内容
echo "DEBUG: Hello" >> /var/log/test.log
echo "DEBUG: Hello" >> /var/log/test.log
echo "ADEBUG: Hello" >> /var/log/test.log
echo "BDEBUG: Hello" >> /var/log/test.log
- 输出结果
仅显示 ADEBUG 和 BDEBUG 开头的日志,DEBUG 开头的日志被成功过滤。
{
"@timestamp": "2025-07-27T13:13:43.324Z",
"@metadata": {
"beat": "filebeat",
"type": "_doc",
"version": "8.18.2"
},
"ecs": {
"version": "8.0.0"
},
"host": {
"name": "es-node4.wang.org"
},
"log": {
"offset": 14,
"file": {
"path": "/var/log/test.log"
}
},
"message": "ADEBUG: Hello", <==
"input": {
"type": "log"
},
"agent": {
"version": "8.18.2",
"ephemeral_id": "6bd21962-ed78-431b-9aac-e866a0643dd4",
"id": "db23f4d1-8f1e-48ac-a632-b7411303f161",
"name": "es-node4.wang.org",
"type": "filebeat"
}
}
{
"@timestamp": "2025-07-27T13:19:38.415Z",
"@metadata": {
"beat": "filebeat",
"type": "_doc",
"version": "8.18.2"
},
"host": {
"name": "es-node4.wang.org"
},
"log": {
"offset": 54,
"file": {
"path": "/var/log/test.log"
}
},
"message": "BDEBUG: Hello", <==
"input": {
"type": "log"
},
"agent": {
"name": "es-node4.wang.org",
"type": "filebeat",
"version": "8.18.2",
"ephemeral_id": "6bd21962-ed78-431b-9aac-e866a0643dd4",
"id": "db23f4d1-8f1e-48ac-a632-b7411303f161"
},
"ecs": {
"version": "8.0.0"
}
}
6.3 删除无用字段
- 配置文件编写
[root@web01 ~]# vim /etc/filebeat/conf.d/07-log-processors-output-console.yml
配置文件内容:
filebeat.inputs:
- type: log
enabled: true
paths: /var/log/test.log # 目标日志文件路径
processors:
- drop_fields: # 移除指定字段的处理器
fields: ["ecs","host","input"] # 需要删除的字段列表
output.console:
pretty: true
console: true
- 启动 filebeat 并写入测试内容
[root@web01 ~]# filebeat -e -c /etc/filebeat/conf.d/07-log-processors-output-console.yml
# 向 test.log 写入内容
echo "Hello filebeat" >> /var/log/test.log
- 检查对应的结果
会发现 ecs、host、input 等字段都被删除了。
{
"@timestamp": "2025-07-27T13:25:48.805Z",
"@metadata": {
"beat": "filebeat",
"type": "_doc",
"version": "8.18.2"
},
"log": {
"offset": 68,
"file": {
"path": "/var/log/test.log"
}
},
"message": "Hello filebeat",
"agent": {
"version": "8.18.2",
"ephemeral_id": "d73fa509-7ce0-4832-94cb-9f572ca69ada",
"id": "db23f4d1-8f1e-48ac-a632-b7411303f161",
"name": "es-node4.wang.org",
"type": "filebeat"
}
}
6.4 重写源数据信息
- 配置文件编写
[root@web01 ~]# vim /etc/filebeat/conf.d/08-log-processors-output-console.yml
配置文件内容:
filebeat.inputs:
- type: log
enabled: true
paths: /var/log/test.log # 目标日志文件路径
processors:
- rename: # 字段重命名处理器
fields: # 需要重命名的字段列表
- from: "agent.name" # 原字段名
to: "agent_name" # 新字段名 (顶级字段)
- from: "agent.type"
to: "agent_type"
- from: "log.file.path"
to: "log_file_path"
output.console:
pretty: true
console: true
- 启动 filebeat 并写入测试内容
[root@web01 ~]# filebeat -e -c /etc/filebeat/conf.d/08-log-processors-output-console.yml
# 向 test.log 写入内容
echo "Hello filebeat" >> /var/log/test.log
- 检查 filebeat 输出的结果
字段结构从嵌套变为平级,更易于后续处理。
{
"@timestamp": "2025-07-28T15:26:45.256Z",
"@metadata": {
"beat": "filebeat",
"type": "_doc",
"version": "8.18.2"
},
"agent": {
"version": "8.18.2",
"ephemeral_id": "dea0a33b-419f-4900-9e2c-0a2fbace6092",
"id": "db23f4d1-8f1e-48ac-a632-b7411303f161"
},
"ecs": {
"version": "8.0.0"
},
"log": {
"offset": 83,
"file": {}
},
"input": {
"type": "log"
},
"agent_name": "es-node4.wang.org", <==
"agent_type": "filebeat", <==
"log_file_path": "/var/log/test.log", <==
"message": "Hello filebeat",
"host": {
"name": "es-node4.wang.org"
}
}
