rsyslog 无法在模板中编译正则表达式模式

问题描述 投票:0回答:1

我有下面的 rsyslog 配置,它读取一个非标准格式的日志文件并将我需要的数据解析为 json 有效负载。现在,当我试图提取最后一组可以包含

[Info  ]
[Error ]
的括号后面的所有内容时,它会抛出一条错误消息:错误编译正则表达式。我知道正则表达式模式
(?:\[Info\s*\]|\[Error\s*\])\s*(.*)
应该可以工作(在 rsyslog 网站上的正则表达式检查器以及其他检查器上测试过)但我不太明白为什么 rsyslog 无法编译它。如果我不转义括号,它会抛出一堆其他错误。我错过了一些明显的东西吗?

/path/to/log/file.log

 11955 - [Mon Apr  6 20:40:03 2023] [Info   ] This message can contain anything [d54d13fa-4657-4891-f99d08674ee]

/etc/rsyslog.d/mylog.conf

module(load="imfile")
input(type="imfile" tag="mylog" file="/path/to/log/file.log")
    
template(name="jsonFormat" type="list" option.jsonf="on") {                 
    property(outname="msg" name="msg" regex.expression="(?:\\[Info\\s*\\]|\\[Error\\s*\\])\\s*(.*)" regex.type="ERE" regex.submatch="1" format="jsonf")
}

if ($syslogtag == "mylog") then {
        action(type="omfile" file="/path/to/output/file.log" template="jsonFormat")
}

# rsyslogd -N1

rsyslogd: error compiling regex '(?:\[Info\s*\]|\[Error\s*\])\s*(.*)' [v8.2302.0]
regex logging rsyslog
1个回答
1
投票

正则表达式 ERE 语法不包括非捕获语法

(?:)
。也许正则表达式检查器适用于较新版本的 rsyslog。 您可以简单地将
regex.submatch
更改为2:

regex.expression="(\\[Info\\s*\\]|\\[Error\\s*\\])\\s*(.*)"
regex.submatch="2"
© www.soinside.com 2019 - 2024. All rights reserved.