php發送log至syslog server

範例

<?php
function BSD_Syslog($msg,$severity=7, $program="php-test",$remote_ip='127.0.0.1',$remote_port=514){
  date_default_timezone_set('Asia/Taipei');
  $facility=5;
  $severity=7;
  $PRI = $facility*8+$severity;
  $HEADER=date('M d H:i:s ') . $program;
  $syslog_message = "<{$PRI}>{$HEADER} {$msg}";
  $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  socket_sendto($sock, $syslog_message, strlen($syslog_message), 0, $remote_ip, (int)$remote_port);
  socket_close($sock);
}
BSD_Syslog("test: ".rand()%10000);

其中facility的值如下:

         Code

          0             kernel messages
          1             user-level messages
          2             mail system
          3             system daemons
          4             security/authorization messages
          5             messages generated internally by syslogd
          6             line printer subsystem
          7             network news subsystem
          8             UUCP subsystem
          9             clock daemon
         10             security/authorization messages
         11             FTP daemon
         12             NTP subsystem
         13             log audit
         14             log alert
         15             clock daemon (note 2)
         16             local use 0  (local0)
         17             local use 1  (local1)
         18             local use 2  (local2)
         19             local use 3  (local3)
         20             local use 4  (local4)
         21             local use 5  (local5)
         22             local use 6  (local6)
         23             local use 7  (local7)

severity的值如下:


         Code
          0       Emergency: system is unusable
          1       Alert: action must be taken immediately
          2       Critical: critical conditions
          3       Error: error conditions
          4       Warning: warning conditions
          5       Notice: normal but significant condition
          6       Informational: informational messages
          7       Debug: debug-level messages