Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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"}
share|improve this question
    
similar question: mail-archive.com/rsyslog@lists.adiscon.com/msg01707.html –  Evgeny May 31 '11 at 18:01
add comment

2 Answers

up vote 1 down vote accepted

As of rsyslog 4.6.2, it seems you can just use the json property option:

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

See here for more details.

share|improve this answer
add comment

I found an extremely ugly solution to this, that I would gladly replace with something sensible:

$template JsonFormat,"{\"rawmsg\":\"%rawmsg:R,BRE,1,BLANK,0:\([^\"]*\)\"*--end%%rawmsg:R,BRE,1,BLANK,1:\([^\"]*\)\"--end%%rawmsg:R,BRE,1,BLANK,2:\([^\"]*\)\"--end%%rawmsg:R,BRE,1,BLANK,3:\([^\"]*\)\"--end%%rawmsg:R,BRE,1,BLANK,4:\([^\"]*\)\"--end%%rawmsg:R,BRE,1,BLANK,5:\([^\"]*\)\"--end%%rawmsg:R,BRE,1,BLANK,6:\([^\"]*\)\"--end%%rawmsg:R,BRE,1,BLANK,7:\([^\"]*\)\"--end%%rawmsg:R,BRE,1,BLANK,8:\([^\"]*\)\"--end%%rawmsg:R,BRE,1,BLANK,9:\([^\"]*\)\"--end%\",\"msg\":\"%msg:R,BRE,1,BLANK,0:\([^\"]*\)\"*--end%%msg:R,BRE,1,BLANK,1:\([^\"]*\)\"--end%%msg:R,BRE,1,BLANK,2:\([^\"]*\)\"--end%%msg:R,BRE,1,BLANK,3:\([^\"]*\)\"--end%%msg:R,BRE,1,BLANK,4:\([^\"]*\)\"--end%%msg:R,BRE,1,BLANK,5:\([^\"]*\)\"--end%%msg:R,BRE,1,BLANK,6:\([^\"]*\)\"--end%%msg:R,BRE,1,BLANK,7:\([^\"]*\)\"--end%%msg:R,BRE,1,BLANK,8:\([^\"]*\)\"--end%%msg:R,BRE,1,BLANK,9:\([^\"]*\)\"--end%\",\"hostname\":\"%hostname%\",\"fromhost\":\"%fromhost%\",\"syslogtag\":\"%syslogtag%\",\"programname\":\"%programname%\",\"priority\":\"%pri%\",\"priority-text\":\"%PRI-text%\",\"infounittype\":\"%iut%\",\"syslogfacility\":\"%syslogfacility%\",\"syslogfacility-text\":\"%syslogfacility-text%\",\"syslogseverity\":\"%syslogseverity%\",\"syslogseverity-text\":\"%syslogseverity-text%\",\"timegenerated\":\"%timegenerated%\",\"timereported\":\"%timereported%\",\"app-name\":\"%app-name%\",\"procid\":\"%procid%\",\"msgid\":\"%msgid%\"}\n"

What it does it cuts the string into pieces using regular expressions, and then pastes them together with the double quotes removed - also limited to just 10 double quotes in a message.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.