Log systemd service stderr to different file using rsyslog

I have the following config in a systemd service:

StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=udocit

And this rsyslog conf file:

if $programname == 'udocit' and $syslogseverity > 5 then {
  action(
    type="omfile"
    FileCreateMode="0640"
    File="/var/log/udocit.log"
  )
& stop
}

if $programname == 'udocit' and $syslogseverity <= 5 then {
  action(
    type="omfile"
    FileCreateMode="0640"
    File="/var/log/udocit.err"
  )
& stop
}

The service starts a nodejs app.

I would expect that messages which come from the console.error() function, which outputs to stderr, would go to the udocit.err file with this config, but all messages are instead written to the udocit.log file.

What am I doing wrong here?


Solution 1:

I don't know what console.error() might do, but systemd will make all messages written to stdout or stderr generate a syslog entry with a default level of info (SyslogLevel=info).

To get a different level the lines printed must begin with a string such as <3> which is level error. Systemd, by default, removes such a prefix and converts the syslog entry severity to it. See man systemd.exec for option SyslogLevelPrefix and man sd-daemon.