You are here

public function MonologServiceProvider::alter in Monolog 8

Same name and namespace in other branches
  1. 2.x src/MonologServiceProvider.php \Drupal\monolog\MonologServiceProvider::alter()

Modifies existing service definitions.

Parameters

ContainerBuilder $container: The ContainerBuilder whose service definitions can be altered.

Overrides ServiceProviderBase::alter

File

src/MonologServiceProvider.php, line 17

Class

MonologServiceProvider
Overrides the logger.factory service with the monolog factory.

Namespace

Drupal\monolog

Code

public function alter(ContainerBuilder $container) {
  $definition = $container
    ->getDefinition('logger.factory');
  $definition
    ->setClass('Drupal\\monolog\\Logger\\MonologLoggerChannelFactory')
    ->clearTags();

  // Allow existing Drupal loggers to be added as handlers.
  $drupalLoggers = $container
    ->findTaggedServiceIds('logger');
  foreach ($drupalLoggers as $id => $tags) {
    $handlerId = sprintf('monolog.handler.drupal.%s', preg_replace('/^logger\\./', '', $id));

    // Allow the handler to be explicitly defined elsewhere.
    if (!$container
      ->has($handlerId)) {
      $definition = $container
        ->register($handlerId, 'Drupal\\monolog\\Logger\\Handler\\DrupalHandler');
      $definition
        ->addArgument(new Reference($id));
    }
  }
}