8000 [HttpKernel][MonologBridge][FrameworkBundle] Revisit wiring of debug … · symfony/symfony@187b42a · GitHub
[go: up one dir, main page]

Skip to content

Commit 187b42a

Browse files
[HttpKernel][MonologBridge][FrameworkBundle] Revisit wiring of debug loggers
1 parent 4606073 commit 187b42a

File tree

17 files changed

+112
-40
lines changed

17 files changed

+112
-40
lines changed

src/Symfony/Bridge/Monolog/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add native return type to `Logger::clear()` and to `DebugProcessor::clear()`
8+
* Deprecate class `Logger`, use HttpKernel's `DebugLoggerConfigurator` instead
89

910
6.1
1011
---

src/Symfony/Bridge/Monolog/Logger.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111

1212
namespace Symfony\Bridge\Monolog;
1313

14+
trigger_deprecation('symfony/monolog-bridge', '5.4', 'The "%s" class is deprecated, use HttpKernel\'s DebugLoggerConfigurator instead.', Logger::class);
15+
1416
use Monolog\Logger as BaseLogger;
1517
use Monolog\ResettableInterface;
1618
use Symfony\Component\HttpFoundation\Request;
1719
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
1820
use Symfony\Contracts\Service\ResetInterface;
1921

2022
/**
21-
* @author Fabien Potencier <fabien@symfony.com>
23+
* @deprecated since Symfony 6.4, use HttpKernel's DebugLoggerConfigurator instead
2224
*/
2325
class Logger extends BaseLogger implements DebugLoggerInterface, ResetInterface
2426
{

src/Symfony/Bridge/Monolog/Tests/LoggerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
use Symfony\Bridge\Monolog\Processor\DebugProcessor;
1919
use Symfony\Component\HttpFoundation\Request;
2020

21+
/**
22+
* @group legacy
23+
*/
2124
class LoggerTest extends TestCase
2225
{
2326
public function testGetLogsWithoutDebugProcessor()

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ CHANGELOG
1010
* Add `DomCrawlerAssertionsTrait::assertAnySelectorTextContains(string $selector, string $text)`
1111
* Add `DomCrawlerAssertionsTrait::assertAnySelectorTextSame(string $selector, string $text)`
1212
* Add `DomCrawlerAssertionsTrait::assertAnySelectorTextNotContains(string $selector, string $text)`
13+
* Deprecate `EnableLoggerDebugModePass`, use argument `$debug` of HttpKernel's `Logger` instead
14+
* Deprecate `AddDebugLogProcessorPass::configureLogger()`, use HttpKernel's `DebugLoggerConfigurator` instead
1315

1416
6.3
1517
---

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddDebugLogProcessorPass.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,19 @@ public function process(ContainerBuilder $container)
3232
return;
3333
}
3434

35-
$definition = $container->getDefinition('monolog.logger_prototype');
36-
$definition->setConfigurator([__CLASS__, 'configureLogger']);
37-
$definition->addMethodCall('pushProcessor', [new Reference('debug.log_processor')]);
35+
$container->getDefinition('monolog.logger_prototype')
36+
->setConfigurator([new Reference('debug.debug_logger_configurator'), 'pushDebugLogger']);
3837
}
3938

