Skip to content

Commit

Permalink
[HttpKernel] Fix error logger when stderr is redirected to /dev/null …
Browse files Browse the repository at this point in the history
…(FPM)
  • Loading branch information
fabpot committed May 18, 2020
1 parent cb7e78c commit 92c6c49
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 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 = '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,8 +58,10 @@ 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')) {
throw new InvalidArgumentException(sprintf('Unable to open "%s".', $output));
if ($output) {
if (false === $this->handle = \is_resource($output) ? $output : @fopen($output, 'a')) {
throw new InvalidArgumentException(sprintf('Unable to open "%s".', $output));
}
}
}

Expand All @@ -77,7 +79,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));
}
}

/**
Expand Down

0 comments on commit 92c6c49

Please sign in to comment.