Passed
Push — main ( 5f2e15...5f3314 )
by ANDREY
04:34 queued 01:51
created

JSConsoleLogger::stringFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VPA\Logger;
6
7
use Psr\Log\AbstractLogger;
8
use Psr\Log\LogLevel;
9
use VPA\DI\Container;
10
use VPA\DI\Injectable;
11
12
#[Injectable()]
13
class JSConsoleLogger extends BaseLogger
14
{
15
    /**
16
     * Table information.
17
     *
18
     * @param string $message
19
     * @param array $context
20
     * @return void
21
     */
22 1
    public function table(string|\Stringable $message, array $context = []): void
23
    {
24 1
        $this->log('table', $message, $context);
25
    }
26
27
    /**
28
     * Logs with an arbitrary level.
29
     *
30
     * @param mixed $level
31
     * @param string $message
32
     * @param array $context
33
     * @return void
34
     */
35 10
    public function log(mixed $level, string|\Stringable $message, array $context = []): void
36
    {
37 10
        $text = $this->interpolate($message, $context);
38 10
        switch ($level) {
39
            case LogLevel::ERROR:
40 1
                printf(
41
                    "<script>console.error('%s [%s] %s');</script>",
42 1
                    date('y-m-d H:i:s'),
43
                    $level,
44 1
                    $this->stringFormat($text)
45
                );
46 1
                break;
47
            case LogLevel::DEBUG:
48 1
                printf(
49
                    "<script>console.debug('%s [%s] %s');</script>",
50 1
                    date('y-m-d H:i:s'),
51
                    $level,
52 1
                    $this->stringFormat($text)
53
                );
54 1
                break;
55
            case LogLevel::INFO:
56 1
                printf(
57
                    "<script>console.info('%s [%s] %s');</script>",
58 1
                    date('y-m-d H:i:s'),
59
                    $level,
60 1
                    $this->stringFormat($text)
61
                );
62 1
                break;
63
            case LogLevel::WARNING:
64 1
                printf(
65
                    "<script>console.warn('%s [%s] %s');</script>",
66 1
                    date('y-m-d H:i:s'),
67
                    $level,
68 1
                    $this->stringFormat($text)
69
                );
70 1
                break;
71 6
            case 'table':
72 1
                printf(
73
                    "<script>console.debug('%s [%s] %s');console.table(%s);</script>",
74 1
                    date('y-m-d H:i:s'),
75
                    $level,
76 1
                    $this->stringFormat($message),
77 1
                    json_encode($context)
78
                );
79 1
                break;
80
            default:
81 5
                printf(
82
                    "<script>console.log('%s [%s] %s');</script>",
83 5
                    date('y-m-d H:i:s'),
84
                    $level,
85 5
                    $this->stringFormat($text)
86
                );
87
        }
88
    }
89
90 10
    private function stringFormat(string $input): string
91
    {
92 10
        $output = addcslashes($input, "`'\"");
93 10
        $output = str_replace("\n", "\\n", $output);
94
        //$output= str_replace("\\","\\\\", $output);
95 10
        return $output;
96
    }
97
}
98