4039
/**
40+
* @deprecated since Symfony 6.4, use HttpKernel's DebugLoggerConfigurator instead
41+
*
4142
* @return void
4243
*/
4344
public static function configureLogger(mixed $logger)
4445
{
46+
trigger_deprecation('symfony/framework-bundle', '6.4', 'The "%s()" method is deprecated, use HttpKernel\'s DebugLoggerConfigurator instead.', __METHOD__);
47+
4548
if (\is_object($logger) && method_exists($logger, 'removeDebugLogger') && \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
4649
$logger->removeDebugLogger();
4750
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/EnableLoggerDebugModePass.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
1313

14+
trigger_deprecation('symfony/framework-bundle', '6.4', 'The "%s" class is deprecated, use argument $debug of HttpKernel\'s Logger instead.', EnableLoggerDebugModePass::class);
15+
1416
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1517
use Symfony\Component\DependencyInjection\ContainerBuilder;
1618
use Symfony\Component\HttpKernel\Log\Logger;
1719

20+
/**
21+
* @deprecated since Symfony 6.4, use argument $debug of HttpKernel's Logger instead
22+
*/
1823
final class EnableLoggerDebugModePass implements CompilerPassInterface
1924
{
2025
public function process(ContainerBuilder $container): void
@@ -32,7 +37,7 @@ public function process(ContainerBuilder $container): void
3237

3338
public static function configureLogger(Logger $logger): void
3439
{
35-
if (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && method_exists($logger, 'enableDebug')) {
40+
if (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
3641
$logger->enableDebug();
3742
}
3843
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
102102
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
103103
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
104+
use Symfony\Component\HttpKernel\Log\DebugLoggerConfigurator;
104105
use Symfony\Component\Lock\LockFactory;
105106
use Symfony\Component\Lock\LockInterface;
106107
use Symfony\Component\Lock\PersistingStoreInterface;
@@ -1171,7 +1172,11 @@ private function registerDebugConfiguration(array $config, ContainerBuilder $con
11711172
$definition = new Definition(DebugProcessor::class);
11721173
$definition->setPublic(false);
11731174
$definition->addArgument(new Reference('request_stack'));
1175+
$definition->addTag('kernel.reset', ['method' => 'reset']);
11741176
$container->setDefinition('debug.log_processor', $definition);
1177+
1178+
$container->register('debug.debug_logger_configurator', DebugLoggerConfigurator::class)
1179+
->setArguments([new Reference('debug.log_processor')]);
11751180
}
11761181
}
11771182

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ public function build(ContainerBuilder $container)
183183
$container->addCompilerPass(new RemoveUnusedSessionMarshallingHandlerPass());
184184

185185
if ($container->getParameter('kernel.debug')) {
186-
$container->addCompilerPass(new EnableLoggerDebugModePass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -33);
187186
$container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 2);
188187
$container->addCompilerPass(new UnusedTagsPass(), PassConfig::TYPE_AFTER_REMOVING);
189188
$container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_BEFORE_REMOVING, -255);

src/Symfony/Component/ErrorHandler/ErrorRenderer/HtmlErrorRenderer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Symfony\Component\HttpFoundation\RequestStack;
1717
use Symfony\Component\HttpFoundation\Response;
1818
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
19-
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
19+
use Symfony\Component\HttpKernel\Log\DebugLoggerConfigurator;
2020
use Symfony\Component\VarDumper\Cloner\Data;
2121
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
2222

@@ -144,7 +144,7 @@ private function renderException(FlattenException $exception, string $debugTempl
144144
'exceptionMessage' => $exceptionMessage,
145145
'statusText' => $statusText,
146146
'statusCode' => $statusCode,
147-
'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null,
147+
'logger' => DebugLoggerConfigurator::getDebugLogger($this->logger),
148148
'currentContent' => \is_string($this->outputBuffer) ? $this->outputBuffer : ($this->outputBuffer)(),
149149
]);
150150
}

src/Symfony/Component/ErrorHandler/composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121
"symfony/var-dumper": "^5.4|^6.0|^7.0"
2222
},
2323
"require-dev": {
24-
"symfony/http-kernel": "^5.4|^6.0|^7.0",
24+
"symfony/http-kernel": "^6.4|^7.0",
2525
"symfony/serializer": "^5.4|^6.0|^7.0",
2626
"symfony/deprecation-contracts": "^2.5|^3"
2727
},
2828
"conflict": {
29-
"symfony/deprecation-contracts": "<2.5"
29+
"symfony/deprecation-contracts": "<2.5",
30+
"symfony/http-kernel": "<6.4"
3031
},
3132
"autoload": {
3233
"psr-4": { "Symfony\\Component\\ErrorHandler\\": "" },

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ CHANGELOG
99
* Add optional `$className` parameter to `ControllerEvent::getAttributes()`
1010
* Add native return types to `TraceableEventDispatcher` and to `MergeExtensionConfigurationPass`
1111
* Add argument `$validationFailedStatusCode` to `#[MapQueryString]` and `#[MapRequestPayload]`
12+
* Add argument `$debug` to `Logger`
13+
* Add class `DebugLoggerConfigurator`
1214

1315
6.3
1416
---

src/Symfony/Component/HttpKernel/DataCollector/LoggerDataCollector.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\RequestStack;
1717
use Symfony\Component\HttpFoundation\Response;
18+
use Symfony\Component\HttpKernel\Log\DebugLoggerConfigurator;
1819
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
1920
use Symfony\Component\VarDumper\Cloner\Data;
2021

@@ -25,18 +26,15 @@
2526
*/
2627
class LoggerDataCollector extends DataCollector implements LateDataCollectorInterface
2728
{
28-
private DebugLoggerInterface $logger;
29+
private ?DebugLoggerInterface $logger;
2930
private ?string $containerPathPrefix;
3031
private ?Request $currentRequest = null;
3132
private ?RequestStack $requestStack;
3233
private ?array $processedLogs = null;
3334

3435
public function __construct(object $logger = null, string $containerPathPrefix = null, RequestStack $requestStack = null)
3536
{
36-
if ($logger instanceof DebugLoggerInterface) {
37-
$this->logger = $logger;
38-
}
39-
37+
$this->logger = DebugLoggerConfigurator::getDebugLogger($logger);
4038
$this->containerPathPrefix = $containerPathPrefix;
4139
$this->requestStack = $requestStack;
4240
}
@@ -46,17 +44,9 @@ public function collect(Request $request, Response $response, \Throwable $except
4644
$this->currentRequest = $this->requestStack && $this->requestStack->getMainRequest() !== $request ? $request : null;
4745
}
4846

49-
public function reset(): void
50-
{
51-
if (isset($this->logger)) {
52-
$this->logger->clear();
53-
}
54-
parent::reset();
55-
}
56-
5747
public function lateCollect(): void
5848
{
59-
if (isset($this->logger)) {
49+
if ($this->logger) {
6050
$containerDeprecationLogs = $this->getContainerDeprecationLogs();
6151
$this->data = $this->computeErrorsCount($containerDeprecationLogs);
6252
// get compiler logs later (only when they are needed) to improve performance

src/Symfony/Component/HttpKernel/DependencyInjection/LoggerPass.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Psr\Log\LoggerInterface;
1515
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Definition;
1718
use Symfony\Component\DependencyInjection\Reference;
1819
use Symfony\Component\HttpFoundation\RequestStack;
1920
use Symfony\Component\HttpKernel\Log\Logger;
@@ -37,8 +38,21 @@ public function process(ContainerBuilder $container)
3738
return;
3839
}
3940

41+
if ($debug = $container->getParameter('kernel.debug')) {
42+
// Build an expression that will be equivalent to `!in_array(PHP_SAPI, ['cli', 'phpdbg'])`
43+
$debug = (new Definition('bool'))
44+
->setFactory('in_array')
45+
->setArguments([
46+
(new Definition('string'))->setFactory('constant')->setArguments(['PHP_SAPI']),
47+
['cli', 'phpdbg']
48+
]);
49+
$debug = (new Definition('bool'))
50+
->setFactory('in_array')
51+
->setArguments([$debug, [false]]);
52+
}
53+
4054
$container->register('logger', Logger::class)
41-
->setArguments([null, null, null, new Reference(RequestStack::class)])
55+
->setArguments([null, null, null, new Reference(RequestStack::class), $debug])
4256
->setPublic(false);
4357
}
4458
}

src/Symfony/Component/HttpKernel/EventListener/ErrorListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
2727
use Symfony\Component\HttpKernel\HttpKernelInterface;
2828
use Symfony\Component\HttpKernel\KernelEvents;
29-
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
29+
use Symfony\Component\HttpKernel\Log\DebugLoggerConfigurator;
3030

3131
/**
3232
* @author Fabien Potencier <fabien@symfony.com>
@@ -231,7 +231,7 @@ protected function duplicateRequest(\Throwable $exception, Request $request): Re
231231
$attributes = [
232232
'_controller' => $this->controller,
233233
'exception' => $exception,
234-
'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null,
234+
'logger' => DebugLoggerConfigurator::getDebugLogger($this->logger),
235235
];
236236
$request = $request->duplicate(null, null, $attributes);
237237
$request->setMethod('GET');
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Log;
13+
14+
use Monolog\Logger;
15+
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
16+
17+
/**
18+
* @author Nicolas Grekas <p@tchwork.com>
19+
*/
20+
class DebugLoggerConfigurator
21+
{
22+
private ?DebugLoggerInterface $processor = null;
23+
24+
public function __construct(DebugLoggerInterface $processor, bool $enable = null)
25+
{
26+
if ($enable ?? \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
27+
$this->processor = $processor;
28+
}
29+
}
30+
31+
public function pushDebugLogger(Logger $logger): void
32+
{
33+
if ($this->processor) {
34+
$logger->pushProcessor($this->processor);
35+
}
36+
}
37+
38+
public static function getDebugLogger(mixed $logger): ?DebugLoggerInterface
39+
{
40+
if ($logger instanceof DebugLoggerInterface) {
41+
return $logger;
42+
}
43+
44+
if (!$logger instanceof Logger) {
45+
return null;
46+
}
47+
48+
foreach ($logger->getProcessors() as $processor) {
49+
if ($processor instanceof DebugLoggerInterface) {
50+
return $processor;
51+
}
52+
}
53+
54+
return null;
55+
}
56+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Logger extends AbstractLogger implements DebugLoggerInterface
5757
/**
5858
* @param string|resource|null $output
5959
*/
60-
public function __construct(string $minLevel = null, $output = null, callable $formatter = null, private readonly ?RequestStack $requestStack = null)
60+
public function __construct(string $minLevel = null, $output = null, callable $formatter = null, private readonly ?RequestStack $requestStack = null, bool $debug = false)
6161
{
6262
if (null === $minLevel) {
6363
$minLevel = null === $output || 'php://stdout' === $output || 'php://stderr' === $output ? LogLevel::ERROR : LogLevel::WARNING;
@@ -82,6 +82,7 @@ public function __construct(string $minLevel = null, $output = null, callable $f
8282
if ($output && false === $this->handle = \is_resource($output) ? $output : @fopen($output, 'a')) {
8383
throw new InvalidArgumentException(sprintf('Unable to open "%s".', $output));
8484
}
85+
$this->debug = $debug;
8586
}
8687

8788
public function enableDebug(): void

src/Symfony/Component/HttpKernel/Tests/DataCollector/LoggerDataCollectorTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -167,18 +167,6 @@ public function testCollect($nb, $logs, $expectedLogs, $expectedDeprecationCount
167167
}
168168
}
169169

170-
public function testReset()
171-
{
172-
$logger = $this
173-
->getMockBuilder(DebugLoggerInterface::class)
174-
->onlyMethods(['countErrors', 'getLogs', 'clear'])
175-
->getMock();
176-
$logger->expects($this->once())->method('clear');
177-
178-
$c = new LoggerDataCollector($logger);
179-
$c->reset();
180-
}
181-
182170
public static function getCollectTestData()
183171
{
184172
yield 'simple log' => [

0 commit comments

Comments
 (0)
0