CLogRoute.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * CLogRoute class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 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. * @package system.logging
  28. * @since 1.0
  29. */
  30. abstract class CLogRoute extends CComponent
  31. {
  32. /**
  33. * @var boolean whether to enable this log route. Defaults to true.
  34. */
  35. public $enabled=true;
  36. /**
  37. * @var string list of levels separated by comma or space. Defaults to empty, meaning all levels.
  38. */
  39. public $levels='';
  40. /**
  41. * @var mixed array of categories, or string list separated by comma or space.
  42. * Defaults to empty array, meaning all categories.
  43. */
  44. public $categories=array();
  45. /**
  46. * @var mixed array of categories, or string list separated by comma or space, to EXCLUDE from logs.
  47. * Defaults to empty array, meaning no categories are excluded.
  48. * This will exclude any categories after $categories has been ran.
  49. */
  50. public $except=array();
  51. /**
  52. * @var mixed the additional filter (eg {@link CLogFilter}) that can be applied to the log messages.
  53. * The value of this property will be passed to {@link Yii::createComponent} to create
  54. * a log filter object. As a result, this can be either a string representing the
  55. * filter class name or an array representing the filter configuration.
  56. * In general, the log filter class should implement {@link ILogFilter} interface.
  57. * If you want to apply multiple filters you can use {@link CChainedLogFilter} to do so.
  58. * Defaults to null, meaning no filter will be used.
  59. */
  60. public $filter;
  61. /**
  62. * @var array the logs that are collected so far by this log route.
  63. * @since 1.1.0
  64. */
  65. public $logs=array();
  66. /**
  67. * Initializes the route.
  68. * This method is invoked after the route is created by the route manager.
  69. */
  70. public function init()
  71. {
  72. }
  73. /**
  74. * Formats a log message given different fields.
  75. * @param string $message message content
  76. * @param integer $level message level
  77. * @param string $category message category
  78. * @param integer $time timestamp
  79. * @return string formatted message
  80. */
  81. protected function formatLogMessage($message,$level,$category,$time)
  82. {
  83. return @date('Y/m/d H:i:s',$time)." [$level] [$category] $message\n";
  84. }
  85. /**
  86. * Retrieves filtered log messages from logger for further processing.
  87. * @param CLogger $logger logger instance
  88. * @param boolean $processLogs whether to process the logs after they are collected from the logger
  89. */
  90. public function collectLogs($logger, $processLogs=false)
  91. {
  92. $logs=$logger->getLogs($this->levels,$this->categories,$this->except);
  93. $this->logs=empty($this->logs) ? $logs : array_merge($this->logs,$logs);
  94. if($processLogs && !empty($this->logs))
  95. {
  96. if($this->filter!==null)
  97. Yii::createComponent($this->filter)->filter($this->logs);
  98. if($this->logs!==array())
  99. $this->processLogs($this->logs);
  100. $this->logs=array();
  101. }
  102. }
  103. /**
  104. * Processes log messages and sends them to specific destination.
  105. * Derived child classes must implement this method.
  106. * @param array $logs list of messages. Each array element represents one message
  107. * with the following structure:
  108. * array(
  109. * [0] => message (string)
  110. * [1] => level (string)
  111. * [2] => category (string)
  112. * [3] => timestamp (float, obtained by microtime(true));
  113. */
  114. abstract protected function processLogs($logs);
  115. }