diff options
Diffstat (limited to 'vendor/symfony/console/Logger/ConsoleLogger.php')
| -rw-r--r-- | vendor/symfony/console/Logger/ConsoleLogger.php | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/vendor/symfony/console/Logger/ConsoleLogger.php b/vendor/symfony/console/Logger/ConsoleLogger.php new file mode 100644 index 0000000..ad6e49c --- /dev/null +++ b/vendor/symfony/console/Logger/ConsoleLogger.php | |||
| @@ -0,0 +1,120 @@ | |||
| 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 array $verbosityLevelMap = [ | ||
| 33 | LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL, | ||
| 34 | LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL, | ||
| 35 | LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL, | ||
| 36 | LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL, | ||
| 37 | LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL, | ||
| 38 | LogLevel::NOTICE => OutputInterface::VERBOSITY_VERBOSE, | ||
| 39 | LogLevel::INFO => OutputInterface::VERBOSITY_VERY_VERBOSE, | ||
| 40 | LogLevel::DEBUG => OutputInterface::VERBOSITY_DEBUG, | ||
| 41 | ]; | ||
| 42 | private array $formatLevelMap = [ | ||
| 43 | LogLevel::EMERGENCY => self::ERROR, | ||
| 44 | LogLevel::ALERT => self::ERROR, | ||
| 45 | LogLevel::CRITICAL => self::ERROR, | ||
| 46 | LogLevel::ERROR => self::ERROR, | ||
| 47 | LogLevel::WARNING => self::INFO, | ||
| 48 | LogLevel::NOTICE => self::INFO, | ||
| 49 | LogLevel::INFO => self::INFO, | ||
| 50 | LogLevel::DEBUG => self::INFO, | ||
| 51 | ]; | ||
| 52 | private bool $errored = false; | ||
| 53 | |||
| 54 | public function __construct( | ||
| 55 | private OutputInterface $output, | ||
| 56 | array $verbosityLevelMap = [], | ||
| 57 | array $formatLevelMap = [], | ||
| 58 | ) { | ||
| 59 | $this->verbosityLevelMap = $verbosityLevelMap + $this->verbosityLevelMap; | ||
| 60 | $this->formatLevelMap = $formatLevelMap + $this->formatLevelMap; | ||
| 61 | } | ||
| 62 | |||
| 63 | public function log($level, $message, array $context = []): void | ||
| 64 | { | ||
| 65 | if (!isset($this->verbosityLevelMap[$level])) { | ||
| 66 | throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level)); | ||
| 67 | } | ||
| 68 | |||
| 69 | $output = $this->output; | ||
| 70 | |||
| 71 | // Write to the error output if necessary and available | ||
| 72 | if (self::ERROR === $this->formatLevelMap[$level]) { | ||
| 73 | if ($this->output instanceof ConsoleOutputInterface) { | ||
| 74 | $output = $output->getErrorOutput(); | ||
| 75 | } | ||
| 76 | $this->errored = true; | ||
| 77 | } | ||
| 78 | |||
| 79 | // the if condition check isn't necessary -- it's the same one that $output will do internally anyway. | ||
| 80 | // We only do it for efficiency here as the message formatting is relatively expensive. | ||
| 81 | if ($output->getVerbosity() >= $this->verbosityLevelMap[$level]) { | ||
| 82 | $output->writeln(sprintf('<%1$s>[%2$s] %3$s</%1$s>', $this->formatLevelMap[$level], $level, $this->interpolate($message, $context)), $this->verbosityLevelMap[$level]); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | /** | ||
| 87 | * Returns true when any messages have been logged at error levels. | ||
| 88 | */ | ||
| 89 | public function hasErrored(): bool | ||
| 90 | { | ||
| 91 | return $this->errored; | ||
| 92 | } | ||
| 93 | |||
| 94 | /** | ||
| 95 | * Interpolates context values into the message placeholders. | ||
| 96 | * | ||
| 97 | * @author PHP Framework Interoperability Group | ||
| 98 | */ | ||
| 99 | private function interpolate(string $message, array $context): string | ||
| 100 | { | ||
| 101 | if (!str_contains($message, '{')) { | ||
| 102 | return $message; | ||
| 103 | } | ||
| 104 | |||
| 105 | $replacements = []; | ||
| 106 | foreach ($context as $key => $val) { | ||
| 107 | if (null === $val || \is_scalar($val) || $val instanceof \Stringable) { | ||
| 108 | $replacements["{{$key}}"] = $val; | ||
| 109 | } elseif ($val instanceof \DateTimeInterface) { | ||
| 110 | $replacements["{{$key}}"] = $val->format(\DateTimeInterface::RFC3339); | ||
| 111 | } elseif (\is_object($val)) { | ||
| 112 | $replacements["{{$key}}"] = '[object '.$val::class.']'; | ||
| 113 | } else { | ||
| 114 | $replacements["{{$key}}"] = '['.\gettype($val).']'; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | return strtr($message, $replacements); | ||
| 119 | } | ||
| 120 | } | ||
