Logging from a script: logger
When you write a script or a service, send its messages to the journal with logger instead of scattering them in files. -t TAG labels them so you can find them, and -p sets the priority:
logger -t backup "nightly backup started"
logger -t backup -p user.err "nightly backup FAILED"
sudo journalctl -t backup --no-pagerIn the terminal
student@vulntech:~$ logger -t backup "nightly backup started" student@vulntech:~$ logger -t backup -p user.err "nightly backup FAILED" student@vulntech:~$ sudo journalctl -t backup --no-pager [sudo] password for student: Oct 18 08:23:59 vulntech backup[588]: nightly backup started Oct 18 08:24:04 vulntech backup[597]: nightly backup FAILED
Both messages are now in the journal under the tag backup, and the second one is marked as an error, so sudo journalctl -p err -t backup would show just the failure.
Why the journal beats a log file
Logging to the journal (or just printing to standard output from a systemd service, which systemd captures automatically) means every log is in one searchable place, with the time, the service and the priority attached — no scattered files to hunt through, and the same sudo journalctl filters work on all of it.
Tip:
sudo journalctl -ffollows the journal, printing new entries as they arrive — the live view you watch while reproducing a problem (liketail -ffor the journal).
Remember:
logger -t TAG -p FACILITY.LEVEL "message"adds an entry to the journal; find it again withsudo journalctl -t TAG. systemd services log just by printing to standard output.