| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
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 |
namespace Symfony\Component\Console\Logger;
|
|
|
13 |
|
|
|
14 |
use Psr\Log\AbstractLogger;
|
|
|
15 |
use Psr\Log\InvalidArgumentException;
|
|
|
16 |
use Psr\Log\LogLevel;
|
|
|
17 |
use Symfony\Component\Console\Output\ConsoleOutputInterface;
|
|
|
18 |
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* PSR-3 compliant console logger.
|
|
|
22 |
*
|
|
|
23 |
* @author Kévin Dunglas <dunglas@gmail.com>
|
|
|
24 |
*
|
|
|
25 |
* @see https://www.php-fig.org/psr/psr-3/
|
|
|
26 |
*/
|
|
|
27 |
class ConsoleLogger extends AbstractLogger
|
|
|
28 |
{
|
|
|
29 |
public const INFO = 'info';
|
|
|
30 |
public const ERROR = 'error';
|
|
|
31 |
|
|
|
32 |
private OutputInterface $output;
|
|
|
33 |
private array $verbosityLevelMap = [
|
|
|
34 |
LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
|
|
|
35 |
LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
|
|
|
36 |
LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
|
|
|
37 |
LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL,
|
|
|
38 |
LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
|
|
|
39 |
LogLevel::NOTICE => OutputInterface::VERBOSITY_VERBOSE,
|
|
|
40 |
LogLevel::INFO => OutputInterface::VERBOSITY_VERY_VERBOSE,
|
|
|
41 |
LogLevel::DEBUG => OutputInterface::VERBOSITY_DEBUG,
|
|
|
42 |
];
|
|
|
43 |
private array $formatLevelMap = [
|
|
|
44 |
LogLevel::EMERGENCY => self::ERROR,
|
|
|
45 |
LogLevel::ALERT => self::ERROR,
|
|
|
46 |
LogLevel::CRITICAL => self::ERROR,
|
|
|
47 |
LogLevel::ERROR => self::ERROR,
|
|
|
48 |
LogLevel::WARNING => self::INFO,
|
|
|
49 |
LogLevel::NOTICE => self::INFO,
|
|
|
50 |
LogLevel::INFO => self::INFO,
|
|
|
51 |
LogLevel::DEBUG => self::INFO,
|
|
|
52 |
];
|
|
|
53 |
private bool $errored = false;
|
|
|
54 |
|
|
|
55 |
public function __construct(OutputInterface $output, array $verbosityLevelMap = [], array $formatLevelMap = [])
|
|
|
56 |
{
|
|
|
57 |
$this->output = $output;
|
|
|
58 |
$this->verbosityLevelMap = $verbosityLevelMap + $this->verbosityLevelMap;
|
|
|
59 |
$this->formatLevelMap = $formatLevelMap + $this->formatLevelMap;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
public function log($level, $message, array $context = []): void
|
|
|
63 |
{
|
|
|
64 |
if (!isset($this->verbosityLevelMap[$level])) {
|
|
|
65 |
throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level));
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
$output = $this->output;
|
|
|
69 |
|
|
|
70 |
// Write to the error output if necessary and available
|
|
|
71 |
if (self::ERROR === $this->formatLevelMap[$level]) {
|
|
|
72 |
if ($this->output instanceof ConsoleOutputInterface) {
|
|
|
73 |
$output = $output->getErrorOutput();
|
|
|
74 |
}
|
|
|
75 |
$this->errored = true;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
// the if condition check isn't necessary -- it's the same one that $output will do internally anyway.
|
|
|
79 |
// We only do it for efficiency here as the message formatting is relatively expensive.
|
|
|
80 |
if ($output->getVerbosity() >= $this->verbosityLevelMap[$level]) {
|
|
|
81 |
$output->writeln(sprintf('<%1$s>[%2$s] %3$s</%1$s>', $this->formatLevelMap[$level], $level, $this->interpolate($message, $context)), $this->verbosityLevelMap[$level]);
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Returns true when any messages have been logged at error levels.
|
|
|
87 |
*/
|
|
|
88 |
public function hasErrored(): bool
|
|
|
89 |
{
|
|
|
90 |
return $this->errored;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Interpolates context values into the message placeholders.
|
|
|
95 |
*
|
|
|
96 |
* @author PHP Framework Interoperability Group
|
|
|
97 |
*/
|
|
|
98 |
private function interpolate(string $message, array $context): string
|
|
|
99 |
{
|
|
|
100 |
if (!str_contains($message, '{')) {
|
|
|
101 |
return $message;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
$replacements = [];
|
|
|
105 |
foreach ($context as $key => $val) {
|
|
|
106 |
if (null === $val || \is_scalar($val) || $val instanceof \Stringable) {
|
|
|
107 |
$replacements["{{$key}}"] = $val;
|
|
|
108 |
} elseif ($val instanceof \DateTimeInterface) {
|
|
|
109 |
$replacements["{{$key}}"] = $val->format(\DateTimeInterface::RFC3339);
|
|
|
110 |
} elseif (\is_object($val)) {
|
|
|
111 |
$replacements["{{$key}}"] = '[object '.$val::class.']';
|
|
|
112 |
} else {
|
|
|
113 |
$replacements["{{$key}}"] = '['.\gettype($val).']';
|
|
|
114 |
}
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
return strtr($message, $replacements);
|
|
|
118 |
}
|
|
|
119 |
}
|