解析 ESC 键的 Grok 模式

Grok pattern to parse the ESC key

我正在写一个 grok pattern to parse the logs in fluentd of cinder-api,其中一行是:

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

^[[00;32m 和其他类似事件是 ASCII colour codes,在终端中打印时如下所示:

我需要解析线条,并且能够在没有使用 (tested) 模式的颜色代码时执行此操作 %{TIMESTAMP_ISO8601:timestamp}%{SPACE}%{LOGLEVEL:loglevel}%{SPACE}{NOTSPACE:api}%{SPACE}\[(?:%{DATA:request})\]%{SPACE}%{GREEDYDATA:message}

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

我发现了以下内容,如果它可以帮助任何人找到解决方案:

问题已解决。

诀窍是使用 ESC 字符本身,而不是其表示形式 ^[

我使用 emacs,所以我调用了函数 (insert-char) 并输入了字符 1b 的十六进制代码,并在 grok 模式中使用了该字符。

我写的 ANSI 颜色代码的 grok 模式是:

而不是

注意 ^[ 是一个字符。

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

  • Regular Expressions

    Grok sits on top of regular expressions, so any regular expressions are valid in grok as well. The regular expression library is Oniguruma, and you can see the full supported regexp syntax on the Onigiruma site.

  • Oniguruma Regular Expressions: 2. Characters \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 Control Sequences:

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