syslog command in C code

user1060517 picture user1060517 · Dec 13, 2011 · Viewed 28k times · Source
#include<syslog.h>
syslog(LOG_INFO, "Start logging");

The above syslog command is not logging in the syslog. So I tried,

openlog("Logs", "", LOG_USER);
syslog(LOG_INFO, "Start logging");
closelog();

This fails to log anything and I get the following error:

syslog: unknown facility/priority: 8049584

Answer

sarnold picture sarnold · Dec 13, 2011

This line is wrong:

openlog("vyatta-conntrack", "", LOG_USER);

The "" should have been an integer:

void openlog(const char *ident, int option, int facility);

The integer should be any of these constants ORed together:

   LOG_CONS       Write directly to system console if there is
                  an error while sending to system logger.

   LOG_NDELAY     Open the connection immediately (normally, the
                  connection is opened when the first message is
                  logged).

   LOG_NOWAIT     Don't wait for child processes that may have
                  been created while logging the message.  (The
                  GNU C library does not create a child process,
                  so this option has no effect on Linux.)

   LOG_ODELAY     The converse of LOG_NDELAY; opening of the
                  connection is delayed until syslog() is
                  called.  (This is the default, and need not be
                  specified.)

   LOG_PERROR     (Not in POSIX.1-2001.)  Print to stderr as
                  well.

   LOG_PID        Include PID with each message.

Try again with something more like:

openlog("vyatta-conntrack", LOG_PID, LOG_USER);

Note that your syslogd configuration might not be set up to keep messages of log level LOG_INFO. Try LOG_ALERT or something for debugging this problem -- if it works, then drop back to LOG_INFO and configure your syslogd to keep the log messages that you want to keep. Adding a line like:

*.* /var/log/all_messages.log

will do the job for rsyslogd(8). Be sure to read the rsyslog.conf(5) manpage if your system uses rsyslogd(8). If your system uses a different syslog daemon, then check your system's manpages for details.