/library/Zend/Log/Writer/Syslog.php

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