123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. foreach($messages as &$message) {
  59. // exceptions may not be serializable if in the call stack somewhere is a Closure
  60. if ($message[0] instanceof \Throwable || $message[0] instanceof \Exception) {
  61. $message[0] = (string) $message[0];
  62. }
  63. }
  64. return ['messages' => $messages];
  65. }
  66. /**
  67. * Returns an array of models that represents logs of the current request.
  68. * Can be used with data providers, such as \yii\data\ArrayDataProvider.
  69. *
  70. * @param bool $refresh if need to build models from log messages and refresh them.
  71. * @return array models
  72. */
  73. protected function getModels($refresh = false)
  74. {
  75. if ($this->_models === null || $refresh) {
  76. $this->_models = [];
  77. foreach ($this->data['messages'] as $message) {
  78. $this->_models[] = [
  79. 'message' => $message[0],
  80. 'level' => $message[1],
  81. 'category' => $message[2],
  82. 'time' => ($message[3] * 1000), // time in milliseconds
  83. 'trace' => $message[4]
  84. ];
  85. }
  86. }
  87. return $this->_models;
  88. }
  89. }