2
0

ZendMonitor.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. * @category Zend
  26. * @package Zend_Log
  27. * @subpackage Writer
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @version $Id$
  31. */
  32. class Zend_Log_Writer_ZendMonitor extends Zend_Log_Writer_Abstract
  33. {
  34. /**
  35. * Is Zend Monitor enabled?
  36. * @var bool
  37. */
  38. protected $_isEnabled = true;
  39. /**
  40. * Is this for a Zend Server intance?
  41. * @var bool
  42. */
  43. protected $_isZendServer = false;
  44. /**
  45. * @throws Zend_Log_Exception if Zend Monitor extension not present
  46. */
  47. public function __construct()
  48. {
  49. if (!function_exists('monitor_custom_event')) {
  50. $this->_isEnabled = false;
  51. }
  52. if (function_exists('zend_monitor_custom_event')) {
  53. $this->_isZendServer = true;
  54. }
  55. }
  56. /**
  57. * Create a new instance of Zend_Log_Writer_ZendMonitor
  58. *
  59. * @param array|Zend_Config $config
  60. * @return Zend_Log_Writer_Syslog
  61. * @throws Zend_Log_Exception
  62. */
  63. static public function factory($config)
  64. {
  65. return new self();
  66. }
  67. /**
  68. * Is logging to this writer enabled?
  69. *
  70. * If the Zend Monitor extension is not enabled, this log writer will
  71. * fail silently. You can query this method to determine if the log
  72. * writer is enabled.
  73. *
  74. * @return bool
  75. */
  76. public function isEnabled()
  77. {
  78. return $this->_isEnabled;
  79. }
  80. /**
  81. * Log a message to this writer.
  82. *
  83. * @param array $event log data event
  84. * @return void
  85. */
  86. public function write($event)
  87. {
  88. if (!$this->isEnabled()) {
  89. return;
  90. }
  91. parent::write($event);
  92. }
  93. /**
  94. * Write a message to the log.
  95. *
  96. * @param array $event log data event
  97. * @return void
  98. */
  99. protected function _write($event)
  100. {
  101. $priority = $event['priority'];
  102. $message = $event['message'];
  103. unset($event['priority'], $event['message']);
  104. if (!empty($event)) {
  105. if ($this->_isZendServer) {
  106. // On Zend Server; third argument should be the event
  107. zend_monitor_custom_event($priority, $message, $event);
  108. } else {
  109. // On Zend Platform; third argument is severity -- either
  110. // 0 or 1 -- and fourth is optional (event)
  111. // Severity is either 0 (normal) or 1 (severe); classifying
  112. // notice, info, and debug as "normal", and all others as
  113. // "severe"
  114. monitor_custom_event($priority, $message, ($priority > 4) ? 0 : 1, $event);
  115. }
  116. } else {
  117. monitor_custom_event($priority, $message);
  118. }
  119. }
  120. }