我正在尝试解析日志并使用logstash将其置于弹性搜索中。
我的日志文件采用以下格式
[18-Aug-2016 02:28:46,537][ERROR][thread1][package.name] there is error in line 52
\[%{GREEDYDATA:date} %{GREEDYDATA:time}\]\[%{LOGLEVEL:log_type}\]\[%{GREEDYDATA:thread_name}\]\[%{GREEDYDATA:package}\](%{GREEDYDATA:log_msg})?
当我运行这个grok过滤器时,我得到了正确的输出。但是,有些情况下我得到的输入没有最后一个字段(log_msg)。像这样的东西:
[18-Aug-2016 02:28:46,537][ERROR][thread1][package.name]
在这种情况下,grok忽略了最后一个字段log_msg,并且没有插入弹性搜索。
但是,有没有办法,如果消息中不存在,我们可以为log_msg字段设置一个空字符串或字符串,说“无数据”。
真实输出:
{
"message" => "[18-Aug-2016 02:28:46,537][ERROR][thread1][package.name]",
"@version" => "1",
"@timestamp" => "2016-08-17T12:31:58.209Z",
"path" => "/home/admin-nfv/test1_log.log",
"host" => "nendc1-bg-d104",
"date" => "18-Aug-2016",
"time" => "02:28:46,537",
"log_type" => "ERROR",
"thread_name" => "thread1",
"package" => "package.name"
}
预期产出:
{
"message" => "[18-Aug-2016 02:28:46,537][ERROR][thread1][package.name]",
"@version" => "1",
"@timestamp" => "2016-08-17T12:31:58.209Z",
"path" => "/home/admin-nfv/test1_log.log",
"host" => "nendc1-bg-d104",
"date" => "18-Aug-2016",
"time" => "02:28:46,537",
"log_type" => "ERROR",
"thread_name" => "thread1",
"package" => "package.name",
"log_msg" => "no data"
}
答案 0 :(得分:3)
您可以添加mutate
过滤器,如果不存在,则会添加空字段:
filter {
if ![log_msg] {
mutate {
add_field => {"log_msg" => "no data" }
}
}
}