-1

I am running Go binary as a service on ubuntu 16.04,and not able to create log file which is owned by syslog. I have created a services for myapp.service and it looks like as follows:

[Unit]
Description=myapp

[Service]
Type=simple
Restart=always
RestartSec=30
WorkingDirectory= /home/go/src/myapp
ExecStart=/home/go/src/myapp/myapp  

# myapp.log owned by syslog
PermissionsStartOnly=true
ExecStartPre=/bin/mkdir -p /var/log/myapp
ExecStartPre=/bin/chown syslog:adm /var/log/myapp
ExecStartPre=/bin/chmod 755 /var/log/myapp
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=myapp                           

[Install]
WantedBy=multi-user.target

Now It just creates a folder inside /var/log/myapp . But didn't creates any myapp.log file inside myapp folder. I can check these logs using command ::

sudo journalctl -f -u myapp

It will show all logs inside terminal. I have following questions ::

  1. how can I create a log file, which will maintain all logs(inside /var/log/myapp/myapp.log).
  2. And I also want to categorize logs, like I just want to maintain failure logs only, so after created log file how to write a failure logs inside myapp.log. For now it shown all logs(both success failure) inside terminal.
  3. I am hitting golang api on port 8080, but whenever I checked port it doesn't seems busy. I didn't understand why port 8080 is not busy when I am running it as a service.Although it gives response in both cases but after stopping service and if running app in foreground it shows port 8080 is busy. Is there any reason for this?
  • 1
    are you running your app as user `root`? or do you want it run as `syslog`? What does `systemctl status app` show? With 3), what do you mean by hitting? What does it mean 'doesn't seem to be busy' (on the couch?)? If the app process is running,`lsof -p {pid}` would be useful to see if log directory descriptors exist or listing ports. – danblack Sep 20 '18 at 04:00
  • hitting is used to hit the url on browser, and I am using port `8080` for this. `systemctl status app` shows it's active ,loaded and it's processes status. – Archana Sharma Sep 20 '18 at 04:23
  • I am not able to create myapp.log file inside `/var/log/myapp/myapp.log` , please suggest me how can I create failure log file for myapp.service – Archana Sharma Sep 20 '18 at 04:26
  • Thanks for answering some questions. Other answers may have helped give you and answer to your repeated question. Replace `ExecStart=/usr/bin/strace -s 99 -o /tmp/myapp.strace /home/go/src/myapp/myapp` and look at the `tmp/myapp.strace` and see what syscalls there are around `/var/log/myapp` and *show them*. Maybe apparmor is preventing access. Maybe you should use [journal](https://github.com/coreos/go-systemd/blob/master/journal/journal.go) and not worry about creating log files. – danblack Sep 20 '18 at 05:03
  • I caanot replace ExecStart with this, because I am using go app binary to run as syatemd service. And I need a log file to maintain failure logs – Archana Sharma Sep 20 '18 at 05:08

1 Answers1

0

If you are using rsyslog to manage syslogs, created a file inside /etc/rsyslog.d/myapp.conf with the following content:

if $programname == 'myapp' then /var/log/myapp/myapp.log
& stop

Now restart the rsyslog service and you should see the syslog listener on port 514, restart the myapp, and now you should see log events being sent to the log file.

sudo systemctl restart rsyslog
sudo systemctl restart myapp

But It will creates a log file having both success failure logs.