2
0

Syslog.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 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. * @param array|Zend_Config $config
  94. * @return Zend_Log_Writer_Syslog
  95. * @throws Zend_Log_Exception
  96. */
  97. static public function factory($config)
  98. {
  99. return new self(self::_parseConfig($config));
  100. }
  101. /**
  102. * Initialize syslog / set application name and facility
  103. *
  104. * @param string $application Application name
  105. * @param string $facility Syslog facility
  106. * @return void
  107. */
  108. protected function _initializeSyslog()
  109. {
  110. self::$_lastApplication = $this->_application;
  111. self::$_lastFacility = $this->_facility;
  112. openlog($this->_application, LOG_PID, $this->_facility);
  113. }
  114. /**
  115. * Set syslog facility
  116. *
  117. * @param string $facility Syslog facility
  118. * @return void
  119. */
  120. public function setFacility($facility)
  121. {
  122. if ($this->_facility === $facility) {
  123. return;
  124. }
  125. $this->_facility = $facility;
  126. $this->_initializeSyslog();
  127. }
  128. /**
  129. * Set application name
  130. *
  131. * @param string $application Application name
  132. * @return void
  133. */
  134. public function setApplicationName($application)
  135. {
  136. if ($this->_application === $application) {
  137. return;
  138. }
  139. $this->_application = $application;
  140. $this->_initializeSyslog();
  141. }
  142. /**
  143. * Close syslog.
  144. *
  145. * @return void
  146. */
  147. public function shutdown()
  148. {
  149. closelog();
  150. }
  151. /**
  152. * Write a message to syslog.
  153. *
  154. * @param array $event event data
  155. * @return void
  156. */
  157. protected function _write($event)
  158. {
  159. if (array_key_exists($event['priority'], $this->_priorities)) {
  160. $priority = $this->_priorities[$event['priority']];
  161. } else {
  162. $priority = $this->_defaultPriority;
  163. }
  164. if ($this->_application !== self::$_lastApplication
  165. || $this->_facility !== self::$_lastFacility)
  166. {
  167. $this->_initializeSyslog();
  168. }
  169. syslog($priority, $event['message']);
  170. }
  171. }