Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x

LoggerAwarePass.php

Namespace

Drupal\Core\DependencyInjection\Compiler

File

core/lib/Drupal/Core/DependencyInjection/Compiler/LoggerAwarePass.php

View source
<?php

namespace Drupal\Core\DependencyInjection\Compiler;

use Psr\Log\LoggerAwareInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
 * Sets the logger on all services that implement LoggerAwareInterface.
 */
class LoggerAwarePass implements CompilerPassInterface {
    
    /**
     * {@inheritdoc}
     */
    public function process(ContainerBuilder $container) : void {
        $interface = LoggerAwareInterface::class;
        foreach ($container->findTaggedServiceIds('logger_aware') as $id => $attributes) {
            $definition = $container->getDefinition($id);
            // Skip services that are already calling setLogger().
            if ($definition->hasMethodCall('setLogger')) {
                continue;
            }
            if (!is_subclass_of($definition->getClass(), $interface)) {
                throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
            }
            $providerTag = $definition->getTag('_provider');
            $loggerId = 'logger.channel.' . $providerTag[0]['provider'];
            if ($container->has($loggerId)) {
                $definition->addMethodCall('setLogger', [
                    new Reference($loggerId),
                ]);
            }
        }
    }

}

Classes

Title Deprecated Summary
LoggerAwarePass Sets the logger on all services that implement LoggerAwareInterface.
RSS feed
Powered by Drupal