[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/log/src/ -> lmbLog.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/log/src/lmbLogEntry.class.php');
  10  lmb_require('limb/log/src/lmbBacktrace.class.php');
  11  
  12  /**

  13   * class lmbLog.

  14   *

  15   * @package log

  16   * @version $Id$

  17   */
  18  class lmbLog
  19  {
  20    const NOTICE  = 1;
  21    const WARNING = 2;
  22    const ERROR   = 3;
  23    const INFO    = 4;
  24  
  25    protected $logs = array();
  26    protected $log_writers = array();
  27    protected $allowed_levels = array();
  28  
  29    function __construct()
  30    {
  31      $this->allowed_levels = array(
  32        lmbLog :: NOTICE => true,
  33        lmbLog :: WARNING => true,
  34        lmbLog :: ERROR => true,
  35        lmbLog :: INFO => true
  36      );
  37    }
  38  
  39    function skipNotice()
  40    {
  41      $this->_skipErrorLevel(lmbLog :: NOTICE);
  42    }
  43  
  44    function skipWarning()
  45    {
  46      $this->_skipErrorLevel(lmbLog :: WARNING);
  47    }
  48  
  49    function skipError()
  50    {
  51      $this->_skipErrorLevel(lmbLog :: ERROR);
  52    }
  53  
  54    function skipInfo()
  55    {
  56      $this->_skipErrorLevel(lmbLog :: INFO);
  57    }
  58  
  59    function _skipErrorLevel($level)
  60    {
  61      $this->allowed_levels[$level] = false;
  62    }
  63  
  64    function registerWriter($writer)
  65    {
  66      $this->log_writers[] = $writer;
  67    }
  68  
  69    function reset()
  70    {
  71      $this->logs = array();
  72    }
  73  
  74    function getLogs()
  75    {
  76      return $this->logs;
  77    }
  78  
  79    function sizeof()
  80    {
  81      return sizeof($this->logs);
  82    }
  83  
  84    function notice($message, $params = array(), $backtrace = null)
  85    {
  86      if(!$this->isLogEnabled())
  87        return;
  88  
  89      if(!$backtrace)
  90        $backtrace = new lmbBacktrace(1);
  91  
  92      $this->_write(lmbLog :: NOTICE, $message, $params, $backtrace);
  93    }
  94  
  95    function warning($message, $params = array(), $backtrace = null)
  96    {
  97      if(!$this->isLogEnabled())
  98        return;
  99  
 100      if(!$backtrace)
 101        $backtrace = new lmbBacktrace(1);
 102  
 103      $this->_write(lmbLog :: WARNING, $message, $params, $backtrace);
 104    }
 105  
 106    function error($message, $params = array(), $backtrace = null)
 107    {
 108      if(!$this->isLogEnabled())
 109        return;
 110  
 111      if(!$backtrace)
 112        $backtrace = new lmbBacktrace(5);
 113  
 114      $this->_write(lmbLog :: ERROR, $message, $params, $backtrace);
 115    }
 116  
 117    function exception($e)
 118    {
 119      if(!$this->isLogEnabled())
 120        return;
 121  
 122      if(is_a($e, 'lmbException'))
 123        $this->error($e->getMessage(), $e->getParams(), new lmbBacktrace($e->getTrace(), 5));
 124      else
 125        $this->error($e->getMessage(), array(), new lmbBacktrace($e->getTrace(), 5));
 126    }
 127  
 128    function info($message, $params = array(), $backtrace = null)
 129    {
 130      if(!$this->isLogEnabled())
 131        return;
 132  
 133      if(!$backtrace)
 134        $backtrace = new backtrace(3);
 135  
 136      $this->_write(lmbLog :: INFO, $message, $params, $backtrace);
 137    }
 138  
 139    protected function _write($level, $string, $params = array(), $backtrace = null)
 140    {
 141      if(!$this->_isAllowedLevel($level))
 142        return;
 143  
 144      $entry = new lmbLogEntry($level, $string, $params, $backtrace);
 145      $this->logs[] = $entry;
 146  
 147      $this->_writeLogEntry($entry);
 148    }
 149  
 150    function _isAllowedLevel($level)
 151    {
 152      return isset($this->allowed_levels[$level]) && $this->allowed_levels[$level];
 153    }
 154  
 155    function _writeLogEntry($entry)
 156    {
 157      foreach($this->log_writers as $writer)
 158        $writer->write($entry);
 159    }
 160  
 161    function isLogEnabled()
 162    {
 163      return (!defined('LIMB_LOG_ENABLE') ||
 164              (defined('LIMB_LOG_ENABLE') && constant('LIMB_LOG_ENABLE')));
 165    }
 166  }
 167  
 168  ?>


Generated: Tue Oct 7 05:02:03 2008 Cross-referenced by PHPXref 0.7