LogCommand.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Grav\Plugin\Console;
  3. use Grav\Console\ConsoleCommand;
  4. use Symfony\Component\Console\Input\InputArgument;
  5. use Symfony\Component\Console\Input\InputOption;
  6. /**
  7. * Class LogCommand
  8. *
  9. * @package Grav\Plugin\Console
  10. */
  11. class LogCommand extends ConsoleCommand
  12. {
  13. /**
  14. * @var string
  15. */
  16. protected $logfile;
  17. /**
  18. * @var array
  19. */
  20. protected $options = [];
  21. /**
  22. * @var array
  23. */
  24. protected $colors = [
  25. 'DEBUG' => 'green',
  26. 'INFO' => 'cyan',
  27. 'NOTICE' => 'yellow',
  28. 'WARNING' => 'yellow',
  29. 'ERROR' => 'red',
  30. 'CRITICAL' => 'red',
  31. 'ALERT' => 'red',
  32. 'EMERGENCY' => 'magenta'
  33. ];
  34. /**
  35. *
  36. */
  37. protected function configure()
  38. {
  39. $this->logfile = LOG_DIR . 'grav.log';
  40. $this
  41. ->setName("log")
  42. ->setDescription("Outputs the Error Log")
  43. ->addOption(
  44. 'trace',
  45. 't',
  46. InputOption::VALUE_NONE,
  47. 'Include the errors stack trace in the output'
  48. )
  49. ->addOption(
  50. 'limit',
  51. 'l',
  52. InputArgument::OPTIONAL,
  53. 'Outputs only the last X amount of errors. Use as --limit 10 / -l 10 [default 5]',
  54. 5
  55. )
  56. ->setHelp('The <info>log</info> outputs the Errors Log in Console')
  57. ;
  58. }
  59. /**
  60. * @return int|null|void
  61. */
  62. protected function serve()
  63. {
  64. $this->options = [
  65. 'trace' => $this->input->getOption('trace'),
  66. 'limit' => $this->input->getOption('limit')
  67. ];
  68. if (!file_exists($this->logfile)) {
  69. $this->output->writeln("\n" . "Log file not found." . "\n");
  70. exit;
  71. }
  72. $log = file_get_contents($this->logfile);
  73. $lines = explode("\n", $log);
  74. if (!is_numeric($this->options['limit'])) {
  75. $this->options['limit'] = 5;
  76. }
  77. $lines = array_slice($lines, -($this->options['limit'] + 1));
  78. foreach ($lines as $line) {
  79. $this->output->writeln($this->parseLine($line));
  80. }
  81. }
  82. /**
  83. * @param $line
  84. *
  85. * @return null|string
  86. */
  87. protected function parseLine($line)
  88. {
  89. $bit = explode(': ', $line);
  90. $line1 = explode('] ', $bit[0]);
  91. if (!$line1[0]) {
  92. return null;
  93. }
  94. $line2 = explode(' - ', $bit[1]);
  95. $date = $line1[0] . ']';
  96. $type = str_replace('grav.', '', $line1[1]);
  97. $color = $this->colors[$type];
  98. $error = $line2[0];
  99. $trace = implode(': ', array_slice($bit, 2));
  100. $output = [];
  101. $output[] = '';
  102. $output[] = '<cyan>' . $date . '</cyan>';
  103. $output[] = sprintf(' <%s>%s</%s> <white>' . $error . '</white>', $color, $type, $color);
  104. if ($this->options['trace']) {
  105. $output[] = ' <white>TRACE:</white> ';
  106. $output[] = ' ' . $trace;
  107. }
  108. $output[] = '<cyan>' . str_repeat('-', strlen($date)) . '</cyan>';
  109. return implode("\n", $output);
  110. }
  111. }