Skip to content

Commit 1f23d4d

Browse files
Merge branch '3.4' into 4.4
* 3.4: [PhpUnitBridge] fix bad detection of unsilenced deprecations [HttpKernel] Fix error logger when stderr is redirected to /dev/null (FPM)
2 parents 7d9b08c + 238f491 commit 1f23d4d

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

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(string $minLevel = null, $output = 'php://stderr', callable $formatter = null)
40+
public function __construct(string $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(string $minLevel = null, $output = 'php://stderr', c
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
}
@@ -79,10 +79,14 @@ public function log($level, $message, array $context = [])
7979
}
8080

8181
$formatter = $this->formatter;
82-
@fwrite($this->handle, $formatter($level, $message, $context));
82+
if ($this->handle) {
83+
@fwrite($this->handle, $formatter($level, $message, $context));
84+
} else {
85+
error_log($formatter($level, $message, $context, false));
86+
}
8387
}
8488

85-
private function format(string $level, string $message, array $context): string
89+
private function format(string $level, string $message, array $context, bool $prefixDate = true): string
8690
{
8791
if (false !== strpos($message, '{')) {
8892
$replacements = [];
@@ -101,6 +105,11 @@ private function format(string $level, string $message, array $context): string
101105
$message = strtr($message, $replacements);
102106
}
103107

104-
return sprintf('%s [%s] %s', date(\DateTime::RFC3339), $level, $message).PHP_EOL;
108+
$log = sprintf('[%s] %s', $level, $message).PHP_EOL;
109+
if ($prefixDate) {
110+
$log = date(\DateTime::RFC3339).' '.$log;
111+
}
112+
113+
return $log;
105114
}
106115
}

0 commit comments

Comments
 (0)