Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[HttpKernel] Remove EOL when using error_log() in HttpKernel Logger #47878

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

Merged
merged 1 commit into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[HttpKernel] Remove EOL when using error_log() in HttpKernel Logger
  • Loading branch information
cyve authored and nicolas-grekas committed Oct 18, 2022
commit 69cf83ea1aed548176813b89cad5a73046c36895
18 changes: 11 additions & 7 deletions 18 src/Symfony/Component/HttpKernel/Log/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@ public function __construct(string $minLevel = null, $output = null, callable $f

if (isset($_ENV['SHELL_VERBOSITY']) || isset($_SERVER['SHELL_VERBOSITY'])) {
switch ((int) ($_ENV['SHELL_VERBOSITY'] ?? $_SERVER['SHELL_VERBOSITY'])) {
case -1: $minLevel = LogLevel::ERROR; break;
case 1: $minLevel = LogLevel::NOTICE; break;
case 2: $minLevel = LogLevel::INFO; break;
case 3: $minLevel = LogLevel::DEBUG; break;
case -1: $minLevel = LogLevel::ERROR;
break;
case 1: $minLevel = LogLevel::NOTICE;
break;
case 2: $minLevel = LogLevel::INFO;
break;
case 3: $minLevel = LogLevel::DEBUG;
break;
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down Expand Up @@ -80,7 +84,7 @@ public function log($level, $message, array $context = [])

$formatter = $this->formatter;
if ($this->handle) {
@fwrite($this->handle, $formatter($level, $message, $context));
@fwrite($this->handle, $formatter($level, $message, $context).\PHP_EOL);
} else {
error_log($formatter($level, $message, $context, false));
}
Expand All @@ -91,7 +95,7 @@ private function format(string $level, string $message, array $context, bool $pr
if (str_contains($message, '{')) {
$replacements = [];
foreach ($context as $key => $val) {
if (null === $val || is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) {
if (null === $val || \is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) {
$replacements["{{$key}}"] = $val;
} elseif ($val instanceof \DateTimeInterface) {
$replacements["{{$key}}"] = $val->format(\DateTime::RFC3339);
Expand All @@ -105,7 +109,7 @@ private function format(string $level, string $message, array $context, bool $pr
$message = strtr($message, $replacements);
}

$log = sprintf('[%s] %s', $level, $message).\PHP_EOL;
$log = sprintf('[%s] %s', $level, $message);
if ($prefixDate) {
$log = date(\DateTime::RFC3339).' '.$log;
}
Expand Down
24 changes: 22 additions & 2 deletions 24 src/Symfony/Component/HttpKernel/Tests/Log/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function tearDown(): void
public static function assertLogsMatch(array $expected, array $given)
{
foreach ($given as $k => $line) {
self::assertThat(1 === preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}[\+-][0-9]{2}:[0-9]{2} '.preg_quote($expected[$k]).'/', $line), self::isTrue(), "\"$line\" do not match expected pattern \"$expected[$k]\"");
self::assertSame(1, preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}[\+-][0-9]{2}:[0-9]{2} '.preg_quote($expected[$k]).'/', $line), "\"$line\" do not match expected pattern \"$expected[$k]\"");
}
}

Expand Down Expand Up @@ -186,7 +186,7 @@ public function testContextExceptionKeyCanBeExceptionOrOtherValues()
public function testFormatter()
{
$this->logger = new Logger(LogLevel::DEBUG, $this->tmpFile, function ($level, $message, $context) {
return json_encode(['level' => $level, 'message' => $message, 'context' => $context]).\PHP_EOL;
return json_encode(['level' => $level, 'message' => $message, 'context' => $context]);
});

$this->logger->error('An error', ['foo' => 'bar']);
Expand All @@ -196,6 +196,26 @@ public function testFormatter()
'{"level":"warning","message":"A warning","context":{"baz":"bar"}}',
], $this->getLogs());
}

public function testLogsWithoutOutput()
{
$oldErrorLog = ini_set('error_log', $this->tmpFile);

$logger = new Logger();
$logger->error('test');
$logger->critical('test');

$expected = [
'[error] test',
'[critical] test',
];

foreach ($this->getLogs() as $k => $line) {
$this->assertSame(1, preg_match('/\[[\w\/\-: ]+\] '.preg_quote($expected[$k]).'/', $line), "\"$line\" do not match expected pattern \"$expected[$k]\"");
}

ini_set('error_log', $oldErrorLog);
}
}

class DummyTest
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.