[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / Log / Writer / Syslog.php
blob0e357136452449ff4f0ad7a9ebfb1c40259bc511
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
15 * @category Zend
16 * @package Zend_Log
17 * @subpackage Writer
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id$
23 /** Zend_Log_Writer_Abstract */
24 require_once 'Zend/Log/Writer/Abstract.php';
26 /**
27 * Writes log messages to syslog
29 * @category Zend
30 * @package Zend_Log
31 * @subpackage Writer
32 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
33 * @license http://framework.zend.com/license/new-bsd New BSD License
35 class Zend_Log_Writer_Syslog extends Zend_Log_Writer_Abstract
37 /**
38 * Maps Zend_Log priorities to PHP's syslog priorities
39 * @var array
41 protected $_priorities = array(
42 Zend_Log::EMERG => LOG_EMERG,
43 Zend_Log::ALERT => LOG_ALERT,
44 Zend_Log::CRIT => LOG_CRIT,
45 Zend_Log::ERR => LOG_ERR,
46 Zend_Log::WARN => LOG_WARNING,
47 Zend_Log::NOTICE => LOG_NOTICE,
48 Zend_Log::INFO => LOG_INFO,
49 Zend_Log::DEBUG => LOG_DEBUG,
52 /**
53 * The default log priority - for unmapped custom priorities
54 * @var string
56 protected $_defaultPriority = LOG_NOTICE;
58 /**
59 * Last application name set by a syslog-writer instance
60 * @var string
62 protected static $_lastApplication;
64 /**
65 * Last facility name set by a syslog-writer instance
66 * @var string
68 protected static $_lastFacility;
70 /**
71 * Application name used by this syslog-writer instance
72 * @var string
74 protected $_application = 'Zend_Log';
76 /**
77 * Facility used by this syslog-writer instance
78 * @var int
80 protected $_facility = LOG_USER;
82 /**
83 * _validFacilities
85 * @var array
87 protected $_validFacilities = array();
89 /**
90 * Class constructor
92 * @param array $options Array of options; may include "application" and "facility" keys
93 * @return void
95 public function __construct(array $params = array())
97 if (isset($params['application'])) {
98 $this->_application = $params['application'];
101 $runInitializeSyslog = true;
102 if (isset($params['facility'])) {
103 $this->_facility = $this->setFacility($params['facility']);
104 $runInitializeSyslog = false;
107 if ($runInitializeSyslog) {
108 $this->_initializeSyslog();
113 * Create a new instance of Zend_Log_Writer_Syslog
115 * @param array|Zend_Config $config
116 * @return Zend_Log_Writer_Syslog
117 * @throws Zend_Log_Exception
119 static public function factory($config)
121 return new self(self::_parseConfig($config));
125 * Initialize values facilities
127 * @return void
129 protected function _initializeValidFacilities()
131 $constants = array(
132 'LOG_AUTH',
133 'LOG_AUTHPRIV',
134 'LOG_CRON',
135 'LOG_DAEMON',
136 'LOG_KERN',
137 'LOG_LOCAL0',
138 'LOG_LOCAL1',
139 'LOG_LOCAL2',
140 'LOG_LOCAL3',
141 'LOG_LOCAL4',
142 'LOG_LOCAL5',
143 'LOG_LOCAL6',
144 'LOG_LOCAL7',
145 'LOG_LPR',
146 'LOG_MAIL',
147 'LOG_NEWS',
148 'LOG_SYSLOG',
149 'LOG_USER',
150 'LOG_UUCP'
153 foreach ($constants as $constant) {
154 if (defined($constant)) {
155 $this->_validFacilities[] = constant($constant);
161 * Initialize syslog / set application name and facility
163 * @return void
165 protected function _initializeSyslog()
167 self::$_lastApplication = $this->_application;
168 self::$_lastFacility = $this->_facility;
169 openlog($this->_application, LOG_PID, $this->_facility);
173 * Set syslog facility
175 * @param int $facility Syslog facility
176 * @return void
177 * @throws Zend_Log_Exception for invalid log facility
179 public function setFacility($facility)
181 if ($this->_facility === $facility) {
182 return;
185 if (!count($this->_validFacilities)) {
186 $this->_initializeValidFacilities();
189 if (!in_array($facility, $this->_validFacilities)) {
190 require_once 'Zend/Log/Exception.php';
191 throw new Zend_Log_Exception('Invalid log facility provided; please see http://php.net/openlog for a list of valid facility values');
194 if (strstr(strtolower(PHP_OS), 'windows')
195 && ($facility !== LOG_USER)
197 require_once 'Zend/Log/Exception.php';
198 throw new Zend_Log_Exception('Only LOG_USER is a valid log facility on Windows');
201 $this->_facility = $facility;
202 $this->_initializeSyslog();
206 * Set application name
208 * @param string $application Application name
209 * @return void
211 public function setApplicationName($application)
213 if ($this->_application === $application) {
214 return;
216 $this->_application = $application;
217 $this->_initializeSyslog();
221 * Close syslog.
223 * @return void
225 public function shutdown()
227 closelog();
231 * Write a message to syslog.
233 * @param array $event event data
234 * @return void
236 protected function _write($event)
238 if (array_key_exists($event['priority'], $this->_priorities)) {
239 $priority = $this->_priorities[$event['priority']];
240 } else {
241 $priority = $this->_defaultPriority;
244 if ($this->_application !== self::$_lastApplication
245 || $this->_facility !== self::$_lastFacility)
247 $this->_initializeSyslog();
250 syslog($priority, $event['message']);