PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Log/Writer/Syslog.php

https://bitbucket.org/mengqing/magento-mirror
PHP | 257 lines | 120 code | 26 blank | 111 comment | 15 complexity | 55a879eff24fab333f8c6234b37f36e8 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  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: Syslog.php 22977 2010-09-19 12:44:00Z intiilapa $
  21. */
  22. /** Zend_Log */
  23. #require_once 'Zend/Log.php';
  24. /** Zend_Log_Writer_Abstract */
  25. #require_once 'Zend/Log/Writer/Abstract.php';
  26. /**
  27. * Writes log messages to syslog
  28. *
  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
  34. */
  35. class Zend_Log_Writer_Syslog extends Zend_Log_Writer_Abstract
  36. {
  37. /**
  38. * Maps Zend_Log priorities to PHP's syslog priorities
  39. * @var array
  40. */
  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,
  50. );
  51. /**
  52. * The default log priority - for unmapped custom priorities
  53. * @var string
  54. */
  55. protected $_defaultPriority = LOG_NOTICE;
  56. /**
  57. * Last application name set by a syslog-writer instance
  58. * @var string
  59. */
  60. protected static $_lastApplication;
  61. /**
  62. * Last facility name set by a syslog-writer instance
  63. * @var string
  64. */
  65. protected static $_lastFacility;
  66. /**
  67. * Application name used by this syslog-writer instance
  68. * @var string
  69. */
  70. protected $_application = 'Zend_Log';
  71. /**
  72. * Facility used by this syslog-writer instance
  73. * @var int
  74. */
  75. protected $_facility = LOG_USER;
  76. /**
  77. * _validFacilities
  78. *
  79. * @var array
  80. */
  81. protected $_validFacilities = array();
  82. /**
  83. * Class constructor
  84. *
  85. * @param array $options Array of options; may include "application" and "facility" keys
  86. * @return void
  87. */
  88. public function __construct(array $params = array())
  89. {
  90. if (isset($params['application'])) {
  91. $this->_application = $params['application'];
  92. }
  93. $runInitializeSyslog = true;
  94. if (isset($params['facility'])) {
  95. $this->_facility = $this->setFacility($params['facility']);
  96. $runInitializeSyslog = false;
  97. }
  98. if ($runInitializeSyslog) {
  99. $this->_initializeSyslog();
  100. }
  101. }
  102. /**
  103. * Create a new instance of Zend_Log_Writer_Syslog
  104. *
  105. * @param array|Zend_Config $config
  106. * @return Zend_Log_Writer_Syslog
  107. * @throws Zend_Log_Exception
  108. */
  109. static public function factory($config)
  110. {
  111. return new self(self::_parseConfig($config));
  112. }
  113. /**
  114. * Initialize values facilities
  115. *
  116. * @return void
  117. */
  118. protected function _initializeValidFacilities()
  119. {
  120. $constants = array(
  121. 'LOG_AUTH',
  122. 'LOG_AUTHPRIV',
  123. 'LOG_CRON',
  124. 'LOG_DAEMON',
  125. 'LOG_KERN',
  126. 'LOG_LOCAL0',
  127. 'LOG_LOCAL1',
  128. 'LOG_LOCAL2',
  129. 'LOG_LOCAL3',
  130. 'LOG_LOCAL4',
  131. 'LOG_LOCAL5',
  132. 'LOG_LOCAL6',
  133. 'LOG_LOCAL7',
  134. 'LOG_LPR',
  135. 'LOG_MAIL',
  136. 'LOG_NEWS',
  137. 'LOG_SYSLOG',
  138. 'LOG_USER',
  139. 'LOG_UUCP'
  140. );
  141. foreach ($constants as $constant) {
  142. if (defined($constant)) {
  143. $this->_validFacilities[] = constant($constant);
  144. }
  145. }
  146. }
  147. /**
  148. * Initialize syslog / set application name and facility
  149. *
  150. * @return void
  151. */
  152. protected function _initializeSyslog()
  153. {
  154. self::$_lastApplication = $this->_application;
  155. self::$_lastFacility = $this->_facility;
  156. openlog($this->_application, LOG_PID, $this->_facility);
  157. }
  158. /**
  159. * Set syslog facility
  160. *
  161. * @param int $facility Syslog facility
  162. * @return void
  163. * @throws Zend_Log_Exception for invalid log facility
  164. */
  165. public function setFacility($facility)
  166. {
  167. if ($this->_facility === $facility) {
  168. return $this;
  169. }
  170. if (!count($this->_validFacilities)) {
  171. $this->_initializeValidFacilities();
  172. }
  173. if (!in_array($facility, $this->_validFacilities)) {
  174. #require_once 'Zend/Log/Exception.php';
  175. throw new Zend_Log_Exception('Invalid log facility provided; please see http://php.net/openlog for a list of valid facility values');
  176. }
  177. if ('WIN' == strtoupper(substr(PHP_OS, 0, 3))
  178. && ($facility !== LOG_USER)
  179. ) {
  180. #require_once 'Zend/Log/Exception.php';
  181. throw new Zend_Log_Exception('Only LOG_USER is a valid log facility on Windows');
  182. }
  183. $this->_facility = $facility;
  184. $this->_initializeSyslog();
  185. return $this;
  186. }
  187. /**
  188. * Set application name
  189. *
  190. * @param string $application Application name
  191. * @return void
  192. */
  193. public function setApplicationName($application)
  194. {
  195. if ($this->_application === $application) {
  196. return $this;
  197. }
  198. $this->_application = $application;
  199. $this->_initializeSyslog();
  200. return $this;
  201. }
  202. /**
  203. * Close syslog.
  204. *
  205. * @return void
  206. */
  207. public function shutdown()
  208. {
  209. closelog();
  210. }
  211. /**
  212. * Write a message to syslog.
  213. *
  214. * @param array $event event data
  215. * @return void
  216. */
  217. protected function _write($event)
  218. {
  219. if (array_key_exists($event['priority'], $this->_priorities)) {
  220. $priority = $this->_priorities[$event['priority']];
  221. } else {
  222. $priority = $this->_defaultPriority;
  223. }
  224. if ($this->_application !== self::$_lastApplication
  225. || $this->_facility !== self::$_lastFacility)
  226. {
  227. $this->_initializeSyslog();
  228. }
  229. syslog($priority, $event['message']);
  230. }
  231. }