How to escape or remove double quotes in rsyslog template

Posted by Evgeny on Server Fault See other posts from Server Fault or by Evgeny
Published on 2011-05-31T17:29:17Z Indexed on 2012/06/19 21:19 UTC
Read the original article Hit count: 881

Filed under:
|

I want rsyslog to write log messages in JSON format, which requires to use double-quotes (") around strings.

Problem is that values sometime include double-quotes themselves, and those need to be escaped - but I can't figure out how to do that.

Currently my rsyslog.conf contains this format that I use (a bit simplified):

$template JsonFormat,"{\"msg\":\"%msg%\",\"app-name\":\"%app-name%\"}\n",sql

But when a msg arrives that contains double quotes, the JSON is broken, example:

user pid=21214 uid=0 auid=4294967295 msg='PAM setcred:
user="oracle" exe="/bin/su" (hostname=?, addr=?, terminal=?
result=Success)'

turns into:

{"msg":"user pid=21214 uid=0 auid=4294967295 msg='PAM setcred:
user="oracle" exe="/bin/su" (hostname=?, addr=?, terminal=?
result=Success)'","app-name":"user"}

but what I need it to become is:

{"msg":"user pid=21214 uid=0 auid=4294967295 msg='PAM setcred:
user=\"oracle\" exe=\"/bin/su\" (hostname=?, addr=?, terminal=?
result=Success)'","app-name":"user"}

© Server Fault or respective owner

Related posts about rsyslog

Related posts about escaping