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.
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
23 /** Zend_Log_Writer_Abstract */
24 require_once 'Zend/Log/Writer/Abstract.php';
27 * Writes log messages to syslog
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
38 * Maps Zend_Log priorities to PHP's syslog priorities
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
,
53 * The default log priority - for unmapped custom priorities
56 protected $_defaultPriority = LOG_NOTICE
;
59 * Last application name set by a syslog-writer instance
62 protected static $_lastApplication;
65 * Last facility name set by a syslog-writer instance
68 protected static $_lastFacility;
71 * Application name used by this syslog-writer instance
74 protected $_application = 'Zend_Log';
77 * Facility used by this syslog-writer instance
80 protected $_facility = LOG_USER
;
87 protected $_validFacilities = array();
92 * @param array $options Array of options; may include "application" and "facility" keys
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
129 protected function _initializeValidFacilities()
153 foreach ($constants as $constant) {
154 if (defined($constant)) {
155 $this->_validFacilities
[] = constant($constant);
161 * Initialize syslog / set application name and facility
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
177 * @throws Zend_Log_Exception for invalid log facility
179 public function setFacility($facility)
181 if ($this->_facility
=== $facility) {
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
211 public function setApplicationName($application)
213 if ($this->_application
=== $application) {
216 $this->_application
= $application;
217 $this->_initializeSyslog();
225 public function shutdown()
231 * Write a message to syslog.
233 * @param array $event event data
236 protected function _write($event)
238 if (array_key_exists($event['priority'], $this->_priorities
)) {
239 $priority = $this->_priorities
[$event['priority']];
241 $priority = $this->_defaultPriority
;
244 if ($this->_application
!== self
::$_lastApplication
245 ||
$this->_facility
!== self
::$_lastFacility)
247 $this->_initializeSyslog();
250 syslog($priority, $event['message']);