8000 bug #36855 [HttpKernel] Fix error logger when stderr is redirected to… · symfony/symfony@7ee33f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7ee33f9

Browse files
committed
bug #36855 [HttpKernel] Fix error logger when stderr is redirected to /dev/null (fabpot)
This PR was merged into the 3.4 branch. Discussion ---------- [HttpKernel] Fix error logger when stderr is redirected to /dev/null | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | n/a <!-- prefix each issue number with "Fix #", if any --> | License | MIT | Doc PR | n/a The HttpKernel Logger is meant to be used as a last resort logging mechanism when no logger has been explicitly configured (Monolog is not a dependency for instance). For small apps, that can be more than enough. But under some circumstances, it does not work. When you are using PHP-FPM, `stderr` is ignored by default (`catch_workers_output` is `false`) and so, logs are ignored as well. There is no issue with the official PHP Docker image as the setting has been explicitly set to `true`. Not an issue with Symfony CLI as well, as we also change the setting. Not a problem either with the PHP built-in server as it does not use PHP FPM anyway. But, in many other places, where the setting has its default value, logs are lost (as you can imagine, it happened to me). As this feature is meant to be a fallback, I think it should always work, or at least, we need to make everything possible to make it work out of the box; that's why I've considered it a bug and hence a PR on 3.4. This PR changes the default value for the output to `null`, which uses `error_log()` instead of `stderr` to log errors. Why is it better? The output of `error_log()` is controllable by the `error_logs` PHP ini setting and it is well understood by everyone (the default configuration should always work well); so it should work in most/more cases. The other change (to be discussed) is to also log messages at the `ERROR` level and not just the `CRITICAL` ones. /cc @dunglas Commits ------- 5f829bd [HttpKernel] Fix error logger when stderr is redirected to /dev/null (FPM)
2 parents cb7e78c + 5f829bd commit 7ee33f9

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/Symfony/Component/HttpKernel/Log/Logger.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ class Logger extends AbstractLogger
3737
private $formatter;
3838
private $handle;
3939

40-
public function __construct($minLevel = null, $output = 'php://stderr', callable $formatter = null)
40+
public function __construct($minLevel = null, $output = null, callable $formatter = null)
4141
{
4242
if (null === $minLevel) {
43-
$minLevel = 'php://stdout' === $output || 'php://stderr' === $output ? LogLevel::CRITICAL : LogLevel::WARNING;
43+
$minLevel = null === $output || 'php://stdout' === $output || 'php://stderr' === $output ? LogLevel::ERROR : LogLevel::WARNING;
4444

4545
if (isset($_ENV['SHELL_VERBOSITY']) || isset($_SERVER['SHELL_VERBOSITY'])) {
4646
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
5858

5959
$this->minLevelIndex = self::$levels[$minLevel];
6060
$this->formatter = $formatter ?: [$this, 'format'];
61-
if (false === $this->handle = \is_resource($output) ? $output : @fopen($output, 'a')) {
61+
if ($output && false === $this->handle = \is_resource($output) ? $output : @fopen($output, 'a')) {
6262
throw new InvalidArgumentException(sprintf('Unable to open "%s".', $output));
6363
}
6464
}
@@ -77,7 +77,11 @@ public function log($level, $message, array $context = [])
7777
}
7878

7979
$formatter = $this->formatter;
80-
@fwrite($this->handle, $formatter($level, $message, $context));
80+
if ($this->handle) {
81+
@fwrite($this->handle, $formatter($level, $message, $context));
82+
} else {
83+
error_log($formatter($level, $message, $context, false));
84+
}
8185
}
8286

8387
/**
@@ -86,7 +90,7 @@ public function log($level, $message, array $context = [])
8690
*
8791
* @return string
8892
*/
89-
private function format($level, $message, array $context)
93+
private function format($level, $message, array $context, $prefixDate = true)
9094
{
9195
if (false !== strpos($message, '{')) {
9296
$replacements = [];
@@ -105,6 +109,11 @@ private function format($level, $message, array $context)
105109
$message = strtr($message, $replacements);
106110
}
107111

108-
return sprintf('%s [%s] %s', date(\DateTime::RFC3339), $level, $message).PHP_EOL;
112+
$log = sprintf('[%s] %s', $level, $message).PHP_EOL;
113+
if ($prefixDate) {
114+
$log = date(\DateTime::RFC3339).' '.$log;
115+
}
116+
117+
return $log;
109118
}
110119
}

0 commit comments

Comments
 (0)
0