如果你将哈希字段(没有时间戳)注入到 ES 中,它应该能够识别它。
如果您想在 logstash 管道中执行此操作,您可以使用 json filter 并将 source => 指向该行的第二部分(可能会添加时间戳前缀)。
这会导致所有字段都添加到当前消息中,您可以直接访问它们或将它们全部组合起来:
配置:
input { stdin { } }
filter {
# split line in Timestamp and Json
grok { match => [ message , "%{NOTSPACE:ts} %{NOTSPACE:ip} %{GREEDYDATA:js}"] }
# parse json part (called "js") and add new field from above
json { source => "js" }
}
output {
# stdout { codec => rubydebug }
# you access fields directly with %{fieldname}:
stdout { codec => line { format => "sourcefile: %{sourcefile}"} }
}
示例运行
2016-09-01T21:07:30.152Z 153.65.199.92 { "sourcefile":"/usr" }
sourcefile: /usr
并使用 rubydebug(主机和 @timestamp 已删除):
{
"message" => "2016-09-01T21:07:30.152Z 153.65.199.92 { \"sourcefile\":\"/usr\" }",
"@version" => "1",
"ts" => "2016-09-01T21:07:30.152Z",
"ip" => "153.65.199.92",
"js" => "{ \"sourcefile\":\"/usr\" }",
"sourcefile" => "/usr"
}
如您所见,sourcefile 字段直接与 rubydebug 输出中的值有关。
根据日志记录的来源,您可能还需要使用multiline 编解码器。您可能还想删除 js 字段,将 @timestamp 重命名为 _parsedate 并将 ts 解析为记录时间戳(让 Kibana 开心)。这未在示例中显示。我还会删除 message 以节省空间。