File indexing completed on 2024-12-22 05:36:50
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Log 0017 * @subpackage Writer 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 /** Zend_Log */ 0024 // require_once 'Zend/Log.php'; 0025 0026 /** Zend_Log_Writer_Abstract */ 0027 // require_once 'Zend/Log/Writer/Abstract.php'; 0028 0029 /** 0030 * Writes log messages to syslog 0031 * 0032 * @category Zend 0033 * @package Zend_Log 0034 * @subpackage Writer 0035 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0036 * @license http://framework.zend.com/license/new-bsd New BSD License 0037 */ 0038 class Zend_Log_Writer_Syslog extends Zend_Log_Writer_Abstract 0039 { 0040 /** 0041 * Maps Zend_Log priorities to PHP's syslog priorities 0042 * 0043 * @var array 0044 */ 0045 protected $_priorities = array( 0046 Zend_Log::EMERG => LOG_EMERG, 0047 Zend_Log::ALERT => LOG_ALERT, 0048 Zend_Log::CRIT => LOG_CRIT, 0049 Zend_Log::ERR => LOG_ERR, 0050 Zend_Log::WARN => LOG_WARNING, 0051 Zend_Log::NOTICE => LOG_NOTICE, 0052 Zend_Log::INFO => LOG_INFO, 0053 Zend_Log::DEBUG => LOG_DEBUG, 0054 ); 0055 0056 /** 0057 * The default log priority - for unmapped custom priorities 0058 * 0059 * @var string 0060 */ 0061 protected $_defaultPriority = LOG_NOTICE; 0062 0063 /** 0064 * Last application name set by a syslog-writer instance 0065 * 0066 * @var string 0067 */ 0068 protected static $_lastApplication; 0069 0070 /** 0071 * Last facility name set by a syslog-writer instance 0072 * 0073 * @var string 0074 */ 0075 protected static $_lastFacility; 0076 0077 /** 0078 * Application name used by this syslog-writer instance 0079 * 0080 * @var string 0081 */ 0082 protected $_application = 'Zend_Log'; 0083 0084 /** 0085 * Facility used by this syslog-writer instance 0086 * 0087 * @var int 0088 */ 0089 protected $_facility = LOG_USER; 0090 0091 /** 0092 * Types of program available to logging of message 0093 * 0094 * @var array 0095 */ 0096 protected $_validFacilities = array(); 0097 0098 /** 0099 * Class constructor 0100 * 0101 * @param array $params Array of options; may include "application" and "facility" keys 0102 * @return void 0103 */ 0104 public function __construct(array $params = array()) 0105 { 0106 if (isset($params['application'])) { 0107 $this->_application = $params['application']; 0108 } 0109 0110 $runInitializeSyslog = true; 0111 if (isset($params['facility'])) { 0112 $this->setFacility($params['facility']); 0113 $runInitializeSyslog = false; 0114 } 0115 0116 if ($runInitializeSyslog) { 0117 $this->_initializeSyslog(); 0118 } 0119 } 0120 0121 /** 0122 * Create a new instance of Zend_Log_Writer_Syslog 0123 * 0124 * @param array|Zend_Config $config 0125 * @return Zend_Log_Writer_Syslog 0126 */ 0127 static public function factory($config) 0128 { 0129 return new self(self::_parseConfig($config)); 0130 } 0131 0132 /** 0133 * Initialize values facilities 0134 * 0135 * @return void 0136 */ 0137 protected function _initializeValidFacilities() 0138 { 0139 $constants = array( 0140 'LOG_AUTH', 0141 'LOG_AUTHPRIV', 0142 'LOG_CRON', 0143 'LOG_DAEMON', 0144 'LOG_KERN', 0145 'LOG_LOCAL0', 0146 'LOG_LOCAL1', 0147 'LOG_LOCAL2', 0148 'LOG_LOCAL3', 0149 'LOG_LOCAL4', 0150 'LOG_LOCAL5', 0151 'LOG_LOCAL6', 0152 'LOG_LOCAL7', 0153 'LOG_LPR', 0154 'LOG_MAIL', 0155 'LOG_NEWS', 0156 'LOG_SYSLOG', 0157 'LOG_USER', 0158 'LOG_UUCP' 0159 ); 0160 0161 foreach ($constants as $constant) { 0162 if (defined($constant)) { 0163 $this->_validFacilities[] = constant($constant); 0164 } 0165 } 0166 } 0167 0168 /** 0169 * Initialize syslog / set application name and facility 0170 * 0171 * @return void 0172 */ 0173 protected function _initializeSyslog() 0174 { 0175 self::$_lastApplication = $this->_application; 0176 self::$_lastFacility = $this->_facility; 0177 openlog($this->_application, LOG_PID, $this->_facility); 0178 } 0179 0180 /** 0181 * Set syslog facility 0182 * 0183 * @param int $facility Syslog facility 0184 * @return Zend_Log_Writer_Syslog 0185 * @throws Zend_Log_Exception for invalid log facility 0186 */ 0187 public function setFacility($facility) 0188 { 0189 if ($this->_facility === $facility) { 0190 return $this; 0191 } 0192 0193 if (!count($this->_validFacilities)) { 0194 $this->_initializeValidFacilities(); 0195 } 0196 0197 if (!in_array($facility, $this->_validFacilities)) { 0198 // require_once 'Zend/Log/Exception.php'; 0199 throw new Zend_Log_Exception('Invalid log facility provided; please see http://php.net/openlog for a list of valid facility values'); 0200 } 0201 0202 if ('WIN' == strtoupper(substr(PHP_OS, 0, 3)) 0203 && ($facility !== LOG_USER) 0204 ) { 0205 // require_once 'Zend/Log/Exception.php'; 0206 throw new Zend_Log_Exception('Only LOG_USER is a valid log facility on Windows'); 0207 } 0208 0209 $this->_facility = $facility; 0210 $this->_initializeSyslog(); 0211 return $this; 0212 } 0213 0214 /** 0215 * Set application name 0216 * 0217 * @param string $application Application name 0218 * @return Zend_Log_Writer_Syslog 0219 */ 0220 public function setApplicationName($application) 0221 { 0222 if ($this->_application === $application) { 0223 return $this; 0224 } 0225 $this->_application = $application; 0226 $this->_initializeSyslog(); 0227 return $this; 0228 } 0229 0230 /** 0231 * Close syslog. 0232 * 0233 * @return void 0234 */ 0235 public function shutdown() 0236 { 0237 closelog(); 0238 } 0239 0240 /** 0241 * Write a message to syslog. 0242 * 0243 * @param array $event event data 0244 * @return void 0245 */ 0246 protected function _write($event) 0247 { 0248 if (array_key_exists($event['priority'], $this->_priorities)) { 0249 $priority = $this->_priorities[$event['priority']]; 0250 } else { 0251 $priority = $this->_defaultPriority; 0252 } 0253 0254 if ($this->_application !== self::$_lastApplication 0255 || $this->_facility !== self::$_lastFacility) 0256 { 0257 $this->_initializeSyslog(); 0258 } 0259 0260 $message = $event['message']; 0261 if ($this->_formatter instanceof Zend_Log_Formatter_Interface) { 0262 $message = $this->_formatter->format($event); 0263 } 0264 0265 syslog($priority, $message); 0266 } 0267 }