Redirect Systemd Unit Logs to Syslog

Redirect Systemd Unit Logs to Syslog

By default when we create a systemd unit file and start the service using systemd, we can access the logs using journalctl as:

$ sudo journalctl -fu <unit-name>

If we wanted to redirect the log to syslog so that we have the logs available in /var/log/syslog and set a specific indentifier, its possible to do that inside the systemd unit file.

For example if we look at a sample dashd service in /etc/systemd/system/dashd.service:

[Unit]
Description=Dashcoin (DASH) Testnet
After=network.target

[Service]
User=pi
Group=pi
WorkingDirectory=/blockchain/dash/data
Type=simple
ExecStart=/blockchain/dash/software/current/bin/dashd -conf=/blockchain/dash/config/dash.conf
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=dashd

[Install]
WantedBy=multi-user.target

The important part to focus on is:

StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=dashd

As you can see we are setting the identifier as dashd, if you made changes to your unit file, reload the daemon:

$ sudo systemctl daemon-reload

Then restart the service for it to can take affect:

$ sudo systemctl restart dashd

Now we can access the logs on syslog:

$ tail -f /var/log/syslog | grep dash
Oct 20 13:44:20 rpi-05 dashd[9475]: 2021-10-20T11:44:20Z ProcessNewBlock : ACCEPTED

And we can still access the logs using journalctl:

$ sudo journalctl -fu dashd
-- Logs begin at Thu 2019-02-14 12:11:59 SAST. --
Oct 20 13:45:59 rpi-05 dashd[9475]: 2021-10-20T11:45:59Z ProcessNewBlock : ACCEPTED