自定义timestamp
在filebeat采集日志时,会需要将日志中的具体时间用于@timestamp字段,供后续的时间查找等等,在7.16版本中可以使用timestamp processor:https://www.elastic.co/guide/en/beats/filebeat/current/processor-timestamp.html 来进行处理
"2022-01-26 06:43:31.632 | log message"
1.1.1.1 - - [27/Jan/2022:17:08:47 +0800] "GET xxxx"
要注意的是layouts必须使用固定的时间来进行表示,之前没认真看文档,导致时间一直是错误时间
以下配置可以同时处理上述日志格式,获取到真实的日志实时作为timestamp字段存储到es中
- script:
lang: javascript
id: timestamp_filter
tag: enable
source: >
function process(event) {
var str=event.Get("message");
var logPath=event.Get("log.file.path");
if(logPath.indexOf("nginx") != -1){
var time=str.match(/\[(.+?)\]/g);
if (time !== null) {
time = time[0];
if (time !== null) {
time= time.substring(1, time.length - 1)
}
}
} else {
var time=str.substr(0,23);
}
event.Put("log_time",time);
}
- timestamp:
field: log_time
timezone: Asia/Shanghai
layouts:
- '2006-01-02 15:04:05'
- '2006-01-02 15:04:05.999'
- '02/Jan/2006:15:04:05 +0800'
test:
- '2020-08-05 16:21:51.824'
- '26/Jan/2022:15:06:12 +0800'