|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Monolog package. |
|
|
5 |
* |
|
|
6 |
* (c) Jordi Boggiano <j.boggiano@seld.be> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Monolog\Handler; |
|
|
13 |
|
|
|
14 |
use Monolog\Formatter\SimpleFormatter; |
|
|
15 |
use Monolog\Logger; |
|
|
16 |
|
|
|
17 |
/** |
|
|
18 |
* Logs to syslog service. |
|
|
19 |
* |
|
|
20 |
* usage example: |
|
|
21 |
* |
|
|
22 |
* $log = new Logger('application'); |
|
|
23 |
* $syslog = new SyslogHandler('myfacility', 'local6'); |
|
|
24 |
* $formatter = new LineFormatter("%channel%.%level_name%: %message% %extra%"); |
|
|
25 |
* $syslog->setFormatter($formatter); |
|
|
26 |
* $log->pushHandler($syslog); |
|
|
27 |
* |
|
|
28 |
* @author Sven Paulus <sven@karlsruhe.org> |
|
|
29 |
*/ |
|
|
30 |
class SyslogHandler extends AbstractProcessingHandler |
|
|
31 |
{ |
|
|
32 |
/** |
|
|
33 |
* Translates Monolog log levels to syslog log priorities. |
|
|
34 |
*/ |
|
|
35 |
private $logLevels = array( |
|
|
36 |
Logger::DEBUG => LOG_DEBUG, |
|
|
37 |
Logger::INFO => LOG_INFO, |
|
|
38 |
Logger::WARNING => LOG_WARNING, |
|
|
39 |
Logger::ERROR => LOG_ERR, |
|
|
40 |
Logger::CRITICAL => LOG_CRIT, |
|
|
41 |
Logger::ALERT => LOG_ALERT, |
|
|
42 |
); |
|
|
43 |
|
|
|
44 |
/** |
|
|
45 |
* List of valid log facility names. |
|
|
46 |
*/ |
|
|
47 |
private $facilities = array( |
|
|
48 |
'auth' => LOG_AUTH, |
|
|
49 |
'authpriv' => LOG_AUTHPRIV, |
|
|
50 |
'cron' => LOG_CRON, |
|
|
51 |
'daemon' => LOG_DAEMON, |
|
|
52 |
'kern' => LOG_KERN, |
|
|
53 |
'lpr' => LOG_LPR, |
|
|
54 |
'mail' => LOG_MAIL, |
|
|
55 |
'news' => LOG_NEWS, |
|
|
56 |
'syslog' => LOG_SYSLOG, |
|
|
57 |
'user' => LOG_USER, |
|
|
58 |
'uucp' => LOG_UUCP, |
|
|
59 |
); |
|
|
60 |
|
|
|
61 |
/** |
|
|
62 |
* @param string $ident |
|
|
63 |
* @param mixed $facility |
|
|
64 |
* @param integer $level The minimum logging level at which this handler will be triggered |
|
|
65 |
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not |
|
|
66 |
*/ |
|
|
67 |
public function __construct($ident, $facility = LOG_USER, $level = Logger::DEBUG, $bubble = true) |
|
|
68 |
{ |
|
|
69 |
parent::__construct($level, $bubble); |
|
|
70 |
|
|
|
71 |
if (false === strpos(PHP_OS, 'WIN')) { |
|
|
72 |
$this->facilities['local0'] = LOG_LOCAL0; |
|
|
73 |
$this->facilities['local1'] = LOG_LOCAL1; |
|
|
74 |
$this->facilities['local2'] = LOG_LOCAL2; |
|
|
75 |
$this->facilities['local3'] = LOG_LOCAL3; |
|
|
76 |
$this->facilities['local4'] = LOG_LOCAL4; |
|
|
77 |
$this->facilities['local5'] = LOG_LOCAL5; |
|
|
78 |
$this->facilities['local6'] = LOG_LOCAL6; |
|
|
79 |
$this->facilities['local7'] = LOG_LOCAL7; |
|
|
80 |
} |
|
|
81 |
|
|
|
82 |
// convert textual description of facility to syslog constant |
|
|
83 |
if (array_key_exists(strtolower($facility), $this->facilities)) { |
|
|
84 |
$facility = $this->facilities[strtolower($facility)]; |
|
|
85 |
} else if (!in_array($facility, array_values($this->facilities), true)) { |
|
|
86 |
throw new \UnexpectedValueException('Unknown facility value "'.$facility.'" given'); |
|
|
87 |
} |
|
|
88 |
|
|
|
89 |
if (!openlog($ident, LOG_PID, $facility)) { |
|
|
90 |
throw new \LogicException('Can\'t open syslog for ident "'.$ident.'" and facility "'.$facility.'"'); |
|
|
91 |
} |
|
|
92 |
} |
|
|
93 |
|
|
|
94 |
/** |
|
|
95 |
* {@inheritdoc} |
|
|
96 |
*/ |
|
|
97 |
public function close() |
|
|
98 |
{ |
|
|
99 |
closelog(); |
|
|
100 |
} |
|
|
101 |
|
|
|
102 |
/** |
|
|
103 |
* {@inheritdoc} |
|
|
104 |
*/ |
|
|
105 |
protected function write(array $record) |
|
|
106 |
{ |
|
|
107 |
syslog($this->logLevels[$record['level']], (string) $record['formatted']); |
|
|
108 |
} |
|
|
109 |
} |