PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.12.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.12.1
5.12.1 5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / vendor / prefixed / symfony / http-kernel / Log / Logger.php
matomo / app / vendor / prefixed / symfony / http-kernel / Log Last commit date
DebugLoggerInterface.php 2 years ago Logger.php 1 year ago
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