2
0

Firebug.php 5.7 KB

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