【问题标题】:Multiline parsing pattern多行解析模式
【发布时间】:2018-09-04 11:13:33
【问题描述】:

我想在 ELK stack 6.3.2 版本中解析一个标准的 JAVA 异常,它看起来像:

2018-09-04 05:29:03.955 [default task-38] ERROR c.r.e.u.util.MongoConnectionUtil.createMongoUser - Exception occured while creating mongo userCommand failed with error 11000: 'User "asdf" already exists' on server 192.168.1.33:27017. The full response is { "ok" : 0.0, "errmsg" : "User \"asdf\" already exists", "code" : 11000, "codeName" : "DuplicateKey" }
com.mongodb.MongoCommandException: Command failed with error 11000: 'User "qwer" already exists' on server 192.168.1.33:27017. The full response is { "ok" : 0.0, "errmsg" : "User \"asdf\" already exists", "code" : 11000, "codeName" : "DuplicateKey" }
    at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:115)
    at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:114) ...

我的 filebeat.yml 有配置:

filebeat.inputs:

- type: log
  enabled: true
  paths:
    - C:\logs\test.log
  multiline.pattern: '^[[:space:]]+(at|\.{3})\b|^Caused by:'
  multiline.negate: false
  multiline.match: after

我的 logstash.conf 输入看起来像:

input {

beats {
    port=>5044
        codec => multiline {
               pattern => "^\s"
              what => "previous"
}
}

但是 logstash 说无法解析模式,实际上它崩溃了异常。如果我只删除codec 配置,则异常的第一行将被解析。我也在https://discuss.elastic.co/t/multiline-parsing-patterns/147171 提出了同样的问题,但没有回应。

【问题讨论】:

    标签: elasticsearch logstash elastic-stack filebeat


    【解决方案1】:

    您需要更改: multiline.negatetrue。 而且我不确定您要使用该模式实现什么目标,但似乎您应该使用:

    multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'
    

    此外,您无需在 logstash 中使用多行 - 只需简单:

    input {
        beats {
            port => 5044            
        }
    }
    

    为了获取所有日志,我会将您的 Filebeat 配置更改为:

    - type: log
      enabled: true
      paths:
        - C:\logs\test.log
      multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'
      multiline.negate: true
      multiline.match: after
    

    一点解释: 当我们选择negate:truematch: after 时,我们告诉FileBeat:

    不匹配模式的连续行被追加到 匹配的上一行。

    换句话说 - 它告诉 FileBeat 收集以给定模式开头的每一行,并在该模式再次出现在新行的开头时停止。
    对于这种模式^[0-9]{4}-[0-9]{2}-[0-9]{2},如果你得到这两个例外:

    2018-09-04 05:29:03.955 [default task-38] ERROR c.r.e.u.util.MongoConnectionUtil.createMongoUser - Exception occured while creating mongo userCommand failed with error 11000: 'User "asdf" already exists' on server 192.168.1.33:27017. The full response is { "ok" : 0.0, "errmsg" : "User \"asdf\" already exists", "code" : 11000, "codeName" : "DuplicateKey" }
    com.mongodb.MongoCommandException: Command failed with error 11000: 'User "qwer" already exists' on server 192.168.1.33:27017. The full response is { "ok" : 0.0, "errmsg" : "User \"asdf\" already exists", "code" : 11000, "codeName" : "DuplicateKey" }
        at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:115)
        at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:114) ...
    
    2018-09-04 05:30:00.000 [default task-38] ERROR c.r.e.u.util.MongoConnectionUtil.createMongoUser - Exception occured while creating mongo userCommand failed with error 11000: 'User "asdf" already exists' on server 192.168.1.33:27017. The full response is { "ok" : 0.0, "errmsg" : "User \"asdf\" already exists", "code" : 11000, "codeName" : "DuplicateKey" }
    com.mongodb.MongoCommandException: Command failed with error 11000: 'User "qwer" already exists' on server 192.168.1.33:27017. The full response is { "ok" : 0.0, "errmsg" : "User \"asdf\" already exists", "code" : 11000, "codeName" : "DuplicateKey" }
        at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:115)
        at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:114) ...
    

    它将每个异常捕获为不同的条目日志。如果您记录更多内容并且您希望 filebeat 只收集错误,那是另一回事。在我们的程序中,我们收集所有信息并按严重性进行查询(即错误、信息、警告等)

    【讨论】:

    • 我在文档elastic.co/guide/en/beats/filebeat/current/… 中看到,对于abb 类型的模式,它的错误请解释一下,关于caused by 子句的例外情况,这将与您的模式一起记录吗?
    • 我编辑了一点 - 希望它能回答你问题的第一部分。我不明白你为什么要包括由部分引起的。每个异常都以我编写的格式的时间戳开始(即 [0-9]{4}-[0-9]{2}-[0-9]{2}),这应该可以解决问题并且非常直观我的口味。
    • 你试过我的建议了吗?
    • 是的,但我没有看到日志被附加为一个字符串
    • 你重启服务了吗? -i 刚刚仔细检查,它是正确的配置
    猜你喜欢
    • 2011-08-05
    • 2015-10-27
    • 2018-10-12
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多