Grok 模式解析 ESC 键

rez*_*yat 2 regex ansi-escape grok fluentd logstash-grok

我正在编写一种gr​​ok 模式来解析cinder-apifluenced中的日志,其中一行是:

2015-09-17 17:44:49.663 ^[[00;32mDEBUG oslo_concurrency.lockutils [^[[00;36m-^[[00;32m] ^[[01;35m^[[00;32mAcquired semaphore "singleton_lock"^[[00m ^[[00;33mfrom (pid=30534) lock /usr/local/lib/python2.7/dist-packages/oslo_concurrency/lockutils.py:198^[[00m
Run Code Online (Sandbox Code Playgroud)

^[[00;32m其他此类事件是ASCII 颜色代码,在终端中打印时打印如下:

我需要解析该行,并且当没有使用(测试的)模式的 颜色代码时我能够做到这一点%{TIMESTAMP_ISO8601:timestamp}%{SPACE}%{LOGLEVEL:loglevel}%{SPACE}{NOTSPACE:api}%{SPACE}\[(?:%{DATA:request})\]%{SPACE}%{GREEDYDATA:message}

如何修改 grok 模式以便能够解析彩色日志行?

我发现以下内容是否可以帮助任何人找到解决方案:

  • ^[实际上是ESC键,八进制代码为\033,十六进制代码为\x1B,十进制 ASCII 代码为27 ,也用^[表示。
  • 有一个名为color-stripper的fluidd 插件具有相同的功能,但对我不起作用,也不适合我的用例。

Tho*_*key 5

比字面转义字符更好的解决方案是遵循提供的链接中的提示:

  • 常用表达

    Grok 位于正则表达式之上,因此任何正则表达式在 grok 中也有效。正则表达式库是 Oniguruma,您可以在 Oniguruma 站点上查看完整支持的正则表达式语法。

  • Oniguruma 正则表达式:2. 字符 \t horizontal tab (0x09) \v vertical tab (0x0B) \n newline (0x0A) \r return (0x0D) \b back space (0x08) \f form feed (0x0C) \a bell (0x07) \e escape (0x1B)

此外,颜色代码可以与不使用两位数字的其他视频属性混合。引用XTerm 控制序列

CSI Pm m Character Attributes (SGR). Ps = 0 -> Normal (default). Ps = 1 -> Bold. Ps = 2 -> Faint, decreased intensity (ISO 6429). Ps = 3 -> Italicized (ISO 6429). Ps = 4 -> Underlined. Ps = 5 -> Blink (appears as Bold). Ps = 7 -> Inverse. Ps = 8 -> Invisible, i.e., hidden (VT300). Ps = 9 -> Crossed-out characters (ISO 6429). Ps = 2 1 -> Doubly-underlined (ISO 6429). Ps = 2 2 -> Normal (neither bold nor faint). Ps = 2 3 -> Not italicized (ISO 6429). Ps = 2 4 -> Not underlined. Ps = 2 5 -> Steady (not blinking). Ps = 2 7 -> Positive (not inverse).

您可能还会看到正常粗体下划线反向的内容。最后,参数的数量不限于两个,并且参数是可选的。结果可能是

\e\[(\d*;)*(\d*)m
Run Code Online (Sandbox Code Playgroud)