brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: LogExtraFields= adds your own field to a service's log lines for journalctl

Journalctl
~/tips/tip-journalctl-logextrafields-custom-field

journalctl JOB=nightly-backup

Tags the log lines of several services with one field so you can read them together.

When one job is spread over several services, say a backup unit, a sync unit and a cleanup unit, I want to read their logs as one story. -u per unit works, but there’s a neater way: give all of them the same journal field and filter on that.

LogExtraFields= is a unit setting that attaches extra FIELD=VALUE pairs to every log line the service produces. Journal fields are indexed, and the systemd.exec man page mentions this exact use: matching log lines across units. A drop-in per service does it:

sudo systemctl edit backup.service

[Service]
LogExtraFields=JOB=nightly-backup

sudo systemctl restart backup.service
journalctl JOB=nightly-backup

Put the same line in the drop-ins of the sync and cleanup units and journalctl JOB=nightly-backup shows the three of them together, in time order. journalctl -F JOB lists every value you’ve used so far. You can add more than one pair, separated by spaces, and quote a pair when the value has a space in it.

Pick the field name carefully: capitals, digits and underscores only, and no underscore at the start, because names that start with one are reserved for fields the journal itself fills in. The other catch: it only works for system services, not for services in your --user session, and only lines written after the restart get the field. Older entries stay as they were.

Further reading: systemd.exec(5) manual page, sd_journal_print(3) on field names

More Journalctl tips journalctl -k -g 'killed process' --since "2 days ago" journalctl _PID=1 -b ls -lh /var/log/journal/*/*.journal~ Storage=volatile sudo journalctl --setup-keys All Journalctl tips (81) All topics

Link