创建一个自定义的patterns,在logstash目录下创建一个patterns
。
> vim patterns
LOG_TIME (\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2},\d{3})
在patterns文本里写入正则表达式,该正则是匹配日志中的时间的。
然后启动执行:
bin/logstash -e 'input{
stdin{}
}
filter{
grok{
patterns_dir=>"./patterns"
match => {
"message" => "%{LOG_TIME:logtime}"
}
}
}
output{
stdout{codec=>rubydebug}
}'
输入测试日志:
[2022-01-01 16:48:24,946] [ERROR] c.b.t.biz.back.billing.BillingBiz 200 -- [f8b9e646-363e-4976-ac83-944a99e159ac] failed transferFee EXCEPTION :c.b.t.m.common.exception.FrontParamException: 错误码:BILLING_FAILED
返回:
{
"message" => "[2016-11-01 16:48:24,946] [ERROR] c.b.t.biz.back.billing.BillingBiz 200 -- [f8b9e646-363e-4976-ac83-944a99e159ac] failed transferFee EXCEPTION :c.b.t.m.common.exception.FrontParamException: 错误码:BILLING_FAILED",
"@version" => "1",
"@timestamp" => "2016-11-04T09:37:30.223Z",
"host" => "linux21-49",
"logtime" => "2016-11-01 16:48:24,946"
}
logtime里面是正则解析出来的日志,ok了。