Using this rsyslog config:
$template MYFORMAT,"%msg%\n"
if $programname == 'mylog' then {
action(type="omfile" file="/var/log/mylog.log" template="MYFORMAT")
& stop
}
and this PHP script:
<?php
openlog('mylog', LOG_ODELAY, LOG_LOCAL0);
syslog(LOG_INFO, date('Y-m-d: ') . 'stuff has happened!');
closelog();
My output always ends up having an empty space before the logged message (in the custom log file).
2015-06-10: stuff has happened! (there's a space at the beginning of this line)
Per RFC 3164, anything after the colon in the syslog tag gets counted as part of the
%msg%
field, including any space character. This is alluded to in various rsyslog documentation/blog posts, for example https://www.rsyslog.com/log-normalization-and-the-leading-space/ or thesp-if-no-sp
documentation here https://rsyslog.readthedocs.io/en/latest/configuration/property_replacer.htmlSince it's part of the
%msg%
field, there are two ways to log lines without a leading space:Hard code a prefix as part of every log line, for example:
Strip the leading space character. You can use a
$
sign to say "include everything until the end of the line." The msg characters are 1-indexed, so start with field 2.