如何使用 systemd service 匯出 log 訊息到 指定的 file 檔案上

我用 golang 下載別人寫好的 github.com/go 語言時,想要將 log 存在 /var/log/底下。不過都不會出現 log 訊息。所以我去上網去查如何解決。

查了一些資訊,也經過測試,確定以下網友提出解決方式是可以執行,如下連結:
https://stackoverflow.com/questions/37585758/how-to-redirect-output-of-systemd-service-to-a-file


StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=<your program identifier> # without any quote



if $programname == ‘<your program identifier>’ then /path/to/log/file.log & stop


ls -alth /var/log/syslog
-rw-r—– 1 syslog adm 439K Mar 5 19:35 /var/log/syslog
chown syslog:adm /path/to/log/file.log

Then, assuming your distribution is using rsyslog to manage syslogs, create a file in /etc/rsyslog.d/<new_file>.conf with the following content:

Restart rsyslog (sudo systemctl restart rsyslog) and enjoy! Your program stdout/stderr will still be available through journalctl (sudo journalctl -u <your program identifier>) but they will also be available in your file of choice.

額外資訊-查看 systemd 的 log 方式
sudo journalctl -u ran

我將以上的方式,加入 rsyslog.d/底下的設定檔,重新 systemctl daemo-reload
然後 system “服務” restart

就可以看到 log 訊息了