12

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;
}
dev-masih
  • 4,188
  • 3
  • 33
  • 55
suresh
  • 1,109
  • 1
  • 8
  • 24

2 Answers2

11

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.

Community
  • 1
  • 1
lupz
  • 3,620
  • 2
  • 27
  • 43
  • 1
    How to configure syslog.conf so that the log messages from my application go to a specific file ? Writing onto a simple file is not a good solution - read this link http://stackoverflow.com/questions/158457/logging-in-linux and the answer given by http://stackoverflow.com/users/7740/richard – suresh Apr 29 '12 at 23:10
  • @suresh You can directly give the link to an answer by copying the link location from the 'share' link after each answer. Like this - http://stackoverflow.com/a/158493/598175 – deppfx Apr 16 '15 at 00:02
1

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