PRADO PHP Cross Reference Developer Tools

Source: /framework/Util/TLogger.php - 236 lines - 8261 bytes - Summary - Text - Print

Description: TLogger class file

   1  <?php
   2  /**
   3   * TLogger class file
   4   *
   5   * @author Qiang Xue <qiang.xue@gmail.com>
   6   * @link http://www.pradosoft.com/
   7   * @copyright Copyright &copy; 2005-2013 PradoSoft
   8   * @license http://www.pradosoft.com/license/
   9   * @version $Id: TLogger.php 3245 2013-01-07 20:23:32Z ctrlaltca $
  10   * @package System.Util
  11   */
  12  
  13  /**
  14   * TLogger class.
  15   *
  16   * TLogger records log messages in memory and implements the methods to
  17   * retrieve the messages with filter conditions, including log levels,
  18   * log categories, and by control.
  19   *
  20   * @author Qiang Xue <qiang.xue@gmail.com>
  21   * @version $Id: TLogger.php 3245 2013-01-07 20:23:32Z ctrlaltca $
  22   * @package System.Util
  23   * @since 3.0
  24   */
  25  class TLogger extends TComponent
  26  {
  27      /**
  28       * Log levels.
  29       */
  30      const DEBUG=0x01;
  31      const INFO=0x02;
  32      const NOTICE=0x04;
  33      const WARNING=0x08;
  34      const ERROR=0x10;
  35      const ALERT=0x20;
  36      const FATAL=0x40;
  37      /**
  38       * @var array log messages
  39       */
  40      private $_logs=array();
  41      /**
  42       * @var integer log levels (bits) to be filtered
  43       */
  44      private $_levels;
  45      /**
  46       * @var array list of categories to be filtered
  47       */
  48      private $_categories;
  49      /**
  50       * @var array list of control client ids to be filtered
  51       */
  52      private $_controls;
  53      /**
  54       * @var float timestamp used to filter
  55       */
  56      private $_timestamp;
  57  
  58      /**
  59       * Logs a message.
  60       * Messages logged by this method may be retrieved via {@link getLogs}.
  61       * @param string message to be logged
  62       * @param integer level of the message. Valid values include
  63       * TLogger::DEBUG, TLogger::INFO, TLogger::NOTICE, TLogger::WARNING,
  64       * TLogger::ERROR, TLogger::ALERT, TLogger::FATAL.
  65       * @param string category of the message
  66       * @param string|TControl control of the message
  67       */
  68  	public function log($message,$level,$category='Uncategorized', $ctl=null)
  69      {
  70          if($ctl) {
  71              if($ctl instanceof TControl)
  72                  $ctl = $ctl->ClientId;
  73              else if(!is_string($ctl))
  74                  $ctl = null;
  75          } else
  76              $ctl = null;
  77          $this->_logs[]=array($message,$level,$category,microtime(true),memory_get_usage(),$ctl);
  78      }
  79  
  80      /**
  81       * Retrieves log messages.
  82       * Messages may be filtered by log levels and/or categories and/or control client ids and/or timestamp.
  83       * A level filter is specified by an integer, whose bits indicate the levels interested.
  84       * For example, (TLogger::INFO | TLogger::WARNING) specifies INFO and WARNING levels.
  85       * A category filter is specified by an array of categories to filter. 
  86       * A message whose category name starts with any filtering category
  87       * will be returned. For example, a category filter array('System.Web','System.IO')
  88       * will return messages under categories such as 'System.Web', 'System.IO',
  89       * 'System.Web.UI', 'System.Web.UI.WebControls', etc.
  90       * A control client id filter is specified by an array of control client id
  91       * A message whose control client id starts with any filtering naming panels
  92       * will be returned. For example, a category filter array('ctl0_body_header', 
  93       * 'ctl0_body_content_sidebar')
  94       * will return messages under categories such as 'ctl0_body_header', 'ctl0_body_content_sidebar',
  95       * 'ctl0_body_header_title', 'ctl0_body_content_sidebar_savebutton', etc.
  96       * A timestamp filter is specified as an interger or float number.
  97       * A message whose registered timestamp is less or equal the filter value will be returned.
  98       * Level filter, category filter, control filter and timestamp filter are combinational, i.e., only messages
  99       * satisfying all filter conditions will they be returned.
 100       * @param integer level filter
 101       * @param array category filter
 102       * @param array control filter
 103       * @return array list of messages. Each array elements represents one message
 104       * with the following structure:
 105       * array(
 106       *   [0] => message
 107       *   [1] => level
 108       *   [2] => category
 109       *   [3] => timestamp (by microtime(), float number));
 110       *   [4] => memory in bytes
 111       *   [5] => control client id
 112       */
 113  	public function getLogs($levels=null,$categories=null,$controls=null,$timestamp=null)
 114      {
 115          $this->_levels=$levels;
 116          $this->_categories=$categories;
 117          $this->_controls=$controls;
 118          $this->_timestamp=$timestamp;
 119          if(empty($levels) && empty($categories) && empty($controls) && is_null($timestamp))
 120              return $this->_logs;
 121          $logs = $this->_logs;
 122          if(!empty($levels))
 123              $logs = array_values(array_filter( array_filter($logs,array($this,'filterByLevels')) ));
 124          if(!empty($categories))
 125              $logs = array_values(array_filter( array_filter($logs,array($this,'filterByCategories')) ));
 126          if(!empty($controls))
 127              $logs = array_values(array_filter( array_filter($logs,array($this,'filterByControl')) ));
 128          if(!is_null($timestamp))
 129              $logs = array_values(array_filter( array_filter($logs,array($this,'filterByTimeStamp')) ));
 130          return $logs;
 131      }
 132  
 133      /**
 134       * Deletes log messages from the queue.
 135       * Messages may be filtered by log levels and/or categories and/or control client ids and/or timestamp.
 136       * A level filter is specified by an integer, whose bits indicate the levels interested.
 137       * For example, (TLogger::INFO | TLogger::WARNING) specifies INFO and WARNING levels.
 138       * A category filter is specified by an array of categories to filter. 
 139       * A message whose category name starts with any filtering category
 140       * will be deleted. For example, a category filter array('System.Web','System.IO')
 141       * will delete messages under categories such as 'System.Web', 'System.IO',
 142       * 'System.Web.UI', 'System.Web.UI.WebControls', etc.
 143       * A control client id filter is specified by an array of control client id
 144       * A message whose control client id starts with any filtering naming panels
 145       * will be deleted. For example, a category filter array('ctl0_body_header', 
 146       * 'ctl0_body_content_sidebar')
 147       * will delete messages under categories such as 'ctl0_body_header', 'ctl0_body_content_sidebar',
 148       * 'ctl0_body_header_title', 'ctl0_body_content_sidebar_savebutton', etc.
 149       * A timestamp filter is specified as an interger or float number.
 150       * A message whose registered timestamp is less or equal the filter value will be returned.
 151       * Level filter, category filter, control filter and timestamp filter are combinational, i.e., only messages
 152       * satisfying all filter conditions will they be returned.
 153       * @param integer level filter
 154       * @param array category filter
 155       * @param array control filter
 156       */
 157  	public function deleteLogs($levels=null,$categories=null,$controls=null,$timestamp=null)
 158      {
 159          $this->_levels=$levels;
 160          $this->_categories=$categories;
 161          $this->_controls=$controls;
 162          $this->_timestamp=$timestamp;
 163          if(empty($levels) && empty($categories) && empty($controls) && is_null($timestamp))
 164          {
 165              $this->_logs=array();
 166              return;
 167          }
 168          $logs = $this->_logs;
 169          if(!empty($levels))
 170              $logs = array_filter( array_filter($logs,array($this,'filterByLevels')) );
 171          if(!empty($categories))
 172              $logs = array_filter( array_filter($logs,array($this,'filterByCategories')) );
 173          if(!empty($controls))
 174              $logs = array_filter( array_filter($logs,array($this,'filterByControl')) );
 175          if(!is_null($timestamp))
 176              $logs = array_filter( array_filter($logs,array($this,'filterByTimeStamp')) );
 177          $this->_logs = array_values( array_diff_key($this->_logs, $logs) );
 178      }
 179  
 180      /**
 181       * Filter function used by {@link getLogs}.
 182       * @param array element to be filtered
 183       */
 184  	private function filterByCategories($value)
 185      {
 186          foreach($this->_categories as $category)
 187          {
 188              // element 2 is the category
 189              if($value[2]===$category || strpos($value[2],$category.'.')===0)
 190                  return $value;
 191          }
 192          return false;
 193      }
 194  
 195      /**
 196       * Filter function used by {@link getLogs}
 197       * @param array element to be filtered
 198       */
 199  	private function filterByLevels($value)
 200      {
 201          // element 1 are the levels
 202          if($value[1] & $this->_levels)
 203              return $value;
 204          else
 205              return false;
 206      }
 207  
 208      /**
 209       * Filter function used by {@link getLogs}
 210       * @param array element to be filtered
 211       */
 212  	private function filterByControl($value)
 213      {
 214          // element 5 are the control client ids
 215          foreach($this->_controls as $control)
 216          {
 217              if($value[5]===$control || strpos($value[5],$control)===0)
 218                  return $value;
 219          }
 220          return false;
 221      }
 222  
 223      /**
 224       * Filter function used by {@link getLogs}
 225       * @param array element to be filtered
 226       */
 227  	private function filterByTimeStamp($value)
 228      {
 229          // element 3 is the timestamp
 230          if($value[3] <= $this->_timestamp)
 231              return $value;
 232          else
 233              return false;
 234      }
 235  }
 236  

title

Description

title

Description

title

Description

title

title

Body