Show
Ignore:
Timestamp:
08/28/08 23:02:20 (4 months ago)
Author:
nowelium
Message:
 
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/php/misc/Logging/appender/SyslogAppender.php

    r18405 r18406  
    11<?php 
     2 
     3interface SyslogFacility { 
     4    // kernel messages 
     5    const LOG_KERN = 0; 
     6    // random user-level messages(1 << 3) 
     7    const LOG_USER = 8; 
     8    // mail system(2<<3) 
     9    const LOG_MAIL = 16;  
     10    // system daemons(3<<3) 
     11    const LOG_DAEMON = 24; 
     12    // security/authorization(4<<3) 
     13    const LOG_AUTH = 32; 
     14    // internal syslogd use(5<<3) 
     15    const LOG_SYSLOG = 40; 
     16    // line printer subsystem(6<<3) 
     17    const LOG_LPR = 48; 
     18    // network news subsystem(7<<3) 
     19    const LOG_NEWS = 56; 
     20    // UUCP subsystem(8<<3) 
     21    const LOG_UUCP = 64; 
     22    // clock daemon(15<<3) 
     23    const LOG_CRON = 120; 
     24     
     25    // reserved for local use(16<<3) 
     26    const LOG_LOCAL0 = 128; 
     27    // reserved for local use(17<<3) 
     28    const LOG_LOCAL1 = 136; 
     29    // reserved for local use(18<<3) 
     30    const LOG_LOCAL2 = 144; 
     31    // reserved for local use(19<<3) 
     32    const LOG_LOCAL3 = 152; 
     33    // reserved for local use(20<<3) 
     34    const LOG_LOCAL4 = 160; 
     35    // reserved for local use(21<<3) 
     36    const LOG_LOCAL5 = 168; 
     37    // reserved for local use(22<<3) 
     38    const LOG_LOCAL6 = 176; 
     39    // reserved for local use(23<<3) 
     40    const LOG_LOCAL7 = 184; 
     41     
     42    // mask to extract facility 
     43    const LOG_FACMASK = 0x03F8; 
     44} 
     45 
     46interface SyslogPriority { 
     47    // system is unusable 
     48    const LOG_EMERG = 0; 
     49    // action must be taken immediately 
     50    const LOG_ALERT = 1; 
     51    // critical conditions 
     52    const LOG_CRIT = 2; 
     53    // error conditions 
     54    const LOG_ERR = 3; 
     55    // warning conditions 
     56    const LOG_WARNING = 4; 
     57    // normal but significant condition 
     58    const LOG_NOTICE = 5; 
     59    // informational 
     60    const LOG_INFO = 6; 
     61    // debug-level messages 
     62    const LOG_DEBUG = 7; 
     63    // mask to extract priority 
     64    const LOG_PRIMASK = 0x0007; 
     65} 
    266 
    367class SyslogAppender implements Appender { 
    468 
    5     const SYSLOG_PORT = 514; 
     69    private $ident; 
     70    private $opt; 
     71    private $facility; 
    672 
    7     private $host = 'localhost'; 
    8     private $layout; 
    9     public function __construct($host, LogLayout $layout = null){ 
    10         if($layout === null){ 
    11             $this->layout = new SimpleLayout; 
    12         } else { 
    13             $this->layout = $layout; 
    14         } 
     73    private $ip = '127.0.0.1'; 
     74    private $port = 514; 
     75 
     76    public function __construct($ident, $opt, $facility){ 
     77        $this->ident = $ident; 
     78        $this->opt = $opt; 
     79        $this->facility = $facility; 
     80    } 
     81 
     82    public function setIp($ip){ 
     83        $this->ip = $ip; 
     84    } 
     85 
     86    public function setPort($port){ 
     87        $this->port = $port; 
    1588    } 
    1689 
    1790    public function append(LoggingEvent $event){ 
    18         echo $this->layout->format($event); 
     91        $level = $event->getLevel(); 
     92        $priority = SyslogPriority::LOG_PRIMASK; 
     93        switch($level->toInt()){ 
     94        case LogLevel::ALL_INT: 
     95            $priority = SyslogPriority::LOG_EMERG; 
     96            break; 
     97        case LogLevel::DEBUG_INT: 
     98            $priority = SyslogPriority::LOG_DEBUG; 
     99            break; 
     100        case LogLevel::INFO_INT: 
     101            $priority = SyslogPriority::LOG_DEBUG; 
     102            break; 
     103        case LogLevel::WARN_INT: 
     104            $priority = SyslogPriority::LOG_WARNING; 
     105            break; 
     106        case LogLevel::ERROR_INT: 
     107            $priority = SyslogPriority::LOG_ERR; 
     108            break; 
     109        case LogLevel::FATAL_INT: 
     110            $priority = SyslogPriority::LOG_CRIT; 
     111            break; 
     112        case LogLevel::OFF_INT: 
     113        default: 
     114            return; 
     115        } 
     116        $this->write($event->getMessage(), $priority); 
    19117    } 
     118 
     119    public function write($message, $priority){ 
     120        $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); 
     121        socket_set_nonblock($socket); 
     122        if(socket_connect($socket, $this->ip, $this->port)){ 
     123            $data = ''; 
     124            $data .= '<' . self::makePriority($this->facility, $priority) . '>'; 
     125            $data .=  $this->ident . ': ' . $message; 
     126            socket_write($socket, $data, strlen($data)); 
     127        } 
     128        socket_close($socket); 
     129    } 
     130 
     131    private static function makePriority($facility, $priority){ 
     132        return ($facility & SyslogFacility::LOG_FACMASK) | $priority; 
     133    } 
     134 
    20135}