scratch – Blame information for rev 122
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
120 | office | 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\Logger; |
||
15 | use Monolog\Handler\SyslogUdp\UdpSocket; |
||
16 | |||
17 | /** |
||
18 | * A Handler for logging to a remote syslogd server. |
||
19 | * |
||
20 | * @author Jesper Skovgaard Nielsen <nulpunkt@gmail.com> |
||
21 | */ |
||
22 | class SyslogUdpHandler extends AbstractSyslogHandler |
||
23 | { |
||
24 | protected $socket; |
||
25 | protected $ident; |
||
26 | |||
27 | /** |
||
28 | * @param string $host |
||
29 | * @param int $port |
||
30 | * @param mixed $facility |
||
31 | * @param int $level The minimum logging level at which this handler will be triggered |
||
32 | * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not |
||
33 | * @param string $ident Program name or tag for each log message. |
||
34 | */ |
||
35 | public function __construct($host, $port = 514, $facility = LOG_USER, $level = Logger::DEBUG, $bubble = true, $ident = 'php') |
||
36 | { |
||
37 | parent::__construct($facility, $level, $bubble); |
||
38 | |||
39 | $this->ident = $ident; |
||
40 | |||
41 | $this->socket = new UdpSocket($host, $port ?: 514); |
||
42 | } |
||
43 | |||
44 | protected function write(array $record) |
||
45 | { |
||
46 | $lines = $this->splitMessageIntoLines($record['formatted']); |
||
47 | |||
48 | $header = $this->makeCommonSyslogHeader($this->logLevels[$record['level']]); |
||
49 | |||
50 | foreach ($lines as $line) { |
||
51 | $this->socket->write($line, $header); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | public function close() |
||
56 | { |
||
57 | $this->socket->close(); |
||
58 | } |
||
59 | |||
60 | private function splitMessageIntoLines($message) |
||
61 | { |
||
62 | if (is_array($message)) { |
||
63 | $message = implode("\n", $message); |
||
64 | } |
||
65 | |||
66 | return preg_split('/$\R?^/m', $message, -1, PREG_SPLIT_NO_EMPTY); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Make common syslog header (see rfc5424) |
||
71 | */ |
||
72 | protected function makeCommonSyslogHeader($severity) |
||
73 | { |
||
74 | $priority = $severity + $this->facility; |
||
75 | |||
76 | if (!$pid = getmypid()) { |
||
77 | $pid = '-'; |
||
78 | } |
||
79 | |||
80 | if (!$hostname = gethostname()) { |
||
81 | $hostname = '-'; |
||
82 | } |
||
83 | |||
84 | return "<$priority>1 " . |
||
85 | $this->getDateTime() . " " . |
||
86 | $hostname . " " . |
||
87 | $this->ident . " " . |
||
88 | $pid . " - - "; |
||
89 | } |
||
90 | |||
91 | protected function getDateTime() |
||
92 | { |
||
93 | return date(\DateTime::RFC3339); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Inject your own socket, mainly used for testing |
||
98 | */ |
||
99 | public function setSocket($socket) |
||
100 | { |
||
101 | $this->socket = $socket; |
||
102 | } |
||
103 | } |