logstash导入日志到elasticsearch时提取日志时间作为@timestamp属性。
filter{
grok{
match => {"message" => "(?<datetime>\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})\s*%{LOGLEVEL:loglevel}\s*%{GREEDYDATA:msg}"}
} ### 通过grok匹配内容并将
date{
match => ["datetime","yyyy-MM-dd HH:mm:ss"]
target => "@timestamp"
} ### 处理时间
}
日志信息格式:
2022-04-08 04:20:53 INFO [http-nio-8804-exec-6]>>>>>>>>>>sessionIde958c7b8-281e-48a9-9b65-294c50fd47cc - c.f.w.c.i.c.i.AuthChannelImpl[notAuth]
日志解析输出结果:
{
"loglevel" => "INFO",
"msg" => "[http-nio-8804-exec-6]>>>>>>>>>>sessionIde958c7b8-281e-48a9-9b65-294c50fd47cc - c.f.w.c.i.c.i.AuthChannelImpl[notAuth]\r",
"type" => "type1",
"datetime" => "2022-04-08 04:20:53",
"@version" => "1",
"host" => "DESKTOP-O93E7VQ",
"@timestamp" => 2022-04-07T20:20:53.000Z,
"path" => "e:/logstash-test/log-info.log",
"message" => "2022-04-08 04:20:53 INFO [http-nio-8804-exec-6]>>>>>>>>>>sessionIde958c7b8-281e-48a9-9b65-294c50fd47cc - c.f.w.c.i.c.i.AuthChannelImpl[notAuth]\r"
}
可见,上述的日志格式和提取配置是正确的。
开始的时候的配置如下(年采用两位数字简写方式):
filter{
grok{
match => {"message" => "(?<datetime>\d{2}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})\s*%{LOGLEVEL:loglevel}\s*%{GREEDYDATA:msg}"}
} ### 通过grok匹配内容并将
date{
match => ["datetime","yy-MM-dd HH:mm:ss"]
target => "@timestamp"
} ### 处理时间
}
日志格式:
22-04-08 04:20:53 INFO [http-nio-8804-exec-6]>>>>>>>>>>sessionIde958c7b8-281e-48a9-9b65-294c50fd47cc - c.f.w.c.i.c.i.AuthChannelImpl[notAuth]
该配置方式和日志,不能将日志中的日期时间替换为日志的日期时间,所以日志的日期时间时导入时系统的日期时间。
也就是说,提取日志中的日期时间,作为导入es的日志时间时,不支持年的2位数字简写方式。