How to indicate the log level of a line of output from a systemd service

systemdsystemd-journald

I have a script that I intend to run as a systemd service.

I know that stdout and stderr will be routed to the systemd journal. Is there any way for me to indicate that a line of output be treated as INFO or ERROR or whatever is appropriate?

Best Answer

From systemd for Developers III: Revenge of the systemd we find:

The printed string in this example is logged at a default log priority of LOG_INFO1. Sometimes it is useful to change the log priority for such a printed string. When systemd parses STDOUT/STDERR of a service it will look for priority values enclosed in < > at the beginning of each line

And the magic numbers are apparently copied from syslog(3) so

$ egrep 'INFO|ERR' /usr/include/sys/syslog.h | fgrep define
#define LOG_ERR     3   /* error conditions */
#define LOG_INFO    6   /* informational */
#define LOG_PERROR  0x20    /* log to stderr as well */

To emit info or error messages from e.g. a TCL script would look something like

#!/usr/bin/env expect
puts "<3>auuugh"
puts "<6>infos"
Related Question