You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.4KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\debug\panels;
  8. use Yii;
  9. use yii\debug\Panel;
  10. use yii\log\Logger;
  11. use yii\debug\models\search\Log;
  12. /**
  13. * Debugger panel that collects and displays logs.
  14. *
  15. * @author Qiang Xue <qiang.xue@gmail.com>
  16. * @since 2.0
  17. */
  18. class LogPanel extends Panel
  19. {
  20. /**
  21. * @var array log messages extracted to array as models, to use with data provider.
  22. */
  23. private $_models;
  24. /**
  25. * @inheritdoc
  26. */
  27. public function getName()
  28. {
  29. return 'Logs';
  30. }
  31. /**
  32. * @inheritdoc
  33. */
  34. public function getSummary()
  35. {
  36. return Yii::$app->view->render('panels/log/summary', ['data' => $this->data, 'panel' => $this]);
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function getDetail()
  42. {
  43. $searchModel = new Log();
  44. $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams(), $this->getModels());
  45. return Yii::$app->view->render('panels/log/detail', [
  46. 'dataProvider' => $dataProvider,
  47. 'panel' => $this,
  48. 'searchModel' => $searchModel,
  49. ]);
  50. }
  51. /**
  52. * @inheritdoc
  53. */
  54. public function save()
  55. {
  56. $target = $this->module->logTarget;
  57. $messages = $target->filterMessages($target->messages, Logger::LEVEL_ERROR | Logger::LEVEL_INFO | Logger::LEVEL_WARNING | Logger::LEVEL_TRACE);
  58. return ['messages' => $messages];
  59. }
  60. /**
  61. * Returns an array of models that represents logs of the current request.
  62. * Can be used with data providers, such as \yii\data\ArrayDataProvider.
  63. *
  64. * @param boolean $refresh if need to build models from log messages and refresh them.
  65. * @return array models
  66. */
  67. protected function getModels($refresh = false)
  68. {
  69. if ($this->_models === null || $refresh) {
  70. $this->_models = [];
  71. foreach ($this->data['messages'] as $message) {
  72. $this->_models[] = [
  73. 'message' => $message[0],
  74. 'level' => $message[1],
  75. 'category' => $message[2],
  76. 'time' => ($message[3] * 1000), // time in milliseconds
  77. 'trace' => $message[4]
  78. ];
  79. }
  80. }
  81. return $this->_models;
  82. }
  83. }