如何在grok / logstash中匹配换行符?

时间:2014-10-20 21:04:03

标签: regex logstash logstash-grok

我有一台远程机器,它结合了多行事件并通过伐木工人协议发送它们。

有什么东西看起来像这样:

{
     "message" => "2014-10-20T20:52:56.133+0000 host 2014-10-20 15:52:56,036 [ERROR   ][app.logic     ] Failed to turn message into JSON\nTraceback (most recent call last):\n  File \"somefile.py", line 249, in _get_values\n    return r.json()\n  File \"/path/to/env/lib/python3.4/site-packages/requests/models.py\", line 793, in json\n    return json.loads(self.text, **kwargs)\n  File \"/usr/local/lib/python3.4/json/__init__.py\", line 318, in loads\n    return _default_decoder.decode(s)\n  File \"/usr/local/lib/python3.4/json/decoder.py\", line 343, in decode\n    obj, end = self.raw_decode(s, idx=_w(s, 0).end())\n  File \"/usr/local/lib/python3.4/json/decoder.py\", line 361, in raw_decode\n    raise ValueError(errmsg(\"Expecting value\", s, err.value)) from None\nValueError: Expecting value: line 1 column 1 (char 0), Failed to turn message into JSON"
}

当我尝试将消息与

匹配时
grok {         
    match => [ "message", "%{TIMESTAMP_ISO8601:timestamp} \[%LOGLEVEL:loglevel}%{    SPACE}\]\[%{NOTSPACE:module}%{SPACE}\]%{GREEDYDATA:message}" ]
}

GREEDYDATA并不像我想的那样贪婪。

然后我尝试使用gsub:

mutate {
    gsub => ["message", "\n", "LINE_BREAK"]
}
# Grok goes here
mutate {
    gsub => ["message", "LINE_BREAK", "\n"]
}

但是那个不起作用而不是

The Quick brown fox
jumps over the lazy
groks

我得到了

The Quick brown fox\njumps over the lazy\ngroks

因此...

如何将换行符添加回我的数据,让GREEDYDATA与我的换行符匹配,或以其他方式获取我的讯息的相关部分?

3 个答案:

答案 0 :(得分:66)

所有GREEDYDATA都是.*,但.与换行符不匹配,因此您可以将%{GREEDYDATA:message}替换为(?<message>(.|\r|\n)*)并让它真实贪婪。

答案 1 :(得分:18)

将正则表达式标志添加到开头可以匹配换行符:

match => [ "message", "(?m)%{TIMESTA...

答案 2 :(得分:1)

我最后一次使用(?m)和[^ \ n] +

记录Vertica日志
match => ["message","(?m)%{TIMESTAMP_ISO8601:ClientTimestamp}%{SPACE}(%{DATA:Action}:)?(%{DATA:ThreadID} )?(\[%{DATA:Module}\] )?(\<%{DATA:Level}\> )?(\[%{DATA:SubAction}\] )?(@%{DATA:Nodename}:)?( (?<Session>(\{.*?\} )?.*?/.*?): )?(?<message>[^\n]+)((\n)?(\t)?(?<StackTrace>[^\n]+))?"]

感谢asperla

https://github.com/elastic/logstash/issues/2282