Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HttpKernel] Fix error logger when stderr is redirected to /dev/null #36855

Merged
merged 1 commit into from May 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/Symfony/Component/HttpKernel/Log/Logger.php
Expand Up @@ -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'])) {
Expand All @@ -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));
}
}
Expand All @@ -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));
}
}

/**
Expand All @@ -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 = [];
Expand All @@ -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;
}
}