Skip to content

Commit

Permalink
bug #52009 [FrameworkBundle] Configure logger as error logger if th…
Browse files Browse the repository at this point in the history
…e Monolog Bundle is not registered (MatTheCat)

This PR was squashed before being merged into the 5.4 branch.

Discussion
----------

[FrameworkBundle] Configure `logger` as error logger if the Monolog Bundle is not registered

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | Not sure
| New feature?  | Not sure
| Deprecations? | no
| Tickets       | Fix #51910
| License       | MIT

Commits
-------

b7d25e8 [FrameworkBundle] Configure `logger` as error logger if the Monolog Bundle is not registered
  • Loading branch information
nicolas-grekas committed Oct 12, 2023
2 parents 4290999 + b7d25e8 commit f06a752
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* @internal
*/
class ErrorLoggerCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('debug.debug_handlers_listener')) {
return;
}

$definition = $container->getDefinition('debug.debug_handlers_listener');
if ($container->hasDefinition('monolog.logger.php')) {
$definition->replaceArgument(1, new Reference('monolog.logger.php'));
}
if ($container->hasDefinition('monolog.logger.deprecation')) {
$definition->replaceArgument(6, new Reference('monolog.logger.deprecation'));
}
}
}
3 changes: 3 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AssetsContextPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ContainerBuilderDebugDumpPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\DataCollectorTranslatorPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ErrorLoggerCompilerPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggingTranslatorPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RemoveUnusedSessionMarshallingHandlerPass;
Expand Down Expand Up @@ -160,6 +161,8 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new RegisterReverseContainerPass(false), PassConfig::TYPE_AFTER_REMOVING);
$container->addCompilerPass(new RemoveUnusedSessionMarshallingHandlerPass());
$container->addCompilerPass(new SessionPass());
// must be registered after MonologBundle's LoggerChannelPass
$container->addCompilerPass(new ErrorLoggerCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);

if ($container->getParameter('kernel.debug')) {
$container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
->set('debug.debug_handlers_listener', DebugHandlersListener::class)
->args([
null, // Exception handler
service('monolog.logger.php')->nullOnInvalid(),
service('logger')->nullOnInvalid(),
null, // Log levels map for enabled error levels
param('debug.error_handler.throw_at'),
param('kernel.debug'),
param('kernel.debug'),
service('monolog.logger.deprecation')->nullOnInvalid(),
service('logger')->nullOnInvalid(),
])
->tag('kernel.event_subscriber')
->tag('monolog.logger', ['channel' => 'php'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ public function testEnabledPhpErrorsConfig()
$container = $this->createContainerFromFile('php_errors_enabled');

$definition = $container->getDefinition('debug.debug_handlers_listener');
$this->assertEquals(new Reference('monolog.logger.php', ContainerInterface::NULL_ON_INVALID_REFERENCE), $definition->getArgument(1));
$this->assertEquals(new Reference('logger', ContainerInterface::NULL_ON_INVALID_REFERENCE), $definition->getArgument(1));
$this->assertNull($definition->getArgument(2));
$this->assertSame(-1, $container->getParameter('debug.error_handler.throw_at'));
}
Expand All @@ -527,7 +527,7 @@ public function testPhpErrorsWithLogLevel()
$container = $this->createContainerFromFile('php_errors_log_level');

$definition = $container->getDefinition('debug.debug_handlers_listener');
$this->assertEquals(new Reference('monolog.logger.php', ContainerInterface::NULL_ON_INVALID_REFERENCE), $definition->getArgument(1));
$this->assertEquals(new Reference('logger', ContainerInterface::NULL_ON_INVALID_REFERENCE), $definition->getArgument(1));
$this->assertSame(8, $definition->getArgument(2));
}

Expand All @@ -536,7 +536,7 @@ public function testPhpErrorsWithLogLevels()
$container = $this->createContainerFromFile('php_errors_log_levels');

$definition = $container->getDefinition('debug.debug_handlers_listener');
$this->assertEquals(new Reference('monolog.logger.php', ContainerInterface::NULL_ON_INVALID_REFERENCE), $definition->getArgument(1));
$this->assertEquals(new Reference('logger', ContainerInterface::NULL_ON_INVALID_REFERENCE), $definition->getArgument(1));
$this->assertSame([
\E_NOTICE => \Psr\Log\LogLevel::ERROR,
\E_WARNING => \Psr\Log\LogLevel::ERROR,
Expand Down

0 comments on commit f06a752

Please sign in to comment.