From 5f829bdaeb0f045c906ac0f77824a0beea5b5bcc Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 18 May 2020 16:22:26 +0200 Subject: [PATCH 1/2] [HttpKernel] Fix error logger when stderr is redirected to /dev/null (FPM) --- .../Component/HttpKernel/Log/Logger.php | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Log/Logger.php b/src/Symfony/Component/HttpKernel/Log/Logger.php index e05d8c32ec79..bbdd101d7dc2 100644 --- a/src/Symfony/Component/HttpKernel/Log/Logger.php +++ b/src/Symfony/Component/HttpKernel/Log/Logger.php @@ -37,10 +37,10 @@ class Logger extends AbstractLogger private $formatter; private $handle; - public function __construct($minLevel = null, $output = 'php://stderr', callable $formatter = null) + public function __construct($minLevel = null, $output = null, callable $formatter = null) { if (null === $minLevel) { - $minLevel = 'php://stdout' === $output || 'php://stderr' === $output ? LogLevel::CRITICAL : 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'])) { @@ -58,7 +58,7 @@ public function __construct($minLevel = null, $output = 'php://stderr', callable $this->minLevelIndex = self::$levels[$minLevel]; $this->formatter = $formatter ?: [$this, 'format']; - if (false === $this->handle = \is_resource($output) ? $output : @fopen($output, 'a')) { + if ($output && false === $this->handle = \is_resource($output) ? $output : @fopen($output, 'a')) { throw new InvalidArgumentException(sprintf('Unable to open "%s".', $output)); } } @@ -77,7 +77,11 @@ public function log($level, $message, array $context = []) } $formatter = $this->formatter; - @fwrite($this->handle, $formatter($level, $message, $context)); + if ($this->handle) { + @fwrite($this->handle, $formatter($level, $message, $context)); + } else { + error_log($formatter($level, $message, $context, false)); + } } /** @@ -86,7 +90,7 @@ public function log($level, $message, array $context = []) * * @return string */ - private function format($level, $message, array $context) + private function format($level, $message, array $context, $prefixDate = true) { if (false !== strpos($message, '{')) { $replacements = []; @@ -105,6 +109,11 @@ private function format($level, $message, array $context) $message = strtr($message, $replacements); } - return sprintf('%s [%s] %s', date(\DateTime::RFC3339), $level, $message).PHP_EOL; + $log = sprintf('[%s] %s', $level, $message).PHP_EOL; + if ($prefixDate) { + $log = date(\DateTime::RFC3339).' '.$log; + } + + return $log; } } From 9d5bb11ec92950f1caf60925f6b8be007f060750 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 19 May 2020 10:04:03 +0200 Subject: [PATCH 2/2] [PhpUnitBridge] fix bad detection of unsilenced deprecations --- src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php | 4 ++-- .../Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php b/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php index 7cccd8eb83a4..e9c8d19d8f0c 100644 --- a/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php +++ b/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php @@ -140,7 +140,7 @@ public static function register($mode = 0) $Test = $UtilPrefix.'Test'; - if (0 !== error_reporting()) { + if (error_reporting() & $type) { $group = 'unsilenced'; } elseif (0 === strpos($method, 'testLegacy') || 0 === strpos($method, 'provideLegacy') @@ -292,7 +292,7 @@ public static function collectDeprecations($outputFile) return \call_user_func(DeprecationErrorHandler::getPhpUnitErrorHandler(), $type, $msg, $file, $line, $context); } - $deprecations[] = array(error_reporting(), $msg, $file); + $deprecations[] = array(error_reporting() & $type, $msg, $file); return null; }); diff --git a/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php b/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php index ee9378d140f7..b6d05cb0f102 100644 --- a/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php +++ b/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php @@ -352,7 +352,7 @@ public function handleError($type, $msg, $file, $line, $context = array()) if (\is_array($parsedMsg)) { $msg = $parsedMsg['deprecation']; } - if (error_reporting()) { + if (error_reporting() & $type) { $msg = 'Unsilenced deprecation: '.$msg; } $this->gatheredDeprecations[] = $msg;