CLogRouter.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * CLogRouter 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. * CLogRouter manages log routes that record log messages in different media.
  12. *
  13. * For example, a file log route {@link CFileLogRoute} records log messages
  14. * in log files. An email log route {@link CEmailLogRoute} sends log messages
  15. * to specific email addresses. See {@link CLogRoute} for more details about
  16. * different log routes.
  17. *
  18. * Log routes may be configured in application configuration like following:
  19. * <pre>
  20. * array(
  21. * 'preload'=>array('log'), // preload log component when app starts
  22. * 'components'=>array(
  23. * 'log'=>array(
  24. * 'class'=>'CLogRouter',
  25. * 'routes'=>array(
  26. * array(
  27. * 'class'=>'CFileLogRoute',
  28. * 'levels'=>'trace, info',
  29. * 'categories'=>'system.*',
  30. * ),
  31. * array(
  32. * 'class'=>'CEmailLogRoute',
  33. * 'levels'=>'error, warning',
  34. * 'emails'=>array('admin@example.com'),
  35. * ),
  36. * ),
  37. * ),
  38. * ),
  39. * )
  40. * </pre>
  41. *
  42. * You can specify multiple routes with different filtering conditions and different
  43. * targets, even if the routes are of the same type.
  44. *
  45. * @property array $routes The currently initialized routes.
  46. *
  47. * @author Qiang Xue <qiang.xue@gmail.com>
  48. * @package system.logging
  49. * @since 1.0
  50. */
  51. class CLogRouter extends CApplicationComponent
  52. {
  53. private $_routes=array();
  54. /**
  55. * Initializes this application component.
  56. * This method is required by the IApplicationComponent interface.
  57. */
  58. public function init()
  59. {
  60. parent::init();
  61. foreach($this->_routes as $name=>$route)
  62. {
  63. $route=Yii::createComponent($route);
  64. $route->init();
  65. $this->_routes[$name]=$route;
  66. }
  67. Yii::getLogger()->attachEventHandler('onFlush',array($this,'collectLogs'));
  68. Yii::app()->attachEventHandler('onEndRequest',array($this,'processLogs'));
  69. }
  70. /**
  71. * @return array the currently initialized routes
  72. */
  73. public function getRoutes()
  74. {
  75. return new CMap($this->_routes);
  76. }
  77. /**
  78. * @param array $config list of route configurations. Each array element represents
  79. * the configuration for a single route and has the following array structure:
  80. * <ul>
  81. * <li>class: specifies the class name or alias for the route class.</li>
  82. * <li>name-value pairs: configure the initial property values of the route.</li>
  83. * </ul>
  84. */
  85. public function setRoutes($config)
  86. {
  87. foreach($config as $name=>$route)
  88. $this->_routes[$name]=$route;
  89. }
  90. /**
  91. * Collects log messages from a logger.
  92. * This method is an event handler to the {@link CLogger::onFlush} event.
  93. * @param CEvent $event event parameter
  94. */
  95. public function collectLogs($event)
  96. {
  97. $logger=Yii::getLogger();
  98. $dumpLogs=isset($event->params['dumpLogs']) && $event->params['dumpLogs'];
  99. foreach($this->_routes as $route)
  100. {
  101. /* @var $route CLogRoute */
  102. if($route->enabled)
  103. $route->collectLogs($logger,$dumpLogs);
  104. }
  105. }
  106. /**
  107. * Collects and processes log messages from a logger.
  108. * This method is an event handler to the {@link CApplication::onEndRequest} event.
  109. * @since 1.1.0
  110. */
  111. public function processLogs()
  112. {
  113. $logger=Yii::getLogger();
  114. $logger->flush(true);
  115. }
  116. }