updated-packages
This commit is contained in:
59
vendor/symfony/http-kernel/Log/Logger.php
vendored
59
vendor/symfony/http-kernel/Log/Logger.php
vendored
@@ -22,7 +22,7 @@ use Psr\Log\LogLevel;
|
||||
*/
|
||||
class Logger extends AbstractLogger
|
||||
{
|
||||
private static $levels = array(
|
||||
private const LEVELS = [
|
||||
LogLevel::DEBUG => 0,
|
||||
LogLevel::INFO => 1,
|
||||
LogLevel::NOTICE => 2,
|
||||
@@ -31,61 +31,71 @@ class Logger extends AbstractLogger
|
||||
LogLevel::CRITICAL => 5,
|
||||
LogLevel::ALERT => 6,
|
||||
LogLevel::EMERGENCY => 7,
|
||||
);
|
||||
];
|
||||
|
||||
private $minLevelIndex;
|
||||
private $formatter;
|
||||
private $handle;
|
||||
|
||||
public function __construct(string $minLevel = null, $output = 'php://stderr', callable $formatter = null)
|
||||
public function __construct(string $minLevel = null, $output = null, callable $formatter = null)
|
||||
{
|
||||
if (null === $minLevel) {
|
||||
$minLevel = LogLevel::WARNING;
|
||||
$minLevel = null === $output || 'php://stdout' === $output || 'php://stderr' === $output ? LogLevel::ERROR : LogLevel::WARNING;
|
||||
|
||||
if (isset($_ENV['SHELL_VERBOSITY']) || isset($_SERVER['SHELL_VERBOSITY'])) {
|
||||
switch ((int) (isset($_ENV['SHELL_VERBOSITY']) ? $_ENV['SHELL_VERBOSITY'] : $_SERVER['SHELL_VERBOSITY'])) {
|
||||
case -1: $minLevel = LogLevel::ERROR; break;
|
||||
case 1: $minLevel = LogLevel::NOTICE; break;
|
||||
case 2: $minLevel = LogLevel::INFO; break;
|
||||
case 3: $minLevel = LogLevel::DEBUG; break;
|
||||
switch ((int) ($_ENV['SHELL_VERBOSITY'] ?? $_SERVER['SHELL_VERBOSITY'])) {
|
||||
case -1: $minLevel = LogLevel::ERROR;
|
||||
break;
|
||||
case 1: $minLevel = LogLevel::NOTICE;
|
||||
break;
|
||||
case 2: $minLevel = LogLevel::INFO;
|
||||
break;
|
||||
case 3: $minLevel = LogLevel::DEBUG;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset(self::$levels[$minLevel])) {
|
||||
if (!isset(self::LEVELS[$minLevel])) {
|
||||
throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $minLevel));
|
||||
}
|
||||
|
||||
$this->minLevelIndex = self::$levels[$minLevel];
|
||||
$this->formatter = $formatter ?: array($this, 'format');
|
||||
if (false === $this->handle = \is_resource($output) ? $output : @fopen($output, 'a')) {
|
||||
$this->minLevelIndex = self::LEVELS[$minLevel];
|
||||
$this->formatter = $formatter ?: [$this, 'format'];
|
||||
if ($output && false === $this->handle = \is_resource($output) ? $output : @fopen($output, 'a')) {
|
||||
throw new InvalidArgumentException(sprintf('Unable to open "%s".', $output));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function log($level, $message, array $context = array())
|
||||
public function log($level, $message, array $context = [])
|
||||
{
|
||||
if (!isset(self::$levels[$level])) {
|
||||
if (!isset(self::LEVELS[$level])) {
|
||||
throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level));
|
||||
}
|
||||
|
||||
if (self::$levels[$level] < $this->minLevelIndex) {
|
||||
if (self::LEVELS[$level] < $this->minLevelIndex) {
|
||||
return;
|
||||
}
|
||||
|
||||
$formatter = $this->formatter;
|
||||
fwrite($this->handle, $formatter($level, $message, $context));
|
||||
if ($this->handle) {
|
||||
@fwrite($this->handle, $formatter($level, $message, $context).\PHP_EOL);
|
||||
} else {
|
||||
error_log($formatter($level, $message, $context, false));
|
||||
}
|
||||
}
|
||||
|
||||
private function format(string $level, string $message, array $context): string
|
||||
private function format(string $level, string $message, array $context, bool $prefixDate = true): string
|
||||
{
|
||||
if (false !== strpos($message, '{')) {
|
||||
$replacements = array();
|
||||
if (str_contains($message, '{')) {
|
||||
$replacements = [];
|
||||
foreach ($context as $key => $val) {
|
||||
if (null === $val || is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) {
|
||||
if (null === $val || \is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) {
|
||||
$replacements["{{$key}}"] = $val;
|
||||
} elseif ($val instanceof \DateTimeInterface) {
|
||||
$replacements["{{$key}}"] = $val->format(\DateTime::RFC3339);
|
||||
@@ -99,6 +109,11 @@ class Logger extends AbstractLogger
|
||||
$message = strtr($message, $replacements);
|
||||
}
|
||||
|
||||
return sprintf('%s [%s] %s', date(\DateTime::RFC3339), $level, $message).\PHP_EOL;
|
||||
$log = sprintf('[%s] %s', $level, $message);
|
||||
if ($prefixDate) {
|
||||
$log = date(\DateTime::RFC3339).' '.$log;
|
||||
}
|
||||
|
||||
return $log;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user