In older Linux systems, the logger
command can be used to send a log message to syslog.
Reading where does logger
log its messages to in Arch Linux?, it seems that syslog
messages and the logger
command line app only talk to the systemd
journal if a socket for message forwarding is set up.
So what's the modern equivalent of the logger
command? How can I send a message directly to the systemd journal from the command line?
systemd-cat
is the equivalent to logger:In another terminal, running
journalctl -f
:Priorities are specified just by part of the string:
Warnings are bold, emergencies are bold and red. Scary stuff.
You can also use an 'identifier' which is arbitrary, to specify the app name. These are like syslog's old facilities, but you're not stuck with ancient things like
lpr
uucp
nntp
or the ever-descriptivelocal0
throughlocal7
.Is logged as:
Since the question mentions Arch Linux (systemd-controlled from day one) and logging, I'll hazard a guess it is related to logging from a systemd service. Here's another logging technique for shell scripts invoked from systemd service units. systemd can be (and by default is) set up to listen to the service's processes stderr and/or stdout, and forward messages to the journal. When a message starts with a 3-character prefix
'<' N '>'
, where N is a digit from 0 to 7, systemd interprets it as the log level, omits it, and logs the rest of the string at the specified level.It is convenient, since any stderr message from any command will be automatically logged at the error level. Another fd is reserved for logging at an arbitrary different severity.
This is of course available to all programs, not only shell scripts.
Example
A shell script (Bash in this case, relying on process substitution and
trap ... EXIT
behavior), taken from a real service'sExecStartPre
script:The settings in the unit file generally do not need to be set specially. stderr logging works out of the box, unless globally overridden in systemd-system.conf(5) or journald.conf(5). The defaults are:
Note that systemd redirects all commands invoked with the
Exec*
settings, not only the main service processExecStart
, but alsoExecStartPre
,ExecStartPost
, etc.To run the example, save the above script as
logging-test.sh
, run with systemd-run as a temporary unit, then query the full properties of every log record. If you do not see the info level message, check if journald.conf limits the logging level stored in the journal to a higher value.The logging levels are defined in sd-daemon(3):
References
systemd.conf
, named here is the man page).