linux syslog

void openlog(const char *ident, int option, int facility);
void syslog(int priority, const char *format, ...);
void closelog(void);

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 connec‐
               tion is opened when the first message is logged).
LOG_NOWAIT     Don't wait for child processes that may have  been  cre‐
               ated 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.

配置文件

# ls /etc/rsyslog.conf
# ls /etc/rsyslog.d/
20-ufw.conf  50-default.conf
# cat 50-default.conf
auth,authpriv.*                 /var/log/auth.log
*.*;auth,authpriv.none          -/var/log/syslog
#cron.*                         /var/log/cron.log
#daemon.*                       -/var/log/daemon.log
kern.*                          -/var/log/kern.log
#lpr.*                          -/var/log/lpr.log
mail.*                          -/var/log/mail.log
#user.*                         -/var/log/user.log
#local5.*                       -/var/log/user.log

auth:身份认证相关
cron:进程或调度相关
daemon:守护进程相关
kern:内核相关
mail:邮件相关
user:用户自定义相关
local[0-7]:用户自定义相关

格式:
facility.level

:配置生效需要重启rsyslog

facility

LOG_AUTH       security/authorization messages
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 pro‐
               cesses)
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

level

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

emerg:紧急
alert:报警
crit:关键
err:错误
warning:警告
notice:通知
info:消息
debug:调试

举例

local5.*                            -/var/log/user.log

openlog("hello furong.", LOG_CONS | LOG_PID | LOG_PERROR, LOG_LOCAL5);
FILE *f = fopen("hello", "r");      
syslog(LOG_ERR, "oops - %m
");  
closelog();
# tail -n1 /var/log/user.log
hello furong.[5317]: oops - No such file or directory
原文地址:https://www.cnblogs.com/zhangxuechao/p/11709709.html