在 CentOS 上使用 Filebeat 采集多行日志(multiline),核心是在 filebeat.yml 中配置 multiline 相关参数。下面给你几种常见场景和完整示例。
编辑 Filebeat 配置文件(通常路径):
/etc/filebeat/filebeat.yml
在 filebeat.inputs 节点下配置,例如:
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/app/*.log
multiline:
pattern: '^\['
negate: true
match: after
| 参数 | 说明 |
|---|---|
pattern |
正则,用于匹配「新一行日志的开始」 |
negate |
true 表示不匹配 pattern 的行属于上一行 |
match |
after 或 before,把多行合并到前/后 |
max_lines |
最大合并行数(默认 500) |
timeout |
超时时间(如 5s),防止一直等待 |
[ 开头)multiline:
pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'
negate: true
match: after
或:
multiline:
pattern: '^\['
negate: true
match: after
✅ 效果:
不是以日期或 [ 开头的行,都合并到上一行(如异常堆栈)
multiline:
pattern: '^\d{2}:\d{2}:\d{2}'
negate: true
match: after
multiline:
pattern: '^\{'
negate: false
match: after
(一般不推荐 JSON 用 multiline,建议程序直接输出单行 JSON)
multiline:
pattern: '^\['
negate: true
match: after
max_lines: 1000
timeout: 5s
filebeat.inputs:
- type: log
enabled: true
paths:
- /opt/app/logs/app.log
multiline:
pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'
negate: true
match: after
timeout: 5s
output.elasticsearch:
hosts: ["http://localhost:9200"]
filebeat test config
systemctl restart filebeat
systemctl status filebeat
filebeat -e -c /etc/filebeat/filebeat.yml
可以实时看到日志是否被正确合并。
如果你愿意,可以:
我可以直接帮你写 最合适的一版配置。