DiagnosticLogger::log()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 12
    public function log($level, $message, array $context = [])
19
    {
20 12
        $this->logs[] = [$level, $message, $context];
21 12
    }
22
23 2
    public function dumpConsole($useColours = true)
24
    {
25
        $colours = [
26 2
            '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 2
        $reset = $useColours ? "\e[0m" : '';
37
38 2
        foreach ($this->logs as $log) {
39 2
            $col = $useColours ? $colours[$log[0]] : '';
40 2
            echo $col . $log[0] . ': ' . $this->interpolateMessage($log[1], $log[2]) . $reset . "\n";
41
        }
42 2
    }
43
44 2
    public function dumpHTML($echo = true)
45
    {
46 2
        $html = '<div class="liblynx-diagnostic-log">';
47 2
        $html .= '<table class="table"><thead><tr><th>Level</th><th>Message</th></tr></thead><tbody>';
48 2
        $html .= "\n";
49
50 2
        foreach ($this->logs as $log) {
51 2
            $html .= '<tr class="level-' . $log[0] . '"><td>' . $log[0] . '</td><td>' .
52 2
                htmlentities($this->interpolateMessage($log[1], $log[2])) .
53 2
                "</td></tr>\n";
54
        }
55 2
        $html .= "</tbody></table></div>\n";
56
57 2
        if ($echo) {
58
            echo $html; //@codeCoverageIgnore
59
        }
60 2
        return $html;
61
    }
62
63
    /**
64
     * Interpolates context values into the message placeholders.
65
     */
66 2
    private function interpolateMessage($message, array $context = [])
67
    {
68
        // build a replacement array with braces around the context keys
69 2
        $replace = [];
70 2
        foreach ($context as $key => $val) {
71
            // check that the value can be casted to string
72 2
            if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
73 2
                $replace['{' . $key . '}'] = $val;
74
            }
75
        }
76
77
        // interpolate replacement values into the message and return
78 2
        return strtr($message, $replace);
79
    }
80
81
82 2
    public function cleanLogs()
83
    {
84 2
        $logs = $this->logs;
85 2
        $this->logs = [];
86
87 2
        return $logs;
88
    }
89
90 2
    public function countLogs($level)
91
    {
92 2
        $count = 0;
93 2
        foreach ($this->logs as $log) {
94 2
            if ($log[0] == $level) {
95 2
                $count++;
96
            }
97
        }
98 2
        return $count;
99
    }
100
}
101