Write a log using syslogd

Generally speaking,there is a output from our program.But on server endian,we don't hope that the message printing in the screen.So,we put them in a local file.It's not a good idea to deal with the file operation in our programs.Because it's not convenient to maintain the system for some people who is not familar with the program.

/* test.c */
#include <stdio.h>
#include <syslog.h>

int main(int argc, char **argv)
{
    openlog("syslogTest", LOG_CONS | LOG_PID, 0);
    syslog(LOG_DEBUG,"There is a syslog message generated by '%s'\n",argv[0]);
    closelog();
    return 0;
}

gcc -o syslogTest test.c
./syslogTest

vi /var/log/syslog


void openlog(const char *ident, int option, int facility);
This function connect to the daemon process syslogd.

option:
       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.

facility:
       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 (these can't be generated from user processes)

       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(8)

       LOG_USER (default)    generic user-level messages

       LOG_UUCP       UUCP subsystem

void syslog(int priority, const char *format, ...);

priority:
       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



djstava

作者: djstava   发布时间: 2010-10-13