1: <?php
2:
3: /**
4: * Pry Framework
5: *
6: * LICENSE
7: *
8: * This source file is subject to the new BSD license that is bundled
9: * with this package in the file LICENSE.txt.
10: *
11: */
12:
13: namespace Pry\Log\Writer;
14:
15: /**
16: * Class d'écriture de log sur un serveur Syslog
17: *
18: * @package Log
19: * @subpackage Log_Writer
20: * @version 1.0.0
21: * @author Olivier ROGER <oroger.fr>
22: *
23: */
24: class Syslog extends WriterAbstract
25: {
26:
27: const FACILITY_KERNEL = 0;
28: const FACILITY_USER_LEVEL = 1;
29: const FACILITY_MAIL_SYSTEM = 2;
30: const FACILITY_SYSTEM_DEAMON = 3;
31: const FACILITY_SECURITY_MSG = 4;
32: const FACILITY_INTERNAL_SYSLOG = 5;
33: const FACILITY_LINE_PRINTER = 6;
34: const FACILITY_NETWK_NEWS = 7;
35: const FACILITY_UUCP = 8;
36: const FACILITY_CLOCK_DEAMON = 9;
37: const FACILITY_AUTH_MSG = 10;
38: const FACILITY_FTP_DEAMON = 11;
39: const FACILITY_NTP = 12;
40: const FACILITY_LOG_AUDIT = 13;
41: const FACILITY_LOG_ALERT = 14;
42: const FACILITY_LOCAL_USE0 = 16;
43: const FACILITY_LOCAL_USE1 = 17;
44: const FACILITY_LOCAL_USE2 = 18;
45: const FACILITY_LOCAL_USE3 = 19;
46: const FACILITY_LOCAL_USE4 = 20;
47: const FACILITY_LOCAL_USE5 = 21;
48: const FACILITY_LOCAL_USE6 = 22;
49: const FACILITY_LOCAL_USE7 = 23;
50:
51: /**
52: * Serveur syslog
53: * @var string
54: */
55: private $syslogServer;
56:
57: /**
58: * Port de communication
59: * @var int
60: */
61: private $port;
62:
63: /**
64: * Facility
65: * @var int
66: */
67: private $facility = 1;
68:
69: /**
70: * Niveau de sévérité
71: * @var int
72: */
73: private $severity = null;
74:
75: /**
76: * Application émettant le message
77: * @var string
78: */
79: private $app = '';
80:
81: /**
82: * Constructeur
83: * @param string $host IP du serveur
84: * @param string $port Port de communication. defaut 514
85: */
86: public function __construct($host, $port = 514)
87: {
88: $this->syslogServer = 'udp://' . $host;
89: $this->port = intval($port);
90: }
91:
92: /**
93: * Défini le niveau de "Facility" 0- 23
94: * @see http://fr.wikipedia.org/wiki/Syslog
95: * @param int $facility
96: */
97: public function setFacility($facility)
98: {
99: $facility = intval($facility);
100: if ($facility < 24)
101: $this->facility = $facility;
102: else
103: throw new \RangeException('Facility doit être compris entre 0 et 23');
104: }
105:
106: /**
107: * Défini la sévérité du message
108: * @param int $severity
109: */
110: public function setSeverity($severity)
111: {
112: $severity = intval($severity);
113: if ($severity < 8)
114: $this->severity = $severity;
115: else
116: throw new \RangeException('Severity doit être compris entre 0 et 7');
117: }
118:
119: /**
120: * Défini l'application envoyant le message
121: * @param string $app
122: */
123: public function setApp($app)
124: {
125: $this->app = $app;
126: }
127:
128: /**
129: * Ecriture du message vers le serveur syslog
130: * @param string $message
131: * @param int $level Niveau d'importance
132: */
133: protected function _write($message, $level)
134: {
135: $content = $this->app . ':' . $message;
136: if (empty($this->severity))
137: $this->severity = $level;
138:
139: $pri = '<' . ($this->facility * 8 + $this->severity) . '>';
140: $header = $_SERVER['REMOTE_ADDR'] . ' ';
141: $syslogMsg = $pri . $header . $content;
142: $socket = fsockopen($this->syslogServer, $this->port);
143:
144: if ($socket)
145: {
146: fwrite($socket, $syslogMsg);
147: fclose($socket);
148: }
149: else
150: {
151: throw new \Exception('Impossible de contacter le serveur Syslog');
152: }
153: }
154:
155: }