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