Syslog.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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-2009 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-2009 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 string
  72. */
  73. protected $_facility = LOG_USER;
  74. /**
  75. * Class constructor
  76. *
  77. * @param array $options Array of options; may include "application" and "facility" keys
  78. * @return void
  79. */
  80. public function __construct(array $params = array())
  81. {
  82. if (isset($params['application'])) {
  83. $this->_application = $params['application'];
  84. }
  85. if (isset($params['facility'])) {
  86. $this->_facility = $params['facility'];
  87. }
  88. $this->_initializeSyslog();
  89. }
  90. /**
  91. * Create a new instance of Zend_Log_Writer_Syslog
  92. *
  93. * @exception Zend_Log_Exception
  94. * @param mixed $config
  95. * @return Zend_Log_Writer_Syslog
  96. */
  97. static public function factory($config)
  98. {
  99. return new self(
  100. self::_parseConfig($config)
  101. );
  102. }
  103. /**
  104. * Initialize syslog / set application name and facility
  105. *
  106. * @param string $application Application name
  107. * @param string $facility Syslog facility
  108. * @return void
  109. */
  110. protected function _initializeSyslog()
  111. {
  112. self::$_lastApplication = $this->_application;
  113. self::$_lastFacility = $this->_facility;
  114. openlog($this->_application, LOG_PID, $this->_facility);
  115. }
  116. /**
  117. * Set syslog facility
  118. *
  119. * @param string $facility Syslog facility
  120. * @return void
  121. */
  122. public function setFacility($facility)
  123. {
  124. if ($this->_facility === $facility) {
  125. return;
  126. }
  127. $this->_facility = $facility;
  128. $this->_initializeSyslog();
  129. }
  130. /**
  131. * Set application name
  132. *
  133. * @param string $application Application name
  134. * @return void
  135. */
  136. public function setApplicationName($application)
  137. {
  138. if ($this->_application === $application) {
  139. return;
  140. }
  141. $this->_application = $application;
  142. $this->_initializeSyslog();
  143. }
  144. /**
  145. * Close syslog.
  146. *
  147. * @return void
  148. */
  149. public function shutdown()
  150. {
  151. closelog();
  152. }
  153. /**
  154. * Write a message to syslog.
  155. *
  156. * @param array $event event data
  157. * @return void
  158. */
  159. protected function _write($event)
  160. {
  161. if (array_key_exists($event['priority'], $this->_priorities)) {
  162. $priority = $this->_priorities[$event['priority']];
  163. } else {
  164. $priority = $this->_defaultPriority;
  165. }
  166. if ($this->_application !== self::$_lastApplication
  167. || $this->_facility !== self::$_lastFacility)
  168. {
  169. $this->_initializeSyslog();
  170. }
  171. syslog($priority, $event['message']);
  172. }
  173. }