Issues (3)

src/VPA/Logger/JSConsoleLogger.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VPA\Logger;
6
7
use Psr\Log\LogLevel;
8
use VPA\DI\Injectable;
9
use \Stringable;
0 ignored issues
show
The type \Stringable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
#[Injectable]
12
class JSConsoleLogger extends BaseLogger
13
{
14
    /**
15
     * Table information.
16
     *
17
     * @param string | Stringable $message
18
     * @param array $context
19
     * @return void
20
     */
21
    public function table(string | Stringable $message, array $context = []): void
22
    {
23
        $this->log('table', $message, $context);
24
    }
25
26
    /**
27
     * Logs with an arbitrary level.
28
     *
29
     * @param mixed $level
30
     * @param string | Stringable $message
31
     * @param array $context
32
     * @return void
33
     */
34 9
    public function log(mixed $level, string | Stringable $message, array $context = []): void
35
    {
36 9
        $text = $this->interpolate($message, $context);
37 9
        $jsMethod = match ($level) {
38 1
            LogLevel::ERROR => 'error',
39 1
            LogLevel::DEBUG => 'debug',
40 1
            LogLevel::INFO => 'info',
41 1
            LogLevel::WARNING => 'warn',
42 5
            default => 'log',
43
        };
44
45 9
        echo "<script>".printf(
46
            "console.%s('%s [%s] %s');",
47
            $jsMethod,
48 9
            date('y-m-d H:i:s'),
49
            $level,
50 9
            $this->stringFormat($text),
51
        )."</script>";
52
    }
53
54 9
    private function stringFormat(string $input): string
55
    {
56 9
        $output = addcslashes($input, "`'\"");
57 9
        $output = str_replace("\n", "\\n", $output);
58
        //$output= str_replace("\\","\\\\", $output);
59 9
        return $output;
60
    }
61
}
62