如何在 Logstash 配置文件中包含过滤器?

How to include the filter in Logstash config file?

我正在使用 LogStash,它接受来自日志文件的数据,该文件具有不同类型的日志。

第一行代表自定义日志,而第二行代表 JSON 格式的日志。

现在,我想写一个过滤器,它会根据内容解析日志,最后将所有 JSON 格式的日志定向到一个名为 jsonformat.log 的文件,其他日志到一个单独的文件。

您可以利用 json 过滤器并检查它是否失败来决定将事件发送到哪里。

input {
   file {
       path => "/Users/mysystem/Desktop/abc.log"
       start_position => beginning
       ignore_older => 0
   }
}
filter {
   json {
     source => "message"
   }
}
output {
  # this condition will be true if the log line is not valid JSON
  if "_jsonparsefailure" in [tags] {
    file {
       path => "/Users/mysystem/Desktop/nonjson.log"
    }
  }
  # this condition will be true if the log line is valid JSON
  else {
    file {
       path => "/Users/mysystem/Desktop/jsonformat.log"
    }
  }
}