Skip to content

Commit 1da5efd

Browse files
committed
feature #47483 [HttpKernel] Make Logger implement DebugLoggerInterface (MatTheCat)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- [HttpKernel] Make Logger implement DebugLoggerInterface | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | N/A When starting a new project from a skeleton I was surprised not to see any log in the profiler and error pages. Turns out this depends of the logger implementing `DebugLoggerInterface` but AFAIK this only happen in Monolog’s bridge. Given the API makes it weird to implement it in userland (see symfony/symfony#47396) would it make sense to provide one by default? Commits ------- 3be04eddaa [HttpKernel] Make Logger implement DebugLoggerInterface
2 parents 50990a2 + 3abe766 commit 1da5efd

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

DependencyInjection/Compiler/AddDebugLogProcessorPass.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Reference;
17+
use Symfony\Component\HttpKernel\Log\Logger;
1718

1819
class AddDebugLogProcessorPass implements CompilerPassInterface
1920
{
@@ -22,22 +23,38 @@ public function process(ContainerBuilder $container)
2223
if (!$container->hasDefinition('profiler')) {
2324
return;
2425
}
25-
if (!$container->hasDefinition('monolog.logger_prototype')) {
26+
27+
if ($container->hasDefinition('monolog.logger_prototype') && $container->hasDefinition('debug.log_processor')) {
28+
$container->getDefinition('monolog.logger_prototype')
29+
->setConfigurator([__CLASS__, 'configureMonologLogger'])
30+
->addMethodCall('pushProcessor', [new Reference('debug.log_processor')])
31+
;
32+
2633
return;
2734
}
28-
if (!$container->hasDefinition('debug.log_processor')) {
35+
36+
if (!$container->hasDefinition('logger')) {
2937
return;
3038
}
3139

32-
$definition = $container->getDefinition('monolog.logger_prototype');
33-
$definition->setConfigurator([__CLASS__, 'configureLogger']);
34-
$definition->addMethodCall('pushProcessor', [new Reference('debug.log_processor')]);
40+
$loggerDefinition = $container->getDefinition('logger');
41+
42+
if (Logger::class === $loggerDefinition->getClass()) {
43+
$loggerDefinition->setConfigurator([__CLASS__, 'configureHttpKernelLogger']);
44+
}
3545
}
3646

37-
public static function configureLogger(mixed $logger)
47+
public static function configureMonologLogger(mixed $logger)
3848
{
39-
if (\is_object($logger) && method_exists($logger, 'removeDebugLogger') && \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
49+
if (\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && \is_object($logger) && method_exists($logger, 'removeDebugLogger')) {
4050
$logger->removeDebugLogger();
4151
}
4252
}
53+
54+
public static function configureHttpKernelLogger(Logger $logger)
55+
{
56+
if (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && method_exists($logger, 'enableDebug')) {
57+
$logger->enableDebug();
58+
}
59+
}
4360
}

0 commit comments

Comments
 (0)