Logger.php
105 lines
| 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 | namespace Matomo\Dependencies\Symfony\Component\HttpKernel\Log; |
| 12 | |
| 13 | use Matomo\Dependencies\Psr\Log\AbstractLogger; |
| 14 | use Matomo\Dependencies\Psr\Log\InvalidArgumentException; |
| 15 | use Matomo\Dependencies\Psr\Log\LogLevel; |
| 16 | /** |
| 17 | * Minimalist PSR-3 logger designed to write in stderr or any other stream. |
| 18 | * |
| 19 | * @author Kévin Dunglas <dunglas@gmail.com> |
| 20 | */ |
| 21 | class Logger extends AbstractLogger |
| 22 | { |
| 23 | private const LEVELS = [LogLevel::DEBUG => 0, LogLevel::INFO => 1, LogLevel::NOTICE => 2, LogLevel::WARNING => 3, LogLevel::ERROR => 4, LogLevel::CRITICAL => 5, LogLevel::ALERT => 6, LogLevel::EMERGENCY => 7]; |
| 24 | private $minLevelIndex; |
| 25 | private $formatter; |
| 26 | /** @var resource|null */ |
| 27 | private $handle; |
| 28 | /** |
| 29 | * @param string|resource|null $output |
| 30 | */ |
| 31 | public function __construct(?string $minLevel = null, $output = null, ?callable $formatter = null) |
| 32 | { |
| 33 | if (null === $minLevel) { |
| 34 | $minLevel = null === $output || 'php://stdout' === $output || 'php://stderr' === $output ? LogLevel::ERROR : LogLevel::WARNING; |
| 35 | if (isset($_ENV['SHELL_VERBOSITY']) || isset($_SERVER['SHELL_VERBOSITY'])) { |
| 36 | switch ((int) ($_ENV['SHELL_VERBOSITY'] ?? $_SERVER['SHELL_VERBOSITY'])) { |
| 37 | case -1: |
| 38 | $minLevel = LogLevel::ERROR; |
| 39 | break; |
| 40 | case 1: |
| 41 | $minLevel = LogLevel::NOTICE; |
| 42 | break; |
| 43 | case 2: |
| 44 | $minLevel = LogLevel::INFO; |
| 45 | break; |
| 46 | case 3: |
| 47 | $minLevel = LogLevel::DEBUG; |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | if (!isset(self::LEVELS[$minLevel])) { |
| 53 | throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $minLevel)); |
| 54 | } |
| 55 | $this->minLevelIndex = self::LEVELS[$minLevel]; |
| 56 | $this->formatter = $formatter ?: [$this, 'format']; |
| 57 | if ($output && \false === ($this->handle = \is_string($output) ? @fopen($output, 'a') : $output)) { |
| 58 | throw new InvalidArgumentException(sprintf('Unable to open "%s".', $output)); |
| 59 | } |
| 60 | } |
| 61 | /** |
| 62 | * {@inheritdoc} |
| 63 | * |
| 64 | * @return void |
| 65 | */ |
| 66 | public function log($level, $message, array $context = []) |
| 67 | { |
| 68 | if (!isset(self::LEVELS[$level])) { |
| 69 | throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level)); |
| 70 | } |
| 71 | if (self::LEVELS[$level] < $this->minLevelIndex) { |
| 72 | return; |
| 73 | } |
| 74 | $formatter = $this->formatter; |
| 75 | if ($this->handle) { |
| 76 | @fwrite($this->handle, $formatter($level, $message, $context) . \PHP_EOL); |
| 77 | } else { |
| 78 | error_log($formatter($level, $message, $context, \false)); |
| 79 | } |
| 80 | } |
| 81 | private function format(string $level, string $message, array $context, bool $prefixDate = \true) : string |
| 82 | { |
| 83 | if (str_contains($message, '{')) { |
| 84 | $replacements = []; |
| 85 | foreach ($context as $key => $val) { |
| 86 | if (null === $val || \is_scalar($val) || \is_object($val) && method_exists($val, '__toString')) { |
| 87 | $replacements["{{$key}}"] = $val; |
| 88 | } elseif ($val instanceof \DateTimeInterface) { |
| 89 | $replacements["{{$key}}"] = $val->format(\DateTime::RFC3339); |
| 90 | } elseif (\is_object($val)) { |
| 91 | $replacements["{{$key}}"] = '[object ' . \get_class($val) . ']'; |
| 92 | } else { |
| 93 | $replacements["{{$key}}"] = '[' . \gettype($val) . ']'; |
| 94 | } |
| 95 | } |
| 96 | $message = strtr($message, $replacements); |
| 97 | } |
| 98 | $log = sprintf('[%s] %s', $level, $message); |
| 99 | if ($prefixDate) { |
| 100 | $log = date(\DateTime::RFC3339) . ' ' . $log; |
| 101 | } |
| 102 | return $log; |
| 103 | } |
| 104 | } |
| 105 |