前言
用来记录filebeat使用过程中用到的一些实用的配置
filebeat版本:8.1.1
一、多行合并
对于java项目来说,正常的日志打印格式一般都是很规整的,比如下面这样:
2023-12-11 10:27:58,228 DEBUG - b04da818-ace2-40ce-b13e-5c3b07de07d7 xxxxxx.
2023-12-11 10:28:28,193 DEBUG - b04da818-ace2-40ce-b13e-5c3b07de07d7 xxxx
2023-12-11 10:28:28,198 DEBUG - b04da818-ace2-40ce-b13e-5c3b07de07d7 xxxxx
但是对于异常信息,打印出来的堆栈信息会因为和正常的日志格式不匹配导致不能被我们很好的检索出来,例如:
2023-12-11 08:59:28,682 ERROR - 9090dfeb-267a-4e08-b87b-07695e47cc74 xxxx -> Order not found.
com.xxx.exceptions.ApplicationException: Order not found.
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]
at org.jboss.weld.interceptor.proxy.TerminalAroundInvokeInvocationContext.proceedInternal(TerminalAroundInvokeInvocationContext.java:51) ~[weld-core-impl-3.1.9.Final.jar:3.1.9.Final]
at org.jboss.weld.interceptor.proxy.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:78) ~[weld-core-impl-3.1.9.Final.jar:3.1.9.Final]
我们可以观察到,因为堆栈信息出现了换行,且没有日期格式,后期如果我们单纯通过线程号去查日志的话,就会丢失这部分堆栈日志。所以设置多行合并是十分有必要的。那么话不多说,下面讲一下怎么配置:
filebeat.inputs:
- type: filestream
id: xxx-service
paths:
- /home/xxx.log
# 配置多行读取日志,避免异常日志无法被正常观察
parsers:
- multiline:
type: pattern
pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'
negate: true
match: after
核心的多行配置一共有4个:
-
multiline.type
:表示匹配模式,pattern表示正则 -
multiline.pattern
:正则规则,这里就是匹配日志开头是2020-10-10
这种日期格式的日志 -
multiline.negate
:是否反向选择,为true的时候说明匹配到的文本是非日期格式开头的 -
multiline.match
:处理方式,after表示把这些未匹配成功的文本拼在上一条日志后面
参考文档:https://www.elastic.co/guide/en/beats/filebeat/current/multiline-examples.html
二、指定需要的field字段
默认情况下,filebeat传输的字段很多,我们可以通过``来指定我们希望保留的字段
processors:
- include_fields:
when:
condition
fields: ["field1", "field2", ...]
其中的when
和condition
是用来做条件判断的,一般只有当日志的格式比较复杂,我们需要分情况收集的时候才会使用。如果是全量收集日志的话,可以把这两行直接注释掉。
更多可配置项可以参考官方文档:https://www.elastic.co/guide/en/beats/filebeat/current/include-fields.html
三、指定新的索引名称
如果我们不配置自定义索引的话,filebeat则默认会生成名称为filebeat
的索引,但实际上我们一般都是会有自定义索引的需求的,而且索引的值会按照每天的日期进行分开存储。
那么怎么自定义索引名称的?需要加以下配置
output.elasticsearch:
# Array of hosts to connect to.
hosts: ["xx.xx.xx.xx:9200"]
...
index: "custom-index-%{+yyyy.MM.dd}"
setup.template.name: "xxx-core"
setup.template.pattern: "v-core-*"
setup.ilm.enabled: false
-
setup.ilm.enabled
#索引生命周期ilm功能默认开启,开启后索引名称只能为filebeat-* 所以需要关闭 -
setup.template.name
#定义模板名称 -
setup.template.pattern
#定义模板关联的索引名称
网友评论