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