PageRenderTime 79ms CodeModel.GetById 72ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/Log/Writer/Firebug.php

https://bitbucket.org/bigstylee/zend-framework
PHP | 204 lines | 83 code | 21 blank | 100 comment | 7 complexity | 94714de6eb5098ec623851e43c9b50c8 MD5 | raw file
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 * @version    $Id: Firebug.php 24593 2012-01-05 20:35:02Z matthew $
 21 */
 22
 23/** Zend_Log */
 24require_once 'Zend/Log.php';
 25
 26/** Zend_Log_Writer_Abstract */
 27require_once 'Zend/Log/Writer/Abstract.php';
 28
 29/** Zend_Log_Formatter_Firebug */
 30require_once 'Zend/Log/Formatter/Firebug.php';
 31
 32/** Zend_Wildfire_Plugin_FirePhp */
 33require_once 'Zend/Wildfire/Plugin/FirePhp.php';
 34
 35/**
 36 * Writes log messages to the Firebug Console via FirePHP.
 37 *
 38 * @category   Zend
 39 * @package    Zend_Log
 40 * @subpackage Writer
 41 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 42 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 43 */
 44class Zend_Log_Writer_Firebug extends Zend_Log_Writer_Abstract
 45{
 46    /**
 47     * Maps logging priorities to logging display styles
 48     *
 49     * @var array
 50     */
 51    protected $_priorityStyles = array(Zend_Log::EMERG  => Zend_Wildfire_Plugin_FirePhp::ERROR,
 52                                       Zend_Log::ALERT  => Zend_Wildfire_Plugin_FirePhp::ERROR,
 53                                       Zend_Log::CRIT   => Zend_Wildfire_Plugin_FirePhp::ERROR,
 54                                       Zend_Log::ERR    => Zend_Wildfire_Plugin_FirePhp::ERROR,
 55                                       Zend_Log::WARN   => Zend_Wildfire_Plugin_FirePhp::WARN,
 56                                       Zend_Log::NOTICE => Zend_Wildfire_Plugin_FirePhp::INFO,
 57                                       Zend_Log::INFO   => Zend_Wildfire_Plugin_FirePhp::INFO,
 58                                       Zend_Log::DEBUG  => Zend_Wildfire_Plugin_FirePhp::LOG);
 59
 60    /**
 61     * The default logging style for un-mapped priorities
 62     *
 63     * @var string
 64     */
 65    protected $_defaultPriorityStyle = Zend_Wildfire_Plugin_FirePhp::LOG;
 66
 67    /**
 68     * Flag indicating whether the log writer is enabled
 69     *
 70     * @var boolean
 71     */
 72    protected $_enabled = true;
 73
 74    /**
 75     * Class constructor
 76     *
 77     * @return void
 78     */
 79    public function __construct()
 80    {
 81        if (php_sapi_name() == 'cli') {
 82            $this->setEnabled(false);
 83        }
 84
 85        $this->_formatter = new Zend_Log_Formatter_Firebug();
 86    }
 87
 88    /**
 89     * Create a new instance of Zend_Log_Writer_Firebug
 90     *
 91     * @param  array|Zend_Config $config
 92     * @return Zend_Log_Writer_Firebug
 93     */
 94    static public function factory($config)
 95    {
 96        return new self();
 97    }
 98
 99    /**
100     * Enable or disable the log writer.
101     *
102     * @param boolean $enabled Set to TRUE to enable the log writer
103     * @return boolean The previous value.
104     */
105    public function setEnabled($enabled)
106    {
107        $previous = $this->_enabled;
108        $this->_enabled = $enabled;
109        return $previous;
110    }
111
112    /**
113     * Determine if the log writer is enabled.
114     *
115     * @return boolean Returns TRUE if the log writer is enabled.
116     */
117    public function getEnabled()
118    {
119        return $this->_enabled;
120    }
121
122    /**
123     * Set the default display style for user-defined priorities
124     *
125     * @param string $style The default log display style
126     * @return string Returns previous default log display style
127     */
128    public function setDefaultPriorityStyle($style)
129    {
130        $previous = $this->_defaultPriorityStyle;
131        $this->_defaultPriorityStyle = $style;
132        return $previous;
133    }
134
135    /**
136     * Get the default display style for user-defined priorities
137     *
138     * @return string Returns the default log display style
139     */
140    public function getDefaultPriorityStyle()
141    {
142        return $this->_defaultPriorityStyle;
143    }
144
145    /**
146     * Set a display style for a logging priority
147     *
148     * @param int $priority The logging priority
149     * @param string $style The logging display style
150     * @return string|boolean The previous logging display style if defined or TRUE otherwise
151     */
152    public function setPriorityStyle($priority, $style)
153    {
154        $previous = true;
155        if (array_key_exists($priority,$this->_priorityStyles)) {
156            $previous = $this->_priorityStyles[$priority];
157        }
158        $this->_priorityStyles[$priority] = $style;
159        return $previous;
160    }
161
162    /**
163     * Get a display style for a logging priority
164     *
165     * @param int $priority The logging priority
166     * @return string|boolean The logging display style if defined or FALSE otherwise
167     */
168    public function getPriorityStyle($priority)
169    {
170        if (array_key_exists($priority,$this->_priorityStyles)) {
171            return $this->_priorityStyles[$priority];
172        }
173        return false;
174    }
175
176    /**
177     * Log a message to the Firebug Console.
178     *
179     * @param array $event The event data
180     * @return void
181     */
182    protected function _write($event)
183    {
184        if (!$this->getEnabled()) {
185            return;
186        }
187
188        if (array_key_exists($event['priority'],$this->_priorityStyles)) {
189            $type = $this->_priorityStyles[$event['priority']];
190        } else {
191            $type = $this->_defaultPriorityStyle;
192        }
193
194        $message = $this->_formatter->format($event);
195
196        $label = isset($event['firebugLabel'])?$event['firebugLabel']:null;
197
198        Zend_Wildfire_Plugin_FirePhp::getInstance()->send($message,
199                                                          $label,
200                                                          $type,
201                                                          array('traceOffset'=>4,
202                                                                'fixZendLogOffsetIfApplicable'=>true));
203    }
204}