ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
SyslogUdpHandler.php
Go to the documentation of this file.
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;
16 
23 {
24  protected $socket;
25  protected $ident;
26 
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 
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 
99  public function setSocket($socket)
100  {
101  $this->socket = $socket;
102  }
103 }
const DEBUG
Detailed debug information.
Definition: Logger.php:33
makeCommonSyslogHeader($severity)
Make common syslog header (see rfc5424)
A Handler for logging to a remote syslogd server.
catch(Exception $e) $message
setSocket($socket)
Inject your own socket, mainly used for testing.
__construct($host, $port=514, $facility=LOG_USER, $level=Logger::DEBUG, $bubble=true, $ident='php')