#include <syslog.h>
void openlog(char*ident,int option ,int facility);
void syslog(int priority,char*format,……) void closelog()
例子:html
openlog(argv[0], LOG_PID, LOG_LOCAL5);
a. openlog會指定一個indent,那麼這個程序的每一個信息前面都有這個indent,通常是這個程序名。linux
b. option控制syslog行爲:ide
option 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 POSIX.1-2001 or POSIX.1-2008.) Print to stderr as well. LOG_PID Include PID with each message.
c. 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.
配置文件是/etc/syslog.conf,能夠配置不一樣程序類型,保存不一樣的位置:this
kern.* /var/log/kern.log user.* /var/log/syslog local2.* /var/log/network.log local3.* /var/log/onvif.log local5.* /var/log/media.log local6.* /var/log/rtspd.log
例子:spa
syslog(LOG_DEBUG, "This is test message %s\n", argv[0]);
第一個參數是level,表示消息的重要性,syslog man page裏關於level的選項,從上往下重要性逐漸降低。debug
level 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
後面的參數就是fmt與參數。code
closelog()關閉描述符。是否使用是可選的。orm