zoukankan      html  css  js  c++  java
  • perl的Sys::Syslog模块(openlog,syslog,closelog函数,setlogsock)-自定义日志

    perl的Sys::Syslog模块(openlog,syslog,closelog函数,setlogsock)-自定义日志

    http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=30572348&id=5603558

    自定义日志模块(Sys::Syslog)

    1、语法:
    use Sys::Syslog;                          # all except setlogsock(), or:
    use Sys::Syslog qw(:DEFAULT setlogsock);  # default set, plus setlogsock()增强版
    use Sys::Syslog qw(:standard :macros);    # standard functions, plus macros

    2、setlogsock使用
         setlogsock($sock_type)
         setlogsock($sock_type, $stream_location)
               ·   "native" - use the native C functions from your syslog(3) library (added in "Sys::Syslog" 0.15).
               ·   "eventlog" - send messages to the Win32 events logger (Win32 only; added in "Sys::Syslog" 0.19).
               ·   "tcp" - connect to a TCP socket, on the "syslog/tcp" or "syslogng/tcp" service.
               ·   "udp" - connect to a UDP socket, on the "syslog/udp" service.
               ·   "inet" - connect to an INET socket, either TCP or UDP, tried in that order.
               ·   "unix" - connect to a UNIX domain socket (in some systems a character special device).  The name of that socket is
                   the second parameter or, if you omit the second parameter, the value returned by the "_PATH_LOG" macro (if your
                   system defines it), or /dev/log or /dev/conslog, whatever is writable.
               ·   "stream" - connect to the stream indicated by the pathname provided as the optional second parameter, or, if
                   omitted, to /dev/conslog.  For example Solaris and IRIX system may prefer "stream" instead of "unix".
               ·   "pipe" - connect to the named pipe indicated by the pathname provided as the optional second parameter, or, if
                   omitted, to the value returned by the "_PATH_LOG" macro (if your system defines it), or /dev/log (added in
                   "Sys::Syslog" 0.21).
               ·   "console" - send messages directly to the console, as for the "cons" option of "openlog()". 

    3、函数:
    3.1openlog函数
      openlog($ident, $logopt, $facility;)  定义日志内容     
              $ident //每一个日志信息前均会附加$ident
              $logopt //选项
              $facility // 类型

      logopt选项:
            ·   "cons" - This option is ignored, since the failover mechanism will drop down to the console automatically if all other media fail.
            ·   "ndelay" - Open the connection immediately (normally, the connection is opened when the first message is logged).
            ·   "nofatal" - When set to true, "openlog()" and "syslog()" will only emit warnings instead of dying if theconnection to the syslog can't be established.
            ·   "nowait" - Don't wait for child processes that may have been created while logging the message.  (The GNU C library does not create a child process, so this option has no effect on Linux.)
            ·   "perror" - Write the message to standard error output as well to the system log.
            ·   "pid" - Include PID with each message.

      facility类型:
          auth            用户认证
          authpriv        有特权的用户认证
          cron             cron守护进程
          daemon          各种系统守护进程
          ftp              ftp守护进程
          kern             内核消息
          local0-local7   保留用于本地用法
          lpr               打印机
          mail            邮件
          news            新闻
          syslog          内部syslog
          uucp            uucp系统
          user            各种用户程序来的消息

    3.2syslog函数
      syslog($priority, $message)
      syslog($priority, $format, @args)
      syslog可定义优先级
      $priority can specify a level, or a level and a facility.

      *先用openlog()定义格式,syslog()定义内容 //use "openlog()" before using "syslog()"

    3.3closelog()
            Closes the log file and returns true on success.


    具体例子:
    setlogsock(["unix", "udp", "native", "tcp"]);  #通过unix,udp,tcp socket连接LOG  
    my $identity = "czw-syslog";
    my @options = ('cons','pid');
    my $facility = "local5";
    openlog($identity,@options,$facility);  #定义了格式等  

    syslog('info', "message");

    -----------------------------

    $facility="local0";
    openlog($identity,@options,$facility);
    #syslog 优先级:  emerg alert crit err warning notice info debug
    syslog('err',$message);

    ===================================

  • 相关阅读:
    awk统计命令(求和、求平均、求最大值、求最小值)(转)
    高性能跨平台网络IO(Reactor、epoll、iocp)总结
    进程通信和同步(转)
    C++11原子操作与无锁编程(转)
    在线代码编译运行工具
    linux ps 命令的查看
    转: linux sed 命令的使用
    转:利用Eclipse CDT 阅读C/C++代码
    转:Raft一致性选举算法的ppt与视频
    转:ffmpeg time_base详解
  • 原文地址:https://www.cnblogs.com/lsgxeva/p/9287841.html
Copyright © 2011-2022 走看看