|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zwartpet\PHPCertificateToolbox; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Log\AbstractLogger; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* A PSR-3 logger you can use for troubleshooting (note you can use any PSR-3 compatible logger) |
|
9
|
|
|
* |
|
10
|
|
|
* This retains logs in memory and you can dump them when it suits you. In a console app |
|
11
|
|
|
* use dumpConsole() which will output colour-coded logs (pass false to disable this). In a web app, |
|
12
|
|
|
* you can use dumpHTML() to output or obtain an HTML rendering of the logs. |
|
13
|
|
|
*/ |
|
14
|
|
|
class DiagnosticLogger extends AbstractLogger |
|
15
|
|
|
{ |
|
16
|
|
|
private $logs = []; |
|
17
|
|
|
|
|
18
|
|
|
public function log($level, $message, array $context = []) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->logs[] = [$level, $message, $context]; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function dumpConsole($useColours = true) |
|
24
|
|
|
{ |
|
25
|
|
|
$colours = [ |
|
26
|
|
|
'alert' => "\e[97m\e[41m", |
|
27
|
|
|
'emergency' => "\e[97m\e[41m", |
|
28
|
|
|
'critical' => "\e[97m\e[41m", |
|
29
|
|
|
'error' => "\e[91m", |
|
30
|
|
|
'warning' => "\e[93m", |
|
31
|
|
|
'notice' => "\e[96m", |
|
32
|
|
|
'info' => "\e[92m", |
|
33
|
|
|
'debug' => "\e[2m", |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
$reset = $useColours ? "\e[0m" : ''; |
|
37
|
|
|
|
|
38
|
|
|
foreach ($this->logs as $log) { |
|
39
|
|
|
$col = $useColours ? $colours[$log[0]] : ''; |
|
40
|
|
|
echo $col . $log[0] . ': ' . $this->interpolateMessage($log[1], $log[2]) . $reset . "\n"; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function dumpHTML($echo = true) |
|
45
|
|
|
{ |
|
46
|
|
|
$html = '<div class="liblynx-diagnostic-log">'; |
|
47
|
|
|
$html .= '<table class="table"><thead><tr><th>Level</th><th>Message</th></tr></thead><tbody>'; |
|
48
|
|
|
$html .= "\n"; |
|
49
|
|
|
|
|
50
|
|
|
foreach ($this->logs as $log) { |
|
51
|
|
|
$html .= '<tr class="level-' . $log[0] . '"><td>' . $log[0] . '</td><td>' . |
|
52
|
|
|
htmlentities($this->interpolateMessage($log[1], $log[2])) . |
|
53
|
|
|
"</td></tr>\n"; |
|
54
|
|
|
} |
|
55
|
|
|
$html .= "</tbody></table></div>\n"; |
|
56
|
|
|
|
|
57
|
|
|
if ($echo) { |
|
58
|
|
|
echo $html; //@codeCoverageIgnore |
|
59
|
|
|
} |
|
60
|
|
|
return $html; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Interpolates context values into the message placeholders. |
|
65
|
|
|
*/ |
|
66
|
|
|
private function interpolateMessage($message, array $context = []) |
|
67
|
|
|
{ |
|
68
|
|
|
// build a replacement array with braces around the context keys |
|
69
|
|
|
$replace = []; |
|
70
|
|
|
foreach ($context as $key => $val) { |
|
71
|
|
|
// check that the value can be casted to string |
|
72
|
|
|
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) { |
|
73
|
|
|
$replace['{' . $key . '}'] = $val; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// interpolate replacement values into the message and return |
|
78
|
|
|
return strtr($message, $replace); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
public function cleanLogs() |
|
83
|
|
|
{ |
|
84
|
|
|
$logs = $this->logs; |
|
85
|
|
|
$this->logs = []; |
|
86
|
|
|
|
|
87
|
|
|
return $logs; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function countLogs($level) |
|
91
|
|
|
{ |
|
92
|
|
|
$count = 0; |
|
93
|
|
|
foreach ($this->logs as $log) { |
|
94
|
|
|
if ($log[0] == $level) { |
|
95
|
|
|
$count++; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
return $count; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|