Syslog.php 4.5 KB

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