ConsoleLogger   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A log() 0 32 4
A __construct() 0 5 1
A padPrefix() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cecil\Logger;
15
16
use Psr\Log\InvalidArgumentException;
17
use Psr\Log\LogLevel;
18
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
/**
22
 * Console logger class.
23
 *
24
 * This logger prints messages to the console output.
25
 * It supports different log levels and can be configured to print messages
26
 * up to a certain verbosity level.
27
 */
28
class ConsoleLogger extends PrintLogger
29
{
30
    public const ERROR = 'error';
31
    public const WARNING = 'comment';
32
    public const NOTICE = 'info';
33
    public const INFO = 'text';
34
    public const DEBUG = 'debug';
35
36
    protected $output;
37
38
    protected $verbosityLevelMap = [
39
        LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
40
        LogLevel::ALERT     => OutputInterface::VERBOSITY_NORMAL,
41
        LogLevel::CRITICAL  => OutputInterface::VERBOSITY_NORMAL,
42
        LogLevel::ERROR     => OutputInterface::VERBOSITY_NORMAL,
43
        LogLevel::WARNING   => OutputInterface::VERBOSITY_VERY_VERBOSE,
44
        LogLevel::NOTICE    => OutputInterface::VERBOSITY_VERBOSE,
45
        LogLevel::INFO      => OutputInterface::VERBOSITY_VERY_VERBOSE,
46
        LogLevel::DEBUG     => OutputInterface::VERBOSITY_DEBUG,
47
    ];
48
49
    protected $formatLevelMap = [
50
        LogLevel::EMERGENCY => self::ERROR,
51
        LogLevel::ALERT     => self::ERROR,
52
        LogLevel::CRITICAL  => self::ERROR,
53
        LogLevel::ERROR     => self::ERROR,
54
        LogLevel::WARNING   => self::WARNING,
55
        LogLevel::NOTICE    => self::NOTICE,
56
        LogLevel::INFO      => self::INFO,
57
        LogLevel::DEBUG     => self::DEBUG,
58
    ];
59
60
    public function __construct(OutputInterface $output, array $verbosityLevelMap = [], array $formatLevelMap = [])
61
    {
62
        $this->output = $output;
63
        $this->verbosityLevelMap = $verbosityLevelMap + $this->verbosityLevelMap;
64
        $this->formatLevelMap = $formatLevelMap + $this->formatLevelMap;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function log($level, string|\Stringable $message, array $context = []): void
71
    {
72
        $output = $this->output;
73
        $output->getFormatter()->setStyle('text', new OutputFormatterStyle('white'));
74
        $output->getFormatter()->setStyle('debug', new OutputFormatterStyle('blue', 'yellow'));
75
76
        if (!isset($this->verbosityLevelMap[$level])) {
77
            throw new InvalidArgumentException(\sprintf('The log level "%s" does not exist.', $level));
78
        }
79
80
        // default pattern: <level>message</level>
81
        $pattern = '<%1$s>%2$s%3$s</%1$s>';
82
        $prefix = '';
83
84
        // steps prefix
85
        if (isset($context['step'])) {
86
            $prefix = \sprintf('%s. ', $this->padPrefix((string) $context['step'][0], (string) $context['step'][1]));
87
        }
88
89
        // sub steps progress
90
        if (isset($context['progress'])) {
91
            // prefix
92
            $prefix = \sprintf(
93
                '[%s/%s] ',
94
                $this->padPrefix((string) $context['progress'][0], (string) $context['progress'][1]),
95
                $context['progress'][1]
96
            );
97
        }
98
99
        $output->writeln(
100
            \sprintf($pattern, $this->formatLevelMap[$level], $prefix, $this->interpolate($message, $context)),
101
            $this->verbosityLevelMap[$level]
102
        );
103
    }
104
105
    /**
106
     * Prefix padding.
107
     */
108
    private function padPrefix(string $prefix, string $max): string
109
    {
110
        return str_pad($prefix, \strlen($max), ' ', STR_PAD_LEFT);
111
    }
112
}
113