/yii/framework/logging/CLogRoute.php

https://github.com/joshuaswarren/weatherhub · PHP · 115 lines · 29 code · 6 blank · 80 comment · 3 complexity · 39460d9fca496e7a6b860dc7b7b2bc05 MD5 · raw file

  1. <?php
  2. /**
  3. * CLogRoute class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2011 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CLogRoute is the base class for all log route classes.
  12. *
  13. * A log route object retrieves log messages from a logger and sends it
  14. * somewhere, such as files, emails.
  15. * The messages being retrieved may be filtered first before being sent
  16. * to the destination. The filters include log level filter and log category filter.
  17. *
  18. * To specify level filter, set {@link levels} property,
  19. * which takes a string of comma-separated desired level names (e.g. 'Error, Debug').
  20. * To specify category filter, set {@link categories} property,
  21. * which takes a string of comma-separated desired category names (e.g. 'System.Web, System.IO').
  22. *
  23. * Level filter and category filter are combinational, i.e., only messages
  24. * satisfying both filter conditions will they be returned.
  25. *
  26. * @author Qiang Xue <qiang.xue@gmail.com>
  27. * @version $Id: CLogRoute.php 3205 2011-05-07 23:16:46Z qiang.xue $
  28. * @package system.logging
  29. * @since 1.0
  30. */
  31. abstract class CLogRoute extends CComponent
  32. {
  33. /**
  34. * @var boolean whether to enable this log route. Defaults to true.
  35. * @since 1.0.7
  36. */
  37. public $enabled=true;
  38. /**
  39. * @var string list of levels separated by comma or space. Defaults to empty, meaning all levels.
  40. */
  41. public $levels='';
  42. /**
  43. * @var string list of categories separated by comma or space. Defaults to empty, meaning all categories.
  44. */
  45. public $categories='';
  46. /**
  47. * @var mixed the additional filter (eg {@link CLogFilter}) that can be applied to the log messages.
  48. * The value of this property will be passed to {@link Yii::createComponent} to create
  49. * a log filter object. As a result, this can be either a string representing the
  50. * filter class name or an array representing the filter configuration.
  51. * In general, the log filter class should be {@link CLogFilter} or a child class of it.
  52. * Defaults to null, meaning no filter will be used.
  53. * @since 1.0.6
  54. */
  55. public $filter;
  56. /**
  57. * @var array the logs that are collected so far by this log route.
  58. * @since 1.1.0
  59. */
  60. public $logs;
  61. /**
  62. * Initializes the route.
  63. * This method is invoked after the route is created by the route manager.
  64. */
  65. public function init()
  66. {
  67. }
  68. /**
  69. * Formats a log message given different fields.
  70. * @param string $message message content
  71. * @param integer $level message level
  72. * @param string $category message category
  73. * @param integer $time timestamp
  74. * @return string formatted message
  75. */
  76. protected function formatLogMessage($message,$level,$category,$time)
  77. {
  78. return @date('Y/m/d H:i:s',$time)." [$level] [$category] $message\n";
  79. }
  80. /**
  81. * Retrieves filtered log messages from logger for further processing.
  82. * @param CLogger $logger logger instance
  83. * @param boolean $processLogs whether to process the logs after they are collected from the logger
  84. */
  85. public function collectLogs($logger, $processLogs=false)
  86. {
  87. $logs=$logger->getLogs($this->levels,$this->categories);
  88. $this->logs=empty($this->logs) ? $logs : array_merge($this->logs,$logs);
  89. if($processLogs && !empty($this->logs))
  90. {
  91. if($this->filter!==null)
  92. Yii::createComponent($this->filter)->filter($this->logs);
  93. $this->processLogs($this->logs);
  94. $this->logs=array();
  95. }
  96. }
  97. /**
  98. * Processes log messages and sends them to specific destination.
  99. * Derived child classes must implement this method.
  100. * @param array $logs list of messages. Each array elements represents one message
  101. * with the following structure:
  102. * array(
  103. * [0] => message (string)
  104. * [1] => level (string)
  105. * [2] => category (string)
  106. * [3] => timestamp (float, obtained by microtime(true));
  107. */
  108. abstract protected function processLogs($logs);
  109. }