正则表达式 – 如何处理logstash多行过滤器的消息字段?

前端之家收集整理的这篇文章主要介绍了正则表达式 – 如何处理logstash多行过滤器的消息字段?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
背景:

我有一个自定义生成的日志文件具有以下模式:

  1. [2014-03-02 17:34:20] - 127.0.0.1|ERROR| E:\xampp\htdocs\test.PHP|123|subject|The error message goes here ; array (
  2. 'create' =>
  3. array (
  4. 'key1' => 'value1','key2' => 'value2','key3' => 'value3'
  5. ),)
  6. [2014-03-02 17:34:20] - 127.0.0.1|DEBUG| flush_multi_line

第二个条目[2014-03-02 17:34:20] – 127.0.0.1 | DEBUG | flush_multi_line是一个虚拟行,只是为了让logstash知道多行事件结束,这一行在后面被删除

我的配置文件如下:

  1. input {
  2. stdin{}
  3. }
  4.  
  5. filter{
  6. multiline{
  7. pattern => "^\["
  8. what => "prevIoUs"
  9. negate=> true
  10. }
  11. grok{
  12. match => ['message',"\[.+\] - %{IP:ip}\|%{LOGLEVEL:loglevel}"]
  13. }
  14.  
  15. if [loglevel] == "DEBUG"{ # the event flush line
  16. drop{}
  17. }else if [loglevel] == "ERROR" { # the first line of multievent
  18. grok{
  19. match => ['message',".+\|.+\| %{PATH:file}\|%{NUMBER:line}\|%{WORD:tag}\|%{GREEDYDATA:content}"]
  20. }
  21. }else{ # its a new line (from the multi line event)
  22. mutate{
  23. replace => ["content","%{content} %{message}"] # Supposing each new line will override the message field
  24. }
  25. }
  26. }
  27.  
  28. output {
  29. stdout{ debug=>true }
  30. }

内容字段的输出是:错误信息在这里;数组(

问题:

我的问题是我想将其余的多行存储到内容字段中:

  1. The error message goes here ; array (
  2. 'create' =>
  3. array (
  4. 'key1' => 'value1',)

所以我可以稍后删除消息字段。

@message字段包含整个多行事件,所以我尝试了mutate过滤器,替换功能就是这样,但我只是无法使其工作:(。

我不明白Multiline过滤器的工作方式,如果有人可以对此进行一些说明,那将是非常感激的。

谢谢,

阿卜杜

我浏览了源代码,发现:

>多行过滤器将取消被认为是待处理事件后续的所有事件,然后将该行附加到原始消息字段,这意味着在多行过滤器之后的任何过滤器将不适用于此情况
>唯一会通过过滤器的事件是被认为是新的过滤器(从我的例子开始的事情)

这是工作代码

  1. input {
  2. stdin{}
  3. }
  4.  
  5. filter{
  6. if "|ERROR|" in [message]{ #if this is the 1st message in many lines message
  7. grok{
  8. match => ['message',"\[.+\] - %{IP:ip}\|%{LOGLEVEL:loglevel}\| %{PATH:file}\|%{NUMBER:line}\|%{WORD:tag}\|%{GREEDYDATA:content}"]
  9. }
  10.  
  11. mutate {
  12. replace => [ "message","%{content}" ] #replace the message field with the content field ( so it auto append later in it )
  13. remove_field => ["content"] # we no longer need this field
  14. }
  15. }
  16.  
  17. multiline{ #Nothing will pass this filter unless it is a new event ( new [2014-03-02 1.... )
  18. pattern => "^\["
  19. what => "prevIoUs"
  20. negate=> true
  21. }
  22.  
  23. if "|DEBUG| flush_multi_line" in [message]{
  24. drop{} # We don't need the dummy line so drop it
  25. }
  26. }
  27.  
  28. output {
  29. stdout{ debug=>true }
  30. }

干杯,

阿卜杜

猜你在找的正则表达式相关文章