Skip to content
Merged
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
68 changes: 20 additions & 48 deletions src/DependencyInjection/MonologExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
use Monolog\Processor\PsrLogMessageProcessor;
use Monolog\ResettableInterface;
use Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy;
use Symfony\Bridge\Monolog\Logger as LegacyLogger;
use Symfony\Bridge\Monolog\Processor\SwitchUserTokenProcessor;
use Symfony\Bridge\Monolog\Processor\TokenProcessor;
use Symfony\Bridge\Monolog\Processor\WebProcessor;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
use Symfony\Component\DependencyInjection\ChildDefinition;
Expand All @@ -31,18 +29,15 @@
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\HttpKernel\Log\DebugLoggerConfigurator;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* MonologExtension is an extension for the Monolog library.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Christophe Coevoet <stof@notk.org>
*
* @final since 3.9.0
*/
class MonologExtension extends Extension
final class MonologExtension extends Extension
{
private $nestedHandlers = [];

Expand All @@ -61,10 +56,6 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../config'));
$loader->load('monolog.php');

if (!class_exists(DebugLoggerConfigurator::class)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Min Symfony always has this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added in Symfony 6.4 with symfony/symfony#51284. I wonder if we should think about bumping the minimum required version of Symfony components in the 3.x branch to 6.4 and do some of the clean up work there already.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with the idea, if needed I'll open a PR that bumps Symfony in 3.x.

$container->getDefinition('monolog.logger_prototype')->setClass(LegacyLogger::class);
}

$container->setParameter('monolog.use_microseconds', $config['use_microseconds']);

$handlers = [];
Expand Down Expand Up @@ -95,43 +86,35 @@ public function load(array $configs, ContainerBuilder $container)

$container->setParameter('monolog.additional_channels', $config['channels'] ?? []);

if (interface_exists(ProcessorInterface::class)) {
$container->registerForAutoconfiguration(ProcessorInterface::class)
->addTag('monolog.processor');
} else {
$container->registerForAutoconfiguration(WebProcessor::class)
->addTag('monolog.processor');
}
if (interface_exists(ResettableInterface::class)) {
$container->registerForAutoconfiguration(ResettableInterface::class)
->addTag('kernel.reset', ['method' => 'reset']);
}
$container->registerForAutoconfiguration(ProcessorInterface::class)
->addTag('monolog.processor');
$container->registerForAutoconfiguration(ResettableInterface::class)
->addTag('kernel.reset', ['method' => 'reset']);
Comment on lines +89 to +92
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Min Monolog always has these interfaces.

$container->registerForAutoconfiguration(TokenProcessor::class)
->addTag('monolog.processor');

if (interface_exists(HttpClientInterface::class)) {
$handlerAutoconfiguration = $container->registerForAutoconfiguration(HandlerInterface::class);
$handlerAutoconfiguration->setBindings($handlerAutoconfiguration->getBindings() + [
HttpClientInterface::class => new BoundArgument(new Reference('monolog.http_client'), false),
]);
}

if (80000 <= \PHP_VERSION_ID) {
$container->registerAttributeForAutoconfiguration(AsMonologProcessor::class, static function (ChildDefinition $definition, AsMonologProcessor $attribute, \Reflector $reflector): void {
$tagAttributes = get_object_vars($attribute);
if ($reflector instanceof \ReflectionMethod) {
if (isset($tagAttributes['method'])) {
throw new \LogicException(\sprintf('AsMonologProcessor attribute cannot declare a method on "%s::%s()".', $reflector->class, $reflector->name));
}

$tagAttributes['method'] = $reflector->getName();
$container->registerAttributeForAutoconfiguration(AsMonologProcessor::class, static function (ChildDefinition $definition, AsMonologProcessor $attribute, \Reflector $reflector): void {
$tagAttributes = get_object_vars($attribute);
if ($reflector instanceof \ReflectionMethod) {
if (isset($tagAttributes['method'])) {
throw new \LogicException(\sprintf('AsMonologProcessor attribute cannot declare a method on "%s::%s()".', $reflector->class, $reflector->name));
}

$definition->addTag('monolog.processor', $tagAttributes);
});
$container->registerAttributeForAutoconfiguration(WithMonologChannel::class, static function (ChildDefinition $definition, WithMonologChannel $attribute): void {
$definition->addTag('monolog.logger', ['channel' => $attribute->channel]);
});
}
$tagAttributes['method'] = $reflector->getName();
}

$definition->addTag('monolog.processor', $tagAttributes);
});
$container->registerAttributeForAutoconfiguration(WithMonologChannel::class, static function (ChildDefinition $definition, WithMonologChannel $attribute): void {
$definition->addTag('monolog.logger', ['channel' => $attribute->channel]);
});
}

/**
Expand Down Expand Up @@ -899,23 +882,12 @@ private function getHandlerClassByType($handlerType)

private function buildPsrLogMessageProcessor(ContainerBuilder $container, array $processorOptions): string
{
static $hasConstructorArguments;

if (!isset($hasConstructorArguments)) {
$reflectionConstructor = (new \ReflectionClass(PsrLogMessageProcessor::class))->getConstructor();
$hasConstructorArguments = null !== $reflectionConstructor && $reflectionConstructor->getNumberOfParameters() > 0;
unset($reflectionConstructor);
}

$processorId = 'monolog.processor.psr_log_message';
$processorArguments = [];

unset($processorOptions['enabled']);

if (!empty($processorOptions)) {
if (!$hasConstructorArguments) {
throw new \RuntimeException('Monolog 1.26 or higher is required for the "date_format" and "remove_used_context_fields" options to be used.');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Min Monolog always has PsrLogMessageProcessor with these args.

}
if ($processorOptions) {
$processorArguments = [
$processorOptions['date_format'] ?? null,
$processorOptions['remove_used_context_fields'] ?? false,
Expand Down