1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Monolog Extensions |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @see http://github.com/graze/MonologExtensions/blob/master/LICENSE |
11
|
|
|
* @link http://github.com/graze/MonologExtensions |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\Monolog\Formatter; |
15
|
|
|
|
16
|
|
|
use Monolog\Formatter\NormalizerFormatter; |
17
|
|
|
use Monolog\Logger; |
18
|
|
|
|
19
|
|
|
class ConsoleFormatter extends NormalizerFormatter |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Translates Monolog log levels to colour |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $logLevelToColour = [ |
27
|
|
|
Logger::DEBUG => ['1;37', '0;36'], // Cyan |
28
|
|
|
Logger::INFO => ['1;37', '0;32'], // Green |
29
|
|
|
Logger::NOTICE => ['1;37', '1;33'], // Yellow |
30
|
|
|
Logger::WARNING => ['1;37', '0;35'], // Purple |
31
|
|
|
Logger::ERROR => ['1;37', '0;31'], // Red |
32
|
|
|
Logger::CRITICAL => ['0;30', '43'], // Black/Yellow |
33
|
|
|
Logger::ALERT => ['1;37', '45'], // White/Purple |
34
|
|
|
Logger::EMERGENCY => ['1;37', '41'], // White/Red |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $columnLengthMax = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
private $rows = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $dateFormat The format of the timestamp: one supported by DateTime::format |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function __construct($dateFormat = null) |
53
|
|
|
{ |
54
|
|
|
parent::__construct($dateFormat); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Formats a log record. |
59
|
|
|
* |
60
|
|
|
* @param array $record |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function format(array $record) |
65
|
|
|
{ |
66
|
|
|
$this->addRow($record['level_name'], $record['level']); |
67
|
|
|
$this->addRow('Message', (string) $record['message']); |
68
|
|
|
$this->addRow('Time', $record['datetime']->format($this->dateFormat)); |
69
|
|
|
$this->addRow('Channel', $record['channel']); |
70
|
|
|
|
71
|
|
|
if (isset($record['context']['file'])) { |
72
|
|
|
$this->addRow('File', $record['context']['file']); |
73
|
|
|
$this->addRow('Line', $record['context']['line']); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (isset($record['context']['exception'])) { |
77
|
|
|
$trace = explode(PHP_EOL, $record['context']['exception']->getTraceAsString()); |
78
|
|
|
$this->addRow('Trace', array_shift($trace)); |
79
|
|
|
foreach ($trace as $row) { |
80
|
|
|
$this->addRow('', $row); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->render($this->getColoursByLogLevel($record['level'])); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $columnString,... unlimited OPTIONAL |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
protected function addRow() |
93
|
|
|
{ |
94
|
|
|
$arguments = func_get_args(); |
95
|
|
|
|
96
|
|
|
// store the longest column so we can pad out to it on render |
97
|
|
|
foreach ($arguments as $key => $value) { |
98
|
|
|
$columnLength = strlen($value); |
99
|
|
|
|
100
|
|
|
if (!isset($this->columnLengthMax[$key]) || $columnLength > $this->columnLengthMax[$key]) { |
101
|
|
|
$this->columnLengthMax[$key] = $columnLength; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->rows[] = $arguments; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param array $colours |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
protected function render(array $colours) |
114
|
|
|
{ |
115
|
|
|
$separator = '+'; |
116
|
|
|
|
117
|
|
|
foreach ($this->columnLengthMax as $columnLength) { |
118
|
|
|
$separator .= str_repeat('-', $columnLength + 2) . '+'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$output = $separator; |
122
|
|
|
|
123
|
|
|
foreach ($this->rows as $row) { |
124
|
|
|
$output .= PHP_EOL . '|'; |
125
|
|
|
|
126
|
|
|
foreach ($this->columnLengthMax as $key => $null) { |
127
|
|
|
$cellContent = isset($row[$key]) ? $row[$key] : ''; |
128
|
|
|
|
129
|
|
|
$output .= ' ' . str_pad($cellContent, $this->columnLengthMax[$key]) . ' |'; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// flush rows |
134
|
|
|
$this->rows = []; |
135
|
|
|
|
136
|
|
|
// Create the coloured string |
137
|
|
|
return "\n\033[{$colours[0]}m\033[{$colours[1]}m" . $output . PHP_EOL . $separator . PHP_EOL . "\033[0m\n"; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param int $logLevel |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
protected function getColoursByLogLevel($logLevel) |
146
|
|
|
{ |
147
|
|
|
return $this->logLevelToColour[$logLevel]; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|