在 Debian 上配置 Filebeat 多行日志采集(multiline),核心是在 filebeat.yml 中针对日志路径配置 multiline 规则。下面按常见场景说明。
sudo apt update
sudo apt install filebeat -y
配置文件位置:
/etc/filebeat/filebeat.yml
日志如下:
2024-01-01 10:00:00 ERROR main Exception
at com.xxx.A(B.java:10)
at com.xxx.C(D.java:20)
filebeat.inputs:
- type: filestream
id: java-log
paths:
- /var/log/app/*.log
multiline:
type: pattern
pattern: '^\d{4}-\d{2}-\d{2}'
negate: true
match: after
pattern:匹配新日志开始的行negate: true:不匹配 pattern 的行match: after:追加到上一行后面结果:
非时间开头的行(堆栈)会合并到上一行
multiline:
type: pattern
pattern: '^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}'
negate: true
match: after
[ 开头(如 JSON / 框架日志)multiline:
type: pattern
pattern: '^\['
negate: true
match: after
multiline:
type: pattern
pattern: '^(ERROR|WARN|INFO)'
negate: true
match: after
{ 开头的 JSON 日志multiline:
type: pattern
pattern: '^\{'
negate: true
match: after
filebeat.inputs:
- type: log
paths:
- /var/log/app/*.log
multiline:
pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'
negate: true
match: after
⚠️ Filebeat 7.16+ 推荐
filestream
filebeat test config
sudo systemctl restart filebeat
sudo systemctl status filebeat
sudo filebeat -e -c /etc/filebeat/filebeat.yml
观察日志是否被合并。
✅ 正则必须匹配第一行
✅ negate + match: after 最常用
❌ 正则写错会导致日志全合并或全拆分
❌ filestream 和 log 不能混用同一 path
如果你能提供 具体日志样例(如 Java / Python / Docker / Nginx),我可以帮你写精确的多行正则。