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