Syslog.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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$
  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. LOG_AUTH,
  81. LOG_AUTHPRIV,
  82. LOG_CRON,
  83. LOG_DAEMON,
  84. LOG_KERN,
  85. LOG_LOCAL0,
  86. LOG_LOCAL1,
  87. LOG_LOCAL2,
  88. LOG_LOCAL3,
  89. LOG_LOCAL4,
  90. LOG_LOCAL5,
  91. LOG_LOCAL6,
  92. LOG_LOCAL7,
  93. LOG_LPR,
  94. LOG_MAIL,
  95. LOG_NEWS,
  96. LOG_SYSLOG,
  97. LOG_USER,
  98. LOG_UUCP,
  99. );
  100. /**
  101. * Class constructor
  102. *
  103. * @param array $options Array of options; may include "application" and "facility" keys
  104. * @return void
  105. */
  106. public function __construct(array $params = array())
  107. {
  108. if (isset($params['application'])) {
  109. $this->_application = $params['application'];
  110. }
  111. if (isset($params['facility'])) {
  112. $this->_facility = $params['facility'];
  113. }
  114. $this->_initializeSyslog();
  115. }
  116. /**
  117. * Create a new instance of Zend_Log_Writer_Syslog
  118. *
  119. * @param array|Zend_Config $config
  120. * @return Zend_Log_Writer_Syslog
  121. * @throws Zend_Log_Exception
  122. */
  123. static public function factory($config)
  124. {
  125. return new self(self::_parseConfig($config));
  126. }
  127. /**
  128. * Initialize syslog / set application name and facility
  129. *
  130. * @param string $application Application name
  131. * @param string $facility Syslog facility
  132. * @return void
  133. */
  134. protected function _initializeSyslog()
  135. {
  136. self::$_lastApplication = $this->_application;
  137. self::$_lastFacility = $this->_facility;
  138. openlog($this->_application, LOG_PID, $this->_facility);
  139. }
  140. /**
  141. * Set syslog facility
  142. *
  143. * @param int $facility Syslog facility
  144. * @return void
  145. * @throws Zend_Log_Exception for invalid log facility
  146. */
  147. public function setFacility($facility)
  148. {
  149. if ($this->_facility === $facility) {
  150. return;
  151. }
  152. if (!in_array($facility, $this->_validFacilities)) {
  153. require_once 'Zend/Log/Exception.php';
  154. throw new Zend_Log_Exception('Invalid log facility provided; please see http://php.net/openlog for a list of valid facility values');
  155. }
  156. if (strstr(strtolower(PHP_OS), 'windows')
  157. && ($facility !== LOG_USER)
  158. ) {
  159. require_once 'Zend/Log/Exception.php';
  160. throw new Zend_Log_Exception('Only LOG_USER is a valid log facility on Windows');
  161. }
  162. $this->_facility = $facility;
  163. $this->_initializeSyslog();
  164. }
  165. /**
  166. * Set application name
  167. *
  168. * @param string $application Application name
  169. * @return void
  170. */
  171. public function setApplicationName($application)
  172. {
  173. if ($this->_application === $application) {
  174. return;
  175. }
  176. $this->_application = $application;
  177. $this->_initializeSyslog();
  178. }
  179. /**
  180. * Close syslog.
  181. *
  182. * @return void
  183. */
  184. public function shutdown()
  185. {
  186. closelog();
  187. }
  188. /**
  189. * Write a message to syslog.
  190. *
  191. * @param array $event event data
  192. * @return void
  193. */
  194. protected function _write($event)
  195. {
  196. if (array_key_exists($event['priority'], $this->_priorities)) {
  197. $priority = $this->_priorities[$event['priority']];
  198. } else {
  199. $priority = $this->_defaultPriority;
  200. }
  201. if ($this->_application !== self::$_lastApplication
  202. || $this->_facility !== self::$_lastFacility)
  203. {
  204. $this->_initializeSyslog();
  205. }
  206. syslog($priority, $event['message']);
  207. }
  208. }