Skip to content

Instantly share code, notes, and snippets.

@maliarslan
Last active February 21, 2018 14:24
Show Gist options
  • Save maliarslan/1a2f47fa51d4cb050df2faa8c5b4e15f to your computer and use it in GitHub Desktop.
Save maliarslan/1a2f47fa51d4cb050df2faa8c5b4e15f to your computer and use it in GitHub Desktop.
Log service response in a custom file on Ubuntu 16.04

Let's say you have a Node app running as service but you also want to save response in a custom log. Add following lines to your systemd service unit (/lib/systemd/system/myapp.service) file under the [Service] tag

StandardOutput=syslog
StandardError=syslog
SyslogIdentifier='myapp'

Then reload systemd manager configuration

sudo systemctl daemon-reload

Open /etc/rsyslog.conf and uncomment the following lines to listen syslog service on port 514

# provides UDP syslog reception
#module(load="imudp")
#input(type="imudp" port="514")

# provides TCP syslog reception
#module(load="imtcp")
#input(type="imtcp" port="514")

Then restart the service and check if the listener on port 514

sudo systemctl restart rsyslog
netstat -an | grep "LISTEN "

Create /etc/rsyslog.d/myapp.conf file with contents below to tell rsyslog where it will save application log

sudo nano /etc/rsyslog.d/myapp.conf
if $programname == 'myapp' or $syslogtag == 'myapp' then /var/log/myapp/myapp.log
& ~

Restart rsyslog service again

sudo systemctl restart rsyslog

Restart your service again

sudo systemctl restart myapp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment