Reputation: 1109
I have an application myapp
which should send log files only to /var/log/myapp.log
. myapp
is written in C++. The following sample code, sends the logs to /var/log/syslog only. My os is Linux - Ubuntu 12.04 - to be specific. I also found that my machine has rsyslog than syslog installed.
#include <stdio.h>
#include <unistd.h>
#include <syslog.h>
int main(void) {
openlog("myapp", LOG_PID|LOG_CONS, LOG_USER);
syslog(LOG_INFO, "abc 10");
closelog();
return 0;
}
Upvotes: 13
Views: 51522
Reputation: 1963
This worked for me: OS: Ubuntu 20.04
:programname,contains,"myapp" /var/log/myapp.log
service rsyslog restart
Upvotes: 2
Reputation: 81
If your Ubuntu is working with rsyslog, you need to find the configuration file /etc/rsyslog.conf and modify it.
$ echo "if \$programname == 'myapp' then /var/log/myapp.log \n& ~" >> /etc/rsyslog.conf
Then you restart the utility:
$ sudo service rsyslog restart
And run your programm:
$ ./myapp
Check your log:
$ cat /var/log/myapp.log
Apr 8 14:01:51 myusername myapp[21508]: abc 10
Upvotes: 2
Reputation: 3638
According to the syslog(3) manpage, the first parameter for openlog() sets a prefix for log messages, not a filename. You can use a facility like LOG_LOCAL0 to flag your output and then configure syslogd using /etc/syslog.conf to send those logs to the file of your desire.
Upvotes: 12