syslog的启用

来源:互联网 发布:mac文件打包怎么做 编辑:程序博客网 时间:2024/06/05 02:08
If syslogd applet compiled with FEATURE_SYSLOGD_CFG=y, then it supports restricted syslog.conf.
The config resembles rsyslog.conf in RULES part:


LINE = DELIM [RULE | COMMENT]
COMMENT = #.*
DELIM = SPACE TAB
RULE = SELECTOR [;SELECTOR]* DELIM* ACTION DELIM*
SELECTOR = FACILITY [,FACILITY]* .[[!]=] PRIORITY
FACILITY = * | kern | user ... (see syslog.h)
PRIORITY = * | emerg | alert ... (see syslog.h)
ACTION = FILE


"mark" facility is NOT supported.
"none" priority is supported.
In FACILITY and PRIORITY "*" stands for "any".
FILE is a regular file or tty device.


Here is an example:


#syslog.conf
kern,user.*                                 /var/log/messages#all messages of kern and user facilities
kern.!err                                   /var/log/critical#all messages of kern facility with priorities lower than err (warn, notice ...)
*.*;auth,authpriv.none                      /var/log/noauth#all messages except ones with auth and authpriv facilities
kern,user.*;kern.!=notice;*.err;syslog.none /var/log/OMG#some whicked rule just as an example =)
*.*                                         /dev/null#this prevents from logging to default log file (-O FILE or /var/log/messages)


Even in the case of match with some rule another rules will be tried too.

If there was no match with any of the rules, logging to default log file or shared memory will be performed.


1. 查看/usr/include/sys/syslog.h和/etc/syslog.conf,理解syslog日志的配置方法

这个头文件用来定义应用日志名(facility)的代码和优先级(priority)的代码。facility可以简单的认为,用来区分不同的应用;priority用来区分不同的优先级。在syslog的配置文件syslog.conf中,开发人员可以用facility.priority来标示一个日志项,即为某个facility的某个级别的日志专门制定一个文件来记录。在笔者的实验中,facility只能使用在syslog.h中已经定义的那些facility name.   syslog为用户保留了若干可用的facility name ,包含local0,local1...local7等等。


2. 编辑/etc/syslog.conf, 为自己的应用定制日志项

选择syslog.conf中未经试用的facility,作为自己的应用的日志名。笔者在syslog.conf中添加了下面一条记录,该条记录表示把local1这个应用日志名(facility)对应的日志,写入右方的日志文件中。

local1.*                                        /root/tinyhttpd-0.1.0/tinyhttpd.log
.*表示,该应用的所有级别的日志均写入tinyhttpd.log。假如想要为不同级别优先级指定不同的日志项,则需要如下配置:
#此配置未经验证,不对之处请指正
local1.=info          /root/tinyhttpd-0.1.0/tinyhttpd.log1
local1.=err          /root/tinyhttpd-0.1.0/tinyhttpd.log


3. 在自己的应用程序中记录日志

包含头文件 #include在需要打印日志的地方,调用syslog函数
       syslog(LOG_LOCAL1|LOG_INFO,“what you what print in log %s”,  some string var);
此句表示要记录设备(或者应用日志名为)local1的info级别的日志。这里要注意的是,在syslog.conf配置文件中使用的是facility name,而程序中打印日志时使用的是facility code的宏定义。
另外,可以在调用syslog之前的主程序中指定日志打印的选项,具体选项含义请百度。
openlog( "tinyhttpd", LOG_PID|LOG_NOWAIT|LOG_NDELAY|LOG_CONS, LOG_LOCAL1 );