1: <?php
  2:   3:   4:   5:   6:   7:   8:   9:  10:  11: 
 12: 
 13:  14:  15:  16:  17:  18:  19:  20:  21: 
 22: class Log_Writer_Syslog extends Log_Writer_Abstract
 23: {
 24:     const FACILITY_KERNEL           = 0;
 25:     const FACILITY_USER_LEVEL       = 1;
 26:     const FACILITY_MAIL_SYSTEM      = 2;
 27:     const FACILITY_SYSTEM_DEAMON    = 3;
 28:     const FACILITY_SECURITY_MSG     = 4;
 29:     const FACILITY_INTERNAL_SYSLOG  = 5;
 30:     const FACILITY_LINE_PRINTER     = 6;
 31:     const FACILITY_NETWK_NEWS       = 7;
 32:     const FACILITY_UUCP             = 8;
 33:     const FACILITY_CLOCK_DEAMON     = 9;
 34:     const FACILITY_AUTH_MSG         = 10;
 35:     const FACILITY_FTP_DEAMON       = 11;
 36:     const FACILITY_NTP              = 12;
 37:     const FACILITY_LOG_AUDIT        = 13;
 38:     const FACILITY_LOG_ALERT        = 14;
 39:     const FACILITY_LOCAL_USE0       = 16;
 40:     const FACILITY_LOCAL_USE1       = 17;
 41:     const FACILITY_LOCAL_USE2       = 18;
 42:     const FACILITY_LOCAL_USE3       = 19;
 43:     const FACILITY_LOCAL_USE4       = 20;
 44:     const FACILITY_LOCAL_USE5       = 21;
 45:     const FACILITY_LOCAL_USE6       = 22;
 46:     const FACILITY_LOCAL_USE7       = 23;
 47: 
 48:      49:  50:  51: 
 52:     private $syslogServer;
 53: 
 54:      55:  56:  57: 
 58:     private $port;
 59: 
 60:      61:  62:  63: 
 64:     private $facility   = 1;
 65: 
 66:      67:  68:  69: 
 70:     private $severity   = null;
 71: 
 72:      73:  74:  75: 
 76:     private $app        = '';
 77: 
 78:      79:  80:  81:  82: 
 83:     public function __construct($host,$port = 514)
 84:     {
 85:         $this->syslogServer = 'udp://'.$host;
 86:         $this->port = intval($port);
 87:     }
 88: 
 89:      90:  91:  92:  93: 
 94:     public function setFacility($facility)
 95:     {
 96:         $facility = intval($facility);
 97:         if($facility < 24)
 98:             $this->facility = $facility;
 99:         else
100:             throw new RangeException('Facility doit être compris entre 0 et 23');
101:     }
102: 
103:     104: 105: 106: 
107:     public function setSeverity($severity)
108:     {
109:         $severity = intval($severity);
110:         if($severity < 8)
111:             $this->severity = $severity;
112:         else
113:             throw new RangeException ('Severity doit être compris entre 0 et 7');
114:     }
115: 
116:     117: 118: 119: 
120:     public function setApp($app)
121:     {
122:         $this->app = $app;
123:     }
124: 
125:     126: 127: 128: 129: 
130:     protected function _write($message,$level)
131:     {
132:         $content        = $this->app.':'.$message;
133:         if(empty($this->severity))
134:             $this->severity = $level;
135:         
136:         $pri            = '<'.($this->facility*8+$this->severity).'>';
137:         $header         = $_SERVER['REMOTE_ADDR'].' ';
138:         $syslogMsg      = $pri.$header.$content;
139:         $socket         = fsockopen($this->syslogServer,$this->port);
140: 
141:         if($socket)
142:         {
143:             fwrite($socket,$syslogMsg);
144:             fclose($socket);
145:         }
146:         else
147:         {
148:             throw new Exception('Impossible de contacter le serveur Syslog');
149:         }
150:     }
151: }
152: ?>
153: