自定义日志模块(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");
09-26 14:49