Linux‎ > ‎

How to syslog your program output

Thank you for visiting this page, this page has been update in another link How to syslog your program output
How to syslog your application output

Here is an example in C code

  openlog("Logs", LOG_NDELAY, LOG_DAEMON);
  syslog(LOG_DEBUG, "Here comes test logging ...");

Here is the description about three functions above
closelog() closes the descriptor being used to write to the system logger. The use of closelog() is optional.
openlog() opens a connection to the system logger for a program. The string pointed to by ident is prepended to every message, and is typically set to the program name. The option argument specifies flags which control the operation of openlog() and subsequent calls to syslog(). The facility argument establishes a default to be used if none is specified in subsequent calls to syslog(). Values for option and facility are given below. The use of openlog() is optional; it will automatically be called by syslog() if necessary, in which case ident will default to NULL.
syslog() generates a log message, which will be distributed by syslogd(8). The priority argument is formed by ORing the facility and the level values (explained below).

This section lists the parameters used to set the values of option, facility, and priority.  
The option argument to openlog() is an OR of any of these:
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 SUSv3.) Print to stderr as well. LOG_PID Include PID with each message.  


The facility argument is used to specify what type of program is logging the message. This lets the configuration file specify that messages from different facilities will be handled differently. LOG_AUTH security/authorization messages (DEPRECATED Use LOG_AUTHPRIV instead)
LOG_AUTHPRIV security/authorization messages (private) LOG_CRON clock daemon (cron and at)
LOG_DAEMON system daemons without separate facility value LOG_FTP ftp daemon LOG_KERN kernel messages
LOG_LOCAL0 through LOG_LOCAL7 reserved for local use
LOG_LPR line printer subsystem LOG_MAIL mail subsystem LOG_NEWS USENET news subsystem
LOG_SYSLOG messages generated internally by syslogd
LOG_USER (default) generic user-level messages
LOG_UUCP UUCP subsystem
This determines the importance of the message. The levels are, in order of decreasing importance:
LOG_EMERG system is unusable
LOG_ALERT action must be taken immediately
LOG_CRIT critical conditions
LOG_ERR error conditions LOG_WARNING warning conditions LOG_NOTICE normal, but significant, condition
LOG_INFO informational message
LOG_DEBUG debug-level message
The function setlogmask(3) can be used to restrict logging to specified levels only.  

Another simple way is to call logger in shell
logger -p daemon.debug  " daemon started"

To be mentioned is that you need to configure your /etc/rsyslogd.conf to have a line like like this

daemon.debug /var/log/daemon.log
daemon.* which includs all priority level. Of couse you can change the log output file.

restart syslogd