linux日志系统

  • syslog()
  • openlog()
  • closelog()

void openlog(const char *ident, int option, int facility);
此函数用来打开一个到系统日志记录程序的连接,打开之后就可以用syslog或vsyslog函数向系统日志里添加信息了。而closelog函数就是用来关闭此连接的。
第一个参数ident将是一个标记,ident所表示的字符串将固定地加在每行日志的前面以标识这个日志,通常就写成当前程序的名称以作标记。
第二个参数option是下列值取与运算的结果:LOG_CONS,LOG_NDELAY, LOG_NOWAIT, LOG_ODELAY, LOG_PERROR,LOG_PID,各值意义请参考man openlog手册:

LOG_CONS 
   Writedirectly to system console if there is an error while sending tosystem logger. 
LOG_NDELAY 
   Openthe connection immediately (normally, the connection is opened whenthe first message is logged). 
LOG_NOWAIT 
   Don’t wait for childprocesses that may have been created while logging themessage.  (The GNU C library does not createa child process, so this option has no effect onLinux.) 
LOG_ODELAY 
   The converseof LOG_NDELAY; opening of the connection is delayed until syslog()is called.  (This is the default,  and need not be specified.) 
LOG_PERROR 
   (Notin SUSv3.) Print to stderr as well. 
LOG_PID 
    IncludePID with each message. 

第三个参数facility指明记录日志的程序的类型。

    The facility argument is used to specify what type ofprogram  is logging  the  message. 
    This  lets the configuration file specify thatmessages from different facilities will be 
    handled differently. 
    
    LOG_AUTH      security/authorization messages (DEPRECATED Use LOG_AUTHPRIVinstead)
    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 generage 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, …);
syslog函数用于把日志消息发给系统程序syslogd去记录
第一个参数是消息的紧急级别
第二个参数是消息的格式,之后是格式对应的参数。就是printf函数一样使用。
如果我们的程序要使用系统日志功能,只需要在程序启动时使用openlog函数来连接syslogd程序,后面随时用syslog函数写日志就行了。

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
    
    The function setlogmask(3) can be used to restrict logging tospecified levels only. 

NOTES 
    The  argument ident  in  the call  of openlog() is probably storedas-is.  Thus, if the 
    string it points to is changed, syslog() may start prepending thechanged string, and  if 
    the  string it points to ceases to exist, theresults are undefined.  Most portable is to 
    use a string constant. 
     
    Never pass a string with user-supplied data as a format, use thefollowing instead: 
     
        syslog(priority, "%s", string); 

SEE ALSO 
  logger(1), setlogmask(3), syslog.conf(5), syslogd(8) 

syslog.conf文件

对于不同类型的Unix,标准UnixLog系统的设置,实际上除了一些关键词的不同,系统的syslog.conf格式是相同的。syslog采用可配置的、统一的系统登记程序,随时从系统各处接受log请求,然后根据/etc/syslog.conf中的预先设定把log信息写入相应文件中、邮寄给特定用户或者直接以消息的方式发往控制台。值得注意的是,为了防止入侵者修改、删除messages里的记录信息,可以采用用打印机记录或采用方式来挫败入侵者的企图。

格式

可以参考man [5] syslog.conf。这里是对syslog.conf的简单介绍。
/etc/syslog.conf文件中的一项配置记录由“选项”(selector)和“动作”(action)两个部分组成,两者间用tab制表符进行分隔(使用空格间隔是无效的)。而“选项”又由一个或多个形如“类型.级别”格式的保留字段组合而成,各保留字段间用分号分隔。如下行所示:
类型.级别 [;类型.级别] ’TAB‘ 动作

类型
    保留字段中的“类型”代表信息产生的源头,可以是: 
    auth    认证系统,即询问用户名和口令 
    cron    系统定时系统执行定时任务时发出的信息 
    daemon  某些系统的守护程序的syslog,如由in.ftpd产生的log 
    kern    内核的syslog信息 
    lpr     打印机的syslog信息 
    mail    邮件系统的syslog信息 
    mark    定时发送消息的时标程序 
    news    新闻系统的syslog信息 
    user    本地用户应用程序的syslog信息 
    uucp    uucp子系统的syslog信息 
    local0..7 种本地类型的syslog信息,这些信息可以又用户来定义 
    *       代表以上各种设备

级别
    保留字段中的“级别”代表信息的重要性,可以是: 
    emerg   紧急,处于Panic状态。通常应广播到所有用户; 
    alert   告警,当前状态必须立即进行纠正。例如,系统数据库崩溃; 
    crit    关键状态的警告。例如,硬件故障; 
    err     其它错误; 
    warning 警告; 
    notice  注意;非错误状态的报告,但应特别处理; 
    info    通报信息; 
    debug   调试程序时的信息; 
    none    通常调试程序时用,指示带有none级别的类型产生的信息无需送出。如*.debug;mail.none表示调试时除邮件信息外其它信息都送出。

动作
    “动作”域指示信息发送的目的地。可以是: 
    /filename   日志文件。由绝对路径指出的文件名,此文件必须事先建立; 
    @host       远程主机; @符号后面可以是ip,也可以是域名,默认在/etc/hosts文件下loghost这个别名已经指定给了本机。 
    user1,user2 指定用户。如果指定用户已登录,那么他们将收到信息; 
    *           所有用户。所有已登录的用户都将收到信息。 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值