logger¶
logger writes to the local syslog socket (or a remote server) and is the standard way to test routing rules.
logger "plain message" # user.notice
logger -p user.info "Test user.info message"
logger -p authpriv.crit "Testing authpriv.crit"
logger -p local4.warning -t switch01 "Simulated device message"
logger -t myapp -i "message with PID included"
logger -s "also echo to stderr"
logger -f /path/to/file.log # log each line of a file
some_command 2>&1 | logger -t some_command # log a command's output
# Send directly to a remote server, bypassing the local daemon
logger -n logs.example.com -P 514 -T "TCP test" # -T tcp, -d udp
logger -n logs.example.com --rfc5424 "RFC 5424 test"
# Structured journal fields
printf 'MESSAGE=deploy finished\nAPP_VERSION=1.4.2\nENVIRONMENT=prod\n' \
| logger --journald
journalctl APP_VERSION=1.4.2
systemd-cat¶
Sends stdout/stderr of a command into the journal with an identifier and priority:
echo "backup started" | systemd-cat -t backup -p info
systemd-cat -t backup -p warning /usr/local/bin/backup.sh
journalctl -t backup
Simulating kernel messages¶
logger -p kern.* won't produce a genuine kernel message: the kernel facility is reserved, and userspace-submitted kern messages get rewritten or flagged by the syslog daemon. To test parsers, alerting rules or dashboards that key on kernel messages, write to /dev/kmsg as root. The message goes into the real kernel ring buffer, gets a real kernel timestamp, and appears in dmesg and journalctl -k with _TRANSPORT=kernel:
# <N> sets the severity (4 = warning). Prefix with a subsystem-style tag.
echo "<4>testdrv: simulated I/O error on sdz, sector 12345" | sudo tee /dev/kmsg
dmesg -T | tail -1
journalctl -k -n 1 -o verbose
Writes to /dev/kmsg are rate-limited by default (sysctl kernel.printk_devkmsg). Don't do this on production systems where kernel messages feed alerting without warning the people who get paged.
If you only need something that looks kernel-like in a text file (for example to test a regex), use a tag: logger -t kernel "[12345.678901] testdrv: simulated message".