I am running Go binary as a service, and created a log file owned by syslog to maintain all logs.

This is my service file::


WorkingDirectory= /home/go/src/myapp

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


After this 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

After this I restarted service.It creates a log file for me. I have two questions regarding log file, as follows::

  1. Now I have to flush all log file data after every 15 days. Is there any method to do this??
  2. There are some api which will give response in every 3 minutes, so Is there any way to restrict some api from generating logs??

I am doing all this using systemd. Please suggest me if there method to create this type of log file attached with systemd service.

  • Your second question, which largely duplicates unix.stackexchange.com/questions/470181 , is unanswerable. Answerers are not telepathic, and so do not know the code of the program that contains "some API" and whether that program even allows detailed control of its log message generation. This site also prefers one question per question, per the help center. Since you have already asked that unanswerable question before, you are better off not asking it again in a multiple-question question here. – JdeBP Sep 21 '18 at 8:47
  1. To flush that log you can use standard logrotate, with reload of rsyslog in postrotate script. man page
  2. As you can see. Systemd will write all standard output into syslog, So filtering which api should or shouldn't go to stdout has to be done inside application.
| improve this answer | |
  • where I can create logrotate message file? and how to attach logrotate with logfile – Archana Sharma Sep 21 '18 at 8:56
  • install logrotate and put logrotate file into /etc/logrotate.d. attaching to file is first line in logrotate file(the one before '{' sign). There are awesome examples in that man page – Alexander Sep 21 '18 at 8:59
  • I have created a logrotate file with the name myapp, and added these lines inside file:: /var/log/myapp/myapp.log { rotate 5 daily size 100k compress } after this I restart the service, for testing purpose I am using size 100k here, after exceeding 100k size it there is no zip file inside myapp folder, even it's size grows bigger and bigger – Archana Sharma Sep 21 '18 at 9:54
  • I am just restarting service after these changes, and according to these configurations, a new zip file of log will be created when it exceed the size of 100k. But It didn't work like this. Can you please tell me why this happening here?? – Archana Sharma Sep 21 '18 at 10:04
  • Logrotate is running via cron. As daily job. Wait until tomorrow or run it manually logrotate -f yourconfig -v – Alexander Sep 21 '18 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.