Skip to content

Commit abc1357

Browse files
cyvenicolas-grekas
authored andcommitted
[HttpKernel] Remove EOL when using error_log() in HttpKernel Logger
1 parent 5e94292 commit abc1357

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

Log/Logger.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,14 @@ public function __construct(string $minLevel = null, $output = null, callable $f
4444

4545
if (isset($_ENV['SHELL_VERBOSITY']) || isset($_SERVER['SHELL_VERBOSITY'])) {
4646
switch ((int) ($_ENV['SHELL_VERBOSITY'] ?? $_SERVER['SHELL_VERBOSITY'])) {
47-
case -1: $minLevel = LogLevel::ERROR; break;
48-
case 1: $minLevel = LogLevel::NOTICE; break;
49-
case 2: $minLevel = LogLevel::INFO; break;
50-
case 3: $minLevel = LogLevel::DEBUG; break;
47+
case -1: $minLevel = LogLevel::ERROR;
48+
break;
49+
case 1: $minLevel = LogLevel::NOTICE;
50+
break;
51+
case 2: $minLevel = LogLevel::INFO;
52+
break;
53+
case 3: $minLevel = LogLevel::DEBUG;
54+
break;
5155
}
5256
}
5357
}
@@ -80,7 +84,7 @@ public function log($level, $message, array $context = [])
8084

8185
$formatter = $this->formatter;
8286
if ($this->handle) {
83-
@fwrite($this->handle, $formatter($level, $message, $context));
87+
@fwrite($this->handle, $formatter($level, $message, $context).\PHP_EOL);
8488
} else {
8589
error_log($formatter($level, $message, $context, false));
8690
}
@@ -91,7 +95,7 @@ private function format(string $level, string $message, array $context, bool $pr
9195
if (str_contains($message, '{')) {
9296
$replacements = [];
9397
foreach ($context as $key => $val) {
94-
if (null === $val || is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) {
98+
if (null === $val || \is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) {
9599
$replacements["{{$key}}"] = $val;
96100
} elseif ($val instanceof \DateTimeInterface) {
97101
$replacements["{{$key}}"] = $val->format(\DateTime::RFC3339);
@@ -105,7 +109,7 @@ private function format(string $level, string $message, array $context, bool $pr
105109
$message = strtr($message, $replacements);
106110
}
107111

108-
$log = sprintf('[%s] %s', $level, $message).\PHP_EOL;
112+
$log = sprintf('[%s] %s', $level, $message);
109113
if ($prefixDate) {
110114
$log = date(\DateTime::RFC3339).' '.$log;
111115
}

Tests/Log/LoggerTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected function tearDown(): void
4848
public static function assertLogsMatch(array $expected, array $given)
4949
{
5050
foreach ($given as $k => $line) {
51-
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]\"");
51+
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]\"");
5252
}
5353
}
5454

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

192192
$this->logger->error('An error', ['foo' => 'bar']);
@@ -196,6 +196,26 @@ public function testFormatter()
196196
'{"level":"warning","message":"A warning","context":{"baz":"bar"}}',
197197
], $this->getLogs());
198198
}
199+
200+
public function testLogsWithoutOutput()
201+
{
202+
$oldErrorLog = ini_set('error_log', $this->tmpFile);
203+
204+
$logger = new Logger();
205+
$logger->error('test');
206+
$logger->critical('test');
207+
208+
$expected = [
209+
'[error] test',
210+
'[critical] test',
211+
];
212+
213+
foreach ($this->getLogs() as $k => $line) {
214+
$this->assertSame(1, preg_match('/\[[\w\/\-: ]+\] '.preg_quote($expected[$k]).'/', $line), "\"$line\" do not match expected pattern \"$expected[$k]\"");
215+
}
216+
217+
ini_set('error_log', $oldErrorLog);
218+
}
199219
}
200220

201221
class DummyTest

0 commit comments

Comments
 (0)