Skip to content

Commit 35032e4

Browse files
committed
Merge remote-tracking branch 'origin/5.4' into 6.0
* origin/5.4: [FrameworkBundle][5.4] Remove fileLinkFormat property from DebugHandlersListener [WebProfilerBundle] Redesigned the log section
2 parents 5d42a8e + aa0b7e3 commit 35032e4

File tree

4 files changed

+104
-12
lines changed

4 files changed

+104
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ CHANGELOG
1414
---
1515

1616
* Deprecate `AbstractTestSessionListener::getSession` inject a session in the request instead
17+
* Deprecate the `fileLinkFormat` parameter of `DebugHandlersListener`
1718

1819
5.3
1920
---

DataCollector/LoggerDataCollector.php

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class LoggerDataCollector extends DataCollector implements LateDataCollectorInte
2828
private $containerPathPrefix;
2929
private $currentRequest;
3030
private $requestStack;
31+
private $processedLogs;
3132

3233
public function __construct(object $logger = null, string $containerPathPrefix = null, RequestStack $requestStack = null)
3334
{
@@ -80,6 +81,81 @@ public function getLogs()
8081
return $this->data['logs'] ?? [];
8182
}
8283

84+
public function getProcessedLogs()
85+
{
86+
if (null !== $this->processedLogs) {
87+
return $this->processedLogs;
88+
}
89+
90+
$rawLogs = $this->getLogs();
91+
if ([] === $rawLogs) {
92+
return $this->processedLogs = $rawLogs;
93+
}
94+
95+
$logs = [];
96+
foreach ($this->getLogs()->getValue() as $rawLog) {
97+
$rawLogData = $rawLog->getValue();
98+
99+
if ($rawLogData['priority']->getValue() > 300) {
100+
$logType = 'error';
101+
} elseif (isset($rawLogData['scream']) && false === $rawLogData['scream']->getValue()) {
102+
$logType = 'deprecation';
103+
} elseif (isset($rawLogData['scream']) && true === $rawLogData['scream']->getValue()) {
104+
$logType = 'silenced';
105+
} else {
106+
$logType = 'regular';
107+
}
108+
109+
$logs[] = [
110+
'type' => $logType,
111+
'errorCounter' => isset($rawLogData['errorCounter']) ? $rawLogData['errorCounter']->getValue() : 1,
112+
'timestamp' => $rawLogData['timestamp_rfc3339']->getValue(),
113+
'priority' => $rawLogData['priority']->getValue(),
114+
'priorityName' => $rawLogData['priorityName']->getValue(),
115+
'channel' => $rawLogData['channel']->getValue(),
116+
'message' => $rawLogData['message'],
117+
'context' => $rawLogData['context'],
118+
];
119+
}
120+
121+
// sort logs from oldest to newest
122+
usort($logs, static function ($logA, $logB) {
123+
return $logA['timestamp'] <=> $logB['timestamp'];
124+
});
125+
126+
return $this->processedLogs = $logs;
127+
}
128+
129+
public function getFilters()
130+
{
131+
$filters = [
132+
'channel' => [],
133+
'priority' => [
134+
'Debug' => 100,
135+
'Info' => 200,
136+
'Warning' => 300,
137+
'Error' => 400,
138+
'Critical' => 500,
139+
'Alert' => 550,
140+
'Emergency' => 600,
141+
],
142+
];
143+
144+
$allChannels = [];
145+
foreach ($this->getProcessedLogs() as $log) {
146+
if ('' === trim($log['channel'])) {
147+
continue;
148+
}
149+
150+
$allChannels[] = $log['channel'];
151+
}
152+
$channels = array_unique($allChannels);
153+
sort($channels);
154+
$filters['channel'] = $channels;
155+
156+
return $filters;
157+
}
158+
83159
public function getPriorities()
84160
{
85161
return $this->data['priorities'] ?? [];
@@ -132,7 +208,7 @@ private function getContainerDeprecationLogs(): array
132208
$logs = [];
133209
foreach (unserialize($logContent) as $log) {
134210
$log['context'] = ['exception' => new SilencedErrorContext($log['type'], $log['file'], $log['line'], $log['trace'], $log['count'])];
135-
$log['timestamp'] = $bootTime;
211+
$log['timestamp'] = (new \DateTimeImmutable())->setTimestamp($bootTime)->format(\DateTimeInterface::RFC3339_EXTENDED);
136212
$log['priority'] = 100;
137213
$log['priorityName'] = 'DEBUG';
138214
$log['channel'] = null;

EventListener/DebugHandlersListener.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1818
use Symfony\Component\ErrorHandler\ErrorHandler;
1919
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20-
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
2120
use Symfony\Component\HttpKernel\Event\KernelEvent;
2221
use Symfony\Component\HttpKernel\KernelEvents;
2322

@@ -39,21 +38,25 @@ class DebugHandlersListener implements EventSubscriberInterface
3938
private $levels;
4039
private $throwAt;
4140
private $scream;
42-
private $fileLinkFormat;
4341
private $scope;
4442
private $firstCall = true;
4543
private $hasTerminatedWithException;
4644

4745
/**
48-
* @param callable|null $exceptionHandler A handler that must support \Throwable instances that will be called on Exception
49-
* @param array|int $levels An array map of E_* to LogLevel::* or an integer bit field of E_* constants
50-
* @param int|null $throwAt Thrown errors in a bit field of E_* constants, or null to keep the current value
51-
* @param bool $scream Enables/disables screaming mode, where even silenced errors are logged
52-
* @param string|FileLinkFormatter|null $fileLinkFormat The format for links to source files
53-
* @param bool $scope Enables/disables scoping mode
46+
* @param callable|null $exceptionHandler A handler that must support \Throwable instances that will be called on Exception
47+
* @param array|int $levels An array map of E_* to LogLevel::* or an integer bit field of E_* constants
48+
* @param int|null $throwAt Thrown errors in a bit field of E_* constants, or null to keep the current value
49+
* @param bool $scream Enables/disables screaming mode, where even silenced errors are logged
50+
* @param bool $scope Enables/disables scoping mode
5451
*/
55-
public function __construct(callable $exceptionHandler = null, LoggerInterface $logger = null, $levels = \E_ALL, ?int $throwAt = \E_ALL, bool $scream = true, string|FileLinkFormatter $fileLinkFormat = null, bool $scope = true, LoggerInterface $deprecationLogger = null)
52+
public function __construct(callable $exceptionHandler = null, LoggerInterface $logger = null, $levels = \E_ALL, ?int $throwAt = \E_ALL, bool $scream = true, $scope = true, $deprecationLogger = null, $fileLinkFormat = null)
5653
{
54+
if (!\is_bool($scope)) {
55+
trigger_deprecation('symfony/http-kernel', '5.4', 'Passing a $fileLinkFormat is deprecated.');
56+
$scope = $deprecationLogger;
57+
$deprecationLogger = $fileLinkFormat;
58+
}
59+
5760
$handler = set_exception_handler('var_dump');
5861
$this->earlyHandler = \is_array($handler) ? $handler[0] : null;
5962
restore_exception_handler();
@@ -63,7 +66,6 @@ public function __construct(callable $exceptionHandler = null, LoggerInterface $
6366
$this->levels = $levels ?? \E_ALL;
6467
$this->throwAt = \is_int($throwAt) ? $throwAt : (null === $throwAt ? null : ($throwAt ? \E_ALL : null));
6568
$this->scream = $scream;
66-
$this->fileLinkFormat = $fileLinkFormat;
6769
$this->scope = $scope;
6870
$this->deprecationLogger = $deprecationLogger;
6971
}

Tests/EventListener/DebugHandlersListenerTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Psr\Log\LoggerInterface;
1616
use Psr\Log\LogLevel;
17+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1718
use Symfony\Component\Console\Application;
1819
use Symfony\Component\Console\Command\Command;
1920
use Symfony\Component\Console\ConsoleEvents;
@@ -34,6 +35,8 @@
3435
*/
3536
class DebugHandlersListenerTest extends TestCase
3637
{
38+
use ExpectDeprecationTrait;
39+
3740
public function testConfigure()
3841
{
3942
$logger = $this->createMock(LoggerInterface::class);
@@ -219,7 +222,7 @@ public function testLevelsAssignedToLoggers(bool $hasLogger, bool $hasDeprecatio
219222
->method('setDefaultLogger')
220223
->withConsecutive(...$expectedCalls);
221224

222-
$sut = new DebugHandlersListener(null, $logger, $levels, null, true, null, true, $deprecationLogger);
225+
$sut = new DebugHandlersListener(null, $logger, $levels, null, true, true, $deprecationLogger);
223226
$prevHander = set_exception_handler([$handler, 'handleError']);
224227

225228
try {
@@ -236,4 +239,14 @@ public function testLevelsAssignedToLoggers(bool $hasLogger, bool $hasDeprecatio
236239
throw $e;
237240
}
238241
}
242+
243+
/**
244+
* @group legacy
245+
*/
246+
public function testLegacyConstructor()
247+
{
248+
$this->expectDeprecation('Since symfony/http-kernel 5.4: Passing a $fileLinkFormat is deprecated.');
249+
250+
new DebugHandlersListener(null, null, \E_ALL, \E_ALL, true, 'filelinkformat', true, $this->createMock(LoggerInterface::class));
251+
}
239252
}

0 commit comments

Comments
 (0)