Syslog.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. */
  21. /** Zend_Log_Writer_Abstract */
  22. require_once 'Zend/Log/Writer/Abstract.php';
  23. /**
  24. * Writes log messages to syslog
  25. *
  26. * @category Zend
  27. * @package Zend_Log
  28. * @subpackage Writer
  29. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Log_Writer_Syslog extends Zend_Log_Writer_Abstract
  33. {
  34. /**
  35. * Maps Zend_Log priorities to PHP's syslog priorities
  36. * @var array
  37. */
  38. protected $_priorities = array(
  39. Zend_Log::EMERG => LOG_EMERG,
  40. Zend_Log::ALERT => LOG_ALERT,
  41. Zend_Log::CRIT => LOG_CRIT,
  42. Zend_Log::ERR => LOG_ERR,
  43. Zend_Log::WARN => LOG_WARNING,
  44. Zend_Log::NOTICE => LOG_NOTICE,
  45. Zend_Log::INFO => LOG_INFO,
  46. Zend_Log::DEBUG => LOG_DEBUG,
  47. );
  48. /**
  49. * The default log priority - for unmapped custom priorities
  50. * @var string
  51. */
  52. protected $_defaultPriority = LOG_NOTICE;
  53. /**
  54. * Last application name set by a syslog-writer instance
  55. * @var string
  56. */
  57. protected static $_lastApplication;
  58. /**
  59. * Last facility name set by a syslog-writer instance
  60. * @var string
  61. */
  62. protected static $_lastFacility;
  63. /**
  64. * Application name used by this syslog-writer instance
  65. * @var string
  66. */
  67. protected $_application = 'Zend_Log';
  68. /**
  69. * Facility used by this syslog-writer instance
  70. * @var string
  71. */
  72. protected $_facility = LOG_USER;
  73. /**
  74. * Class constructor
  75. *
  76. * @param array $options Array of options; may include "application" and "facility" keys
  77. * @return void
  78. */
  79. public function __construct(array $params = array())
  80. {
  81. if (isset($params['application'])) {
  82. $this->_application = $params['application'];
  83. }
  84. if (isset($params['facility'])) {
  85. $this->_facility = $params['facility'];
  86. }
  87. $this->_initializeSyslog();
  88. }
  89. /**
  90. * Initialize syslog / set application name and facility
  91. *
  92. * @param string $application Application name
  93. * @param string $facility Syslog facility
  94. * @return void
  95. */
  96. protected function _initializeSyslog()
  97. {
  98. self::$_lastApplication = $this->_application;
  99. self::$_lastFacility = $this->_facility;
  100. openlog($this->_application, LOG_PID, $this->_facility);
  101. }
  102. /**
  103. * Set syslog facility
  104. *
  105. * @param string $facility Syslog facility
  106. * @return void
  107. */
  108. public function setFacility($facility)
  109. {
  110. if ($this->_facility === $facility) {
  111. return;
  112. }
  113. $this->_facility = $facility;
  114. $this->_initializeSyslog();
  115. }
  116. /**
  117. * Set application name
  118. *
  119. * @param string $application Application name
  120. * @return void
  121. */
  122. public function setApplicationName($application)
  123. {
  124. if ($this->_application === $application) {
  125. return;
  126. }
  127. $this->_application = $application;
  128. $this->_initializeSyslog();
  129. }
  130. /**
  131. * Close syslog.
  132. *
  133. * @return void
  134. */
  135. public function shutdown()
  136. {
  137. closelog();
  138. }
  139. /**
  140. * Write a message to syslog.
  141. *
  142. * @param array $event event data
  143. * @return void
  144. */
  145. protected function _write($event)
  146. {
  147. if (array_key_exists($event['priority'], $this->_priorities)) {
  148. $priority = $this->_priorities[$event['priority']];
  149. } else {
  150. $priority = $this->_defaultPriority;
  151. }
  152. if ($this->_application !== self::$_lastApplication
  153. || $this->_facility !== self::$_lastFacility)
  154. {
  155. $this->_initializeSyslog();
  156. }
  157. syslog($priority, $event['message']);
  158. }
  159. }