Firebug.php 5.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-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 */
  22. require_once 'Zend/Log.php';
  23. /** Zend_Log_Writer_Abstract */
  24. require_once 'Zend/Log/Writer/Abstract.php';
  25. /** Zend_Log_Formatter_Firebug */
  26. require_once 'Zend/Log/Formatter/Firebug.php';
  27. /** Zend_Wildfire_Plugin_FirePhp */
  28. require_once 'Zend/Wildfire/Plugin/FirePhp.php';
  29. /**
  30. * Writes log messages to the Firebug Console via FirePHP.
  31. *
  32. * @category Zend
  33. * @package Zend_Log
  34. * @subpackage Writer
  35. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. class Zend_Log_Writer_Firebug extends Zend_Log_Writer_Abstract
  39. {
  40. /**
  41. * Maps logging priorities to logging display styles
  42. * @var array
  43. */
  44. protected $_priorityStyles = array(Zend_Log::EMERG => Zend_Wildfire_Plugin_FirePhp::ERROR,
  45. Zend_Log::ALERT => Zend_Wildfire_Plugin_FirePhp::ERROR,
  46. Zend_Log::CRIT => Zend_Wildfire_Plugin_FirePhp::ERROR,
  47. Zend_Log::ERR => Zend_Wildfire_Plugin_FirePhp::ERROR,
  48. Zend_Log::WARN => Zend_Wildfire_Plugin_FirePhp::WARN,
  49. Zend_Log::NOTICE => Zend_Wildfire_Plugin_FirePhp::INFO,
  50. Zend_Log::INFO => Zend_Wildfire_Plugin_FirePhp::INFO,
  51. Zend_Log::DEBUG => Zend_Wildfire_Plugin_FirePhp::LOG);
  52. /**
  53. * The default logging style for un-mapped priorities
  54. * @var string
  55. */
  56. protected $_defaultPriorityStyle = Zend_Wildfire_Plugin_FirePhp::LOG;
  57. /**
  58. * Flag indicating whether the log writer is enabled
  59. * @var boolean
  60. */
  61. protected $_enabled = true;
  62. /**
  63. * Class constructor
  64. */
  65. public function __construct()
  66. {
  67. if (php_sapi_name()=='cli') {
  68. $this->setEnabled(false);
  69. }
  70. $this->_formatter = new Zend_Log_Formatter_Firebug();
  71. }
  72. /**
  73. * Enable or disable the log writer.
  74. *
  75. * @param boolean $enabled Set to TRUE to enable the log writer
  76. * @return boolean The previous value.
  77. */
  78. public function setEnabled($enabled)
  79. {
  80. $previous = $this->_enabled;
  81. $this->_enabled = $enabled;
  82. return $previous;
  83. }
  84. /**
  85. * Determine if the log writer is enabled.
  86. *
  87. * @return boolean Returns TRUE if the log writer is enabled.
  88. */
  89. public function getEnabled()
  90. {
  91. return $this->_enabled;
  92. }
  93. /**
  94. * Set the default display style for user-defined priorities
  95. *
  96. * @param string $style The default log display style
  97. * @return string Returns previous default log display style
  98. */
  99. public function setDefaultPriorityStyle($style)
  100. {
  101. $previous = $this->_defaultPriorityStyle;
  102. $this->_defaultPriorityStyle = $style;
  103. return $previous;
  104. }
  105. /**
  106. * Get the default display style for user-defined priorities
  107. *
  108. * @return string Returns the default log display style
  109. */
  110. public function getDefaultPriorityStyle()
  111. {
  112. return $this->_defaultPriorityStyle;
  113. }
  114. /**
  115. * Set a display style for a logging priority
  116. *
  117. * @param int $priority The logging priority
  118. * @param string $style The logging display style
  119. * @return string|boolean The previous logging display style if defined or TRUE otherwise
  120. */
  121. public function setPriorityStyle($priority, $style)
  122. {
  123. $previous = true;
  124. if (array_key_exists($priority,$this->_priorityStyles)) {
  125. $previous = $this->_priorityStyles[$priority];
  126. }
  127. $this->_priorityStyles[$priority] = $style;
  128. return $previous;
  129. }
  130. /**
  131. * Get a display style for a logging priority
  132. *
  133. * @param int $priority The logging priority
  134. * @return string|boolean The logging display style if defined or FALSE otherwise
  135. */
  136. public function getPriorityStyle($priority)
  137. {
  138. if (array_key_exists($priority,$this->_priorityStyles)) {
  139. return $this->_priorityStyles[$priority];
  140. }
  141. return false;
  142. }
  143. /**
  144. * Log a message to the Firebug Console.
  145. *
  146. * @param array $event The event data
  147. * @return void
  148. */
  149. protected function _write($event)
  150. {
  151. if (!$this->getEnabled()) {
  152. return;
  153. }
  154. if (array_key_exists($event['priority'],$this->_priorityStyles)) {
  155. $type = $this->_priorityStyles[$event['priority']];
  156. } else {
  157. $type = $this->_defaultPriorityStyle;
  158. }
  159. $message = $this->_formatter->format($event);
  160. $label = isset($event['firebugLabel'])?$event['firebugLabel']:null;
  161. Zend_Wildfire_Plugin_FirePhp::getInstance()->send($message,
  162. $label,
  163. $type,
  164. array('traceOffset'=>6));
  165. }
  166. }