温馨提示×

debian filebeat如何多行采集

小樊
35
2026-10-18 03:57:54
栏目: 智能运维

在 Debian 上配置 Filebeat 多行日志采集(multiline),核心是在 filebeat.yml 中针对日志路径配置 multiline 规则。下面按常见场景说明。


一、安装 Filebeat(Debian)

sudo apt update
sudo apt install filebeat -y

配置文件位置:

/etc/filebeat/filebeat.yml

二、基本多行配置示例(Java 堆栈日志)

场景

日志如下:

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:追加到上一行后面

结果:

非时间开头的行(堆栈)会合并到上一行


三、常见多行模式

1️⃣ 以时间开头(最常用)

multiline:
  type: pattern
  pattern: '^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}'
  negate: true
  match: after

2️⃣ 以 [ 开头(如 JSON / 框架日志)

multiline:
  type: pattern
  pattern: '^\['
  negate: true
  match: after

3️⃣ 以 ERROR / WARN 开头

multiline:
  type: pattern
  pattern: '^(ERROR|WARN|INFO)'
  negate: true
  match: after

4️⃣ 以 { 开头的 JSON 日志

multiline:
  type: pattern
  pattern: '^\{'
  negate: true
  match: after

四、旧版写法(log 类型,仍可用)

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

六、重启 Filebeat

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),我可以帮你写精确的多行正则。

0 踩