If you have to use stdout
/stderr
, you may use sd-daemon
logging prefix.
Prepend your stderr
with <3>
to send an ERROR
priority journald
log.
Using your log-test.sh
and log-test.service
:
#!/bin/bash
>&2 echo "<3>This is ERROR"
echo "This is INFO"
exit 0
And journalctl
output:
$ journalctl -u log-test -p 3
May 02 01:22:58 host.example.com log-test.sh[29909]: This is ERROR
If your fancy-app
has any API to write to syslog
, you can use that to write to UNIX datagram /dev/log
(usually writable by default, and logs to journald
) instead of stdout
/stderr
. Use syslog tag to identify your fancy-app
, syslog priority to error
or info
depending on your needs, and any syslog facility.
For example, in Bash we can use logger
:
# emit INFO message to journalctl
$ logger -t fancy-app -u /dev/log -p user.info "This is INFO"
# emit ERROR message to journalctl
$ logger -t fancy-app -u /dev/log -p user.error "This is ERROR"
# show journald messages for fancy-app
$ journalctl -t fancy-app
May 02 01:23:38 host.example.com fancy-app[27302]: This is INFO
May 02 01:23:39 host.example.com fancy-app[27303]: This is ERROR
# show journald ERROR messages for fancy-app
$ journalctl -t fancy-app -p 3
May 02 01:23:39 host.example.com fancy-app[27303]: This is ERROR
Note that in most distros journald
entries usually forwarded to local syslog daemon (syslog-ng
, rsyslog
, ...), so probably check your syslog filters, or maybe use local0
...local7
facilities.
We have many production apps (first and third-party) that leave logging up to the container and just log to stdout for INFO and stderr for ERROR logs (ie only 2 log priorities: INFO|ERROR).
Most container engine should be able to log to syslog. Without knowing your container engine, I'll use Docker as an example.
Docker has syslog logging driver that can be used to send log messages using syslog format to any syslog target. You should be able to log to journald
with something like:
docker run \
--log-driver syslog \
--log-opt syslog-address=unix:///dev/log \
--log-opt syslog-facility=user \
--log-opt tag=fancy-app \
fancy-app:latest
Docker also has journald logging driver available. For example:
docker run \
--log-driver journald \
--log-opt tag=fancy-app \
fancy-app:latest
Both logging drivers (syslog and journald) supports separation between stdout
and stderr
; i.e. stdout
messages will be logged with INFO
priority, and stderr
messages will be logged with ERROR
priority.
Philosophies and flame wars aside, why not log to real syslog? It is easier, stored in text format, and generally supported by log management softwares (see Graylog, Logstash, Papertrail).