zoukankan      html  css  js  c++  java
  • linux日志系统介绍 —— syslog(),openlog(),closelog()

    函数使用介绍


    这里面的三个函数openlog, syslog。closelog是一套系统日志写入接口。另外那个vsyslog和syslog功能一样,仅仅是參数格式不同。

            通常。syslog守护进程读取三种格式的记录消息。

    此守护进程在启动时读一个配置文件。一般来说,其文件名称为/etc/syslog.conf,该文件决定了不同种类的消息应送向何处。比如。紧急消息可被送向系统管理员(若已登录),并在控制台上显示。而警告消息则可记录到一个文件里。

    该机制提供了syslog函数。其调用格式例如以下

    #include <syslog.h>
    void openlog (char*ident,int option ,int facility);
    void syslog(int priority,char*format,……)
    void closelog();

           

            调用openlog是可选择的。

    假设不调用openlog,则在第一次调用syslog时,自己主动调用openlog。调用closelog也是可选择的,它仅仅是关闭被用于与syslog守护进程通信的描写叙述符。调用openlog使我们能够指定一个ident。以后, 此ident 将被加至每则记录消息中。ident 通常是程序的名称(比如 ,cron 。ine等)


    程序的使用方法演示样例代码例如以下:

    #include <syslog.h>
    int main(int argc, char **argv)
    {
       openlog("MyMsgMARK", LOG_CONS | LOG_PID, 0);
       syslog(LOG_DEBUG,
              "This is a syslog test message generated by program '%s'
    ",
              argv[0]);
       closelog();
       return0;
    }

    编译生成可运行程序后,运行一次程序将向/var/log/message文件加入一行信息例如以下:
    Feb 12 08:48:38 localhost MyMsgMARK[7085]: This is a syslog testmessage generated by program './a.out'

    openlog及closelog函数说明


    此函数原型例如以下:
    void openlog(const char *ident, int option, int facility);

    此函数用来打开一个到系统日志记录程序的连接,打开之后就能够用syslog或vsyslog函数向系统日志里加入信息了。而closelog函数就是用来关闭此连接的。

    openlog的參数:

    第一个參数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

     

    syslog函数及參数

    syslog函数用于把日志消息发给系统程序syslogd去记录。此函数原型是:
    void syslog(int priority, const char *format, ...);
    第一个參数是消息的紧急级别

    第二个參数是消息的格式,之后是格式相应的參数。就是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文件解读

    1: syslog.conf的介绍


        对于不同类型的Unix,标准UnixLog系统的设置,实际上除了一些关键词的不同,系统的syslog.conf格式是同样的。

    syslog採用可配置的、统一的系统登记程序,随时从系统各处接受log请求,然后依据/etc/syslog.conf中的预先设定把log信息写入相应文件里、邮寄给特定用户或者直接以消息的方式发往控制台。

    值得注意的是,为了防止入侵者改动、删除messages里的记录信息,能够採用用打印机记录或採用方式来挫败入侵者的企图。



    2: syslog.conf的格式


        能够參考man [5] syslog.conf。

    这里是对syslog.conf的简介。


        /etc/syslog.conf文件里的一项配置记录由“选项”(selector)和“动作”(action)两个部分组成,两者间用tab制表符进行分隔(使用空格间隔是无效的)。

    而“选项”又由一个或多个形如“类型.级别”格式的保留字段组合而成。各保留字段间用分号分隔。例如以下行所看到的:
            类型.级别 [;类型.级别] `TAB` 动作
            

        2.1 类型

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

            *       代表以上各种设备

        2.2 级别

            保留字段中的“级别”代表信息的重要性,能够是:
            emerg   紧急。处于Panic状态。

    通常应广播到全部用户;
            alert   告警,当前状态必须马上进行纠正。比如。系统数据库崩溃。
            crit    关键状态的警告。比如。硬件故障;
            err     其他错误;
            warning 警告。
            notice  注意;非错误状态的报告,但应特别处理。
            info    通报信息;
            debug   调试程序时的信息。

            none    通常调试程序时用,指示带有none级别的类型产生的信息无需送出。

    如*.debug;mail.none表示调试时除邮件信息外其他信息都送出。

        2.3 动作

            “动作”域指示信息发送的目的地。能够是:
            /filename   日志文件。

    由绝对路径指出的文件名称。此文件必须事先建立;
            @host       远程主机; @符号后面能够是ip,也能够是域名,默认在/etc/hosts文件下loghost这个别名已经指定给了本机。


            user1,user2 指定用户。假设指定用户已登录。那么他们将收到信息;
            *           全部用户。

    全部已登录的用户都将收到信息。


    3: 详细实例

        我们来看看/etc/syslog.conf文件里的实例:
        ……
        *.err;kern.debug;daemon.notice;mail.crit    [TAB]   /var/adm/messages
        ……

        这行中的“action”就是我们常关心的那个/var/adm/messages文件。输出到它的信息源头“selector”是:
        *.err - 全部的一般错误信息。
        kern.debug - 核心产生的调试信息;
        daemon.notice - 守护进程的注意信息。

        mail.crit - 邮件系统的关键警告信息


                                                                                                        — — 以上全部内容均来自互联网


  • 相关阅读:
    函数、包和错误处理
    程序流程控制
    poj 2515 Birthday Cake
    poj 2094 多项式求和。
    hdu 3625 第一类striling 数
    hdu 4372 第一类stirling数的应用/。。。好题
    poj 1845 Sumdiv
    hdu 3641 Treasure Hunting 强大的二分
    poj 3335 /poj 3130/ poj 1474 半平面交 判断核是否存在 / poj1279 半平面交 求核的面积
    hdu 2841 Visible Trees
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10048760.html
Copyright © 2011-2022 走看看