分类: C/C++
2007-10-16 16:13:37
函数声明: #includevoid syslog(int priority, const char *message, arguments...); priority参数的格式(severity level|facility code) 示例: LOG_ERR|LOG_USER severity level: Priority Level Description LOG_EMERG An emergency situation LOG_ALERT High-priority problem, such as database corruption LOG_CRIT Critical error, such as hardware failure LOG_ERR Errors LOG_WARNING Warning LOG_NOTICE Special conditions requiring attention LOG_INFO Informational messages LOG_DEBUG Debug messages facility value(转自syslog.h头文件): /* facility codes */ #define LOG_KERN (0<<3) /* kernel messages */ #define LOG_USER (1<<3) /* random user-level messages */ #define LOG_MAIL (2<<3) /* mail system */ #define LOG_DAEMON (3<<3) /* system daemons */ #define LOG_AUTH (4<<3) /* security/authorization messages */ #define LOG_SYSLOG (5<<3) /* messages generated internally by syslogd */ #define LOG_LPR (6<<3) /* line printer subsystem */ #define LOG_NEWS (7<<3) /* network news subsystem */ #define LOG_UUCP (8<<3) /* UUCP subsystem */ #define LOG_CRON (9<<3) /* clock daemon */ #define LOG_AUTHPRIV (10<<3) /* security/authorization messages (private) */ #define LOG_FTP (11<<3) /* ftp daemon */ 示例代码: #include #include int main(void) { FILE *f; f = fopen("abc","r"); if(!f) syslog(LOG_ERR|LOG_USER,"test - %m\n"); }
上面的日志信息由系统自动给出,我们也可过滤日志信息。用到以下函数:
#includevoid closelog(void); void openlog(const char *ident, int logopt, int facility); int setlogmask(int maskpri); logopt参数的选项: logopt Parameter Description LOG_PID Includes the process identifier, a unique number allocated to each process by the system, in the messages. LOG_CONS Sends messages to the console if they can’t be logged. LOG_ODELAY Opens the log facility at first call to . LOG_NDELAY Opens the log facility immediately, rather than at first log. 示例代码: #include #include #include int main(void) { int logmask; openlog("logmask", LOG_PID|LOG_CONS, LOG_USER); /*日志信息会包含进程id。*/ syslog(LOG_INFO, "informative message, pid=%d", getpid()); syslog(LOG_DEBUG,"debug message, should appear"); /*记录该日志信息。*/ logmask = setlogmask(LOG_UPTO(LOG_NOTICE)); /*设置屏蔽低于NOTICE级别的日志信息。*/ syslog(LOG_DEBUG, "debug message, should not appear"); /*该日志信息被屏蔽,不记录。*/ }
不同安全级别的日志信息存放在/var/log目录下的哪个文件中是由/etc/syslog.conf文件控制的,下面是我系统中syslog.conf文件的内容:
# /etc/syslog.conf Configuration file for syslogd. # # For more information see syslog.conf(5) # manpage. # # First some standard logfiles. Log by facility. # 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 uucp.* /var/log/uucp.log # # Logging for the mail system. Split it up so that # it is easy to write scripts to parse these files. # mail.info -/var/log/mail.info mail.warn -/var/log/mail.warn mail.err /var/log/mail.err # Logging for INN news system # news.crit /var/log/news/news.crit news.err /var/log/news/news.err news.notice -/var/log/news/news.notice # # Some `catch-all' logfiles. # *.=debug;\ auth,authpriv.none;\ news.none;mail.none -/var/log/debug *.=info;*.=notice;*.=warn;\ auth,authpriv.none;\ cron,daemon.none;\ mail,news.none -/var/log/messages # # Emergencies are sent to everybody logged in. # *.emerg * # # I like to have messages displayed on the console, but only on a virtual # console I usually leave idle. # #daemon,mail.*;\ # news.=crit;news.=err;news.=notice;\ # *.=debug;*.=info;\ # *.=notice;*.=warn /dev/tty8 # The named pipe /dev/xconsole is for the `xconsole' utility. To use it, # you must invoke `xconsole' with the `-file' option: # # $ xconsole -file /dev/xconsole [...] # # NOTE: adjust the list below, or you'll go crazy if you have a reasonably # busy site.. # daemon.*;mail.*;\ news.crit;news.err;news.notice;\ *.=debug;*.=info;\ *.=notice;*.=warn |/dev/xconsole 实例代码:
|
结果:
|