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



我有以下rsyslog配置,它读取非标准格式的日志文件,并将我需要的数据解析为json有效负载。现在,当我试图提取最后一组括号后面的一切可以包含[Info ][Error ]它抛出一个错误说:错误编译regex。我知道regex模式(?:[Infos*]|[Errors*])s*(.*)应该工作(在rsyslog的网站上的regex检查器上测试,以及在其他检查器上),但我不太明白为什么rsyslog不能编译它。如果我不转义括号,它就会抛出一堆错误。我错过了什么明显的东西吗?

/道路//日志/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 '(?:[Infos*]|[Errors*])s*(.*)' [v8.2302.0]

regex ERE语法不包括非捕获语法(?:)。也许regex检查器适用于较新版本的rsyslog。您可以简单地将regex.submatch更改为2:

regex.expression="(\[Info\s*\]|\[Error\s*\])\s*(.*)"
regex.submatch="2"

最新更新