8000 [FrameworkBundle] Configure `logger` as error logger if the Monolog Bundle is not registered by MatTheCat · Pull Request #52009 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[FrameworkBundle] Configure logger as error logger if the Monolog B…
…undle is not registered
  • Loading branch information
MatTheCat authored and nicolas-grekas committed Oct 12, 2023
commit b7d25e85cf4758e9daf16fd020f43e46db879463
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