Plop
A simple logging library for PHP
SysLog.php
1 <?php
2 /*
3  This file is part of Plop, a simple logging library for PHP.
4 
5  Copyright © 2010-2014 François Poirotte
6 
7  Plop is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Plop is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Plop. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 namespace Plop\Handler;
22 
29 {
31  const LOG_FORMAT_STRING = "<%d>%s\000";
32 
34  const DEFAULT_ADDRESS = 'udg:///dev/log';
35 
37  protected static $priorityNames = array(
38  'alert' => LOG_ALERT,
39  'crit' => LOG_CRIT,
40  'critical' => LOG_CRIT,
41  'debug' => LOG_DEBUG,
42  'emerg' => LOG_EMERG,
43  'err' => LOG_ERR,
44  'error' => LOG_ERR, // Deprecated
45  'info' => LOG_INFO,
46  'notice' => LOG_NOTICE,
47  'panic' => LOG_EMERG, // Deprecated
48  'warn' => LOG_WARNING, // Deprecated
49  'warning' => LOG_WARNING,
50  );
51 
53  protected static $facilityNames = array(
54  'auth' => LOG_AUTH,
55  'authpriv' => LOG_AUTHPRIV,
56  'cron' => LOG_CRON,
57  'daemon' => LOG_DAEMON,
58  'kern' => LOG_KERN,
59  'lpr' => LOG_LPR,
60  'mail' => LOG_MAIL,
61  'news' => LOG_NEWS,
62  'syslog' => LOG_SYSLOG,
63  'user' => LOG_USER,
64  'uucp' => LOG_UUCP,
65  );
66 
76  protected static $priorityMap = array(
77  'DEBUG' => 'debug',
78  'INFO' => 'info',
79  'NOTICE' => 'notice',
80  'WARNING' => 'warning',
81  'ERROR' => 'error',
82  'CRITICAL' => 'critical',
83  'ALERT' => 'alert',
84  'EMERGENCY' => 'emergency',
85  );
86 
88  protected $address;
89 
91  protected $facility;
92 
94  protected $socket;
95 
115  public function __construct(
116  $address = self::DEFAULT_ADDRESS,
117  $facility = LOG_USER
118  ) {
119  if (defined('LOG_LOCAL0') && !isset(static::$facilityNames['local0'])) {
120  static::$facilityNames += array(
121  'local0' => LOG_LOCAL0,
122  'local1' => LOG_LOCAL1,
123  'local2' => LOG_LOCAL2,
124  'local3' => LOG_LOCAL3,
125  'local4' => LOG_LOCAL4,
126  'local5' => LOG_LOCAL5,
127  'local6' => LOG_LOCAL6,
128  'local7' => LOG_LOCAL7,
129  );
130  }
131 
132  parent::__construct();
133  $this->address = $address;
134  $this->facility = $facility;
135  $this->socket = $this->makeSocket();
136  if ($this->socket === false) {
137  throw new \Plop\Exception('Unable to connect to the syslog');
138  }
139  }
140 
142  public function __destruct()
143  {
144  $this->close();
145  }
146 
156  protected function makeSocket()
157  {
158  return stream_socket_client($this->address);
159  }
160 
174  protected function encodePriority($facility, $priority)
175  {
176  if (is_string($facility)) {
177  $facility = static::$facilityNames[$facility];
178  }
179  if (is_string($priority)) {
180  $priority = static::$priorityNames[$priority];
181  }
182  return ($facility << 3) | $priority;
183  }
184 
191  protected function close()
192  {
193  if ($this->socket !== false) {
194  fclose($this->socket);
195  $this->socket = false;
196  }
197  }
198 
210  protected function mapPriority($levelName)
211  {
212  if (isset(static::$priorityMap[$levelName])) {
213  return static::$priorityMap[$levelName];
214  }
215  return "warning";
216  }
217 
219  protected function emit(\Plop\RecordInterface $record)
220  {
221  $msg = $this->format($record);
222  $msg = sprintf(
223  static::LOG_FORMAT_STRING,
224  $this->encodePriority(
225  $this->facility,
226  $this->mapPriority($record['levelname'])
227  ),
228  $msg
229  );
230 
231  for ($written = 0; $written < strlen($msg); $written += $fwrite) {
232  $fwrite = @fwrite($this->socket, substr($msg, $written));
233  if ($fwrite === false) {
234  $this->handleError(
235  $record,
236  new \Plop\Exception('Connection lost')
237  );
238  break;
239  }
240  }
241  }
242 }
emit(\Plop\RecordInterface $record)
Definition: SysLog.php:219
$facility
The facility to use when logging the messages.
Definition: SysLog.php:91
An abstract class that can be used as a base to create new log records handlers.
Interface for a log record.
__construct($address=self::DEFAULT_ADDRESS, $facility=LOG_USER)
Definition: SysLog.php:115
format(\Plop\RecordInterface $record)
mapPriority($levelName)
Definition: SysLog.php:210
static $facilityNames
Mapping between the facility names and their associated constant.
Definition: SysLog.php:53
handleError(\Plop\RecordInterface $record,\Exception $exception)
$socket
The socket that will be used to send the logs.
Definition: SysLog.php:94
static $priorityNames
Mapping between Plop&#39;s log levels and the syslog ones.
Definition: SysLog.php:37
const DEFAULT_ADDRESS
Default address for the system logs.
Definition: SysLog.php:34
static $priorityMap
Definition: SysLog.php:76
__destruct()
Free the resources used by this handler.
Definition: SysLog.php:142
This exception is thrown by Plop whenever a problem is detected.
Definition: Exception.php:30
encodePriority($facility, $priority)
Definition: SysLog.php:174
$address
Address of the syslog where the logs will be sent.
Definition: SysLog.php:88
const LOG_FORMAT_STRING
Specific format string used for system logs.
Definition: SysLog.php:31
An handler that sends log messages to the system logs (syslog).
Definition: SysLog.php:28