Logstash - 从日志中添加字段 - Grok

时间:2016-12-22 01:21:36

标签: elasticsearch logstash logstash-grok logstash-configuration

我正在学习logstash,我正在使用Kibana来查看日志。我想知道是否有使用消息属性中的数据添加字段。

例如,日志是这样的:

@timestamp:December 21st 2016, 21:39:12.444 port:47,144  
appid:%{[path]} host:172.18.0.5 levell:level message:
{"@timestamp":"2016-12-22T00:39:12.438+00:00","@version":1,"message":"Hello","logger_name":"com.empresa.miAlquiler.controllers.UserController","thread_name":"http-nio-7777-exec-1","level":"INFO","level_value":20000,
"HOSTNAME":"6f92ae402cb4","X-Span-Export":"false","X-B3-SpanId":"8f548829e9d18a8a","X-B3-TraceId":"8f548829e9d18a8a"} 

我的logstash conf就像:

filter {
grok {
match => {
  "message" =>
  "^%{TIMESTAMP_ISO8601:timestamp}\s+%{LOGLEVEL:level}\s+%{NUMBER:pid}\s+---\s+\[\s*%{USERNAME:thread}\s*\]\s+%{JAVAFILE:class}\s*:\s*%{DATA:themessage}(?:\n+(?<stacktrace>(?:.|\r|\n)+))?$"
}
}
 date {
match => [ "timestamp" , "yyyy-MM-dd HH:mm:ss.SSS" ]
}
mutate {
remove_field => ["@version"]
add_field => {
  "appid" => "%{[path]}"
}
add_field => {
  "levell" => "level"
}

} }

我想取水平(在日志中是INFO)和消息(在日志中是Hello)并将它们添加为字段。

有没有这样做?

1 个答案:

答案 0 :(得分:3)

如果您使用mutate执行此类操作会怎样:

filter { 
  mutate { 
    add_field => ["newfield", "%{appid} %{levell}"] <-- this should concat both your appid and level to a new field
  } 
} 

您可以查看此thread