diff --git a/core/core.services.yml b/core/core.services.yml index a734c52..04cfbde 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -132,8 +132,8 @@ services: - [setContainer, ['@service_container']] logger.channel.default: class: Drupal\Core\Logger\LoggerChannel - tags: - - { name: logger_channel } + factory_method: get + factory_service: logger.factory settings: class: Drupal\Component\Utility\Settings factory_class: Drupal\Component\Utility\Settings diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 770e7f3..122da72 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -1366,7 +1366,7 @@ function watchdog($type, $message, array $variables = NULL, $severity = WATCHDOG if ($link) { $variables['link'] = $link; } - \Drupal::service('logger.factory')->getLogger($type)->log($severity, $message, $variables); + \Drupal::service('logger.factory')->get($type)->log($severity, $message, $variables); } /** diff --git a/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterLoggersPass.php b/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterLoggersPass.php index 3b6a82c..554bc9d 100644 --- a/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterLoggersPass.php +++ b/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterLoggersPass.php @@ -18,18 +18,12 @@ class RegisterLoggersPass implements CompilerPassInterface { * {@inheritdoc} */ public function process(ContainerBuilder $container) { - $definitions = array(); - foreach ($container->findTaggedServiceIds('logger_channel') as $id => $attributes) { - $definition = $container->getDefinition($id); - $definitions[] = $definition; - } + $factory = $container->getDefinition('logger.factory'); // Loop through all available logger services (eg dblog, syslog) and add - // the available logging channels + // them to the factory. foreach ($container->findTaggedServiceIds('logger') as $id => $attributes) { $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0; - foreach ($definitions as $definition) { - $definition->addMethodCall('addLogger', array(new Reference($id), $priority)); - } + $factory->addMethodCall('addLogger', array(new Reference($id), $priority)); } } diff --git a/core/lib/Drupal/Core/Logger/LoggerChannel.php b/core/lib/Drupal/Core/Logger/LoggerChannel.php index 0e32da6..d96c7cd 100644 --- a/core/lib/Drupal/Core/Logger/LoggerChannel.php +++ b/core/lib/Drupal/Core/Logger/LoggerChannel.php @@ -31,7 +31,7 @@ class LoggerChannel extends AbstractLogger { protected $levelTranslation; /** - * Array of \Psr\Log\LoggerInterface objects. + * An array of arrays of \Psr\Log\LoggerInterface keyed by priority. * * @var array */ @@ -118,6 +118,16 @@ public function setCurrentUser(AccountInterface $current_user = NULL) { } /** + * Sets the loggers for this channel. + * + * @param array $loggers + * An array of arrays of \Psr\Log\LoggerInterface keyed by priority. + */ + public function setLoggers(array $loggers) { + $this->loggers = $loggers; + } + + /** * Adds a logger. * * @param \Psr\Log\LoggerInterface $logger diff --git a/core/lib/Drupal/Core/Logger/LoggerChannelFactory.php b/core/lib/Drupal/Core/Logger/LoggerChannelFactory.php index 917aad7..b489eaf 100644 --- a/core/lib/Drupal/Core/Logger/LoggerChannelFactory.php +++ b/core/lib/Drupal/Core/Logger/LoggerChannelFactory.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Logger; +use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\ContainerAware; use Symfony\Component\DependencyInjection\Exception\RuntimeException; @@ -16,6 +17,20 @@ class LoggerChannelFactory extends ContainerAware { /** + * Array of all instantiated logger channels. + * + * @var array + */ + protected $channels = array(); + + /** + * Array of \Psr\Log\LoggerInterface objects. + * + * @var array + */ + protected $loggers = array(); + + /** * Retrieves the registered logger for the requested channel. * * If the channel does not exist, the default "system" channel is being used. @@ -23,25 +38,47 @@ class LoggerChannelFactory extends ContainerAware { * @return \Psr\Log\LoggerInterface * The registered logger for this channel. */ - public function getLogger($channel) { - $channel_service = 'logger.channel.' . str_replace(' ', '_', $channel); - if ($this->container->has($channel_service)) { - $logger = $this->container->get($channel_service); - } - else { - $logger = $this->container->get('logger.channel.default'); - } + public function get($channel = 'system') { + if (!isset($this->channels[$channel])) { + $instance = new LoggerChannel($channel); - // If the request is available, set it with the current user to the logger. - try { - $logger->setRequest($this->container->get('request')); - $logger->setCurrentUser($this->container->get('current_user')); - } - catch (RuntimeException $e) { - // We are not in a request context. + // If the request is available, set it with the current user to the channel. + try { + $instance->setRequest($this->container->get('request')); + $instance->setCurrentUser($this->container->get('current_user')); + } + catch (RuntimeException $e) { + // We are not in a request context. + } + + // Pass the loggers to the channel. + $instance->setLoggers($this->loggers); + $this->channels[$channel] = $instance; } - return $logger; + return $this->channels[$channel]; + } + + /** + * Adds a logger. + * + * Here is were all services tagged as 'logger' are being retrieved and then + * passed to the channels after instantiation. + * + * @param \Psr\Log\LoggerInterface $logger + * The PSR-3 logger to add. + * @param int $priority + * The priority of the logger being added. + * + * @see \Drupal\Core\DependencyInjection\Compiler\RegisterLoggersPass + */ + public function addLogger(LoggerInterface $logger, $priority = 0) { + // Store it so we can pass it to potential new logger instances. + $this->loggers[$priority][] = $logger; + // Add the logger to already instantiated channels. + foreach ($this->channels as $channel) { + $channel->addLogger($logger, $priority); + } } } diff --git a/core/modules/action/action.services.yml b/core/modules/action/action.services.yml deleted file mode 100644 index 709f763..0000000 --- a/core/modules/action/action.services.yml +++ /dev/null @@ -1,6 +0,0 @@ -services: - logger.channel.action: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['action'] - tags: - - { name: logger_channel } diff --git a/core/modules/aggregator/aggregator.services.yml b/core/modules/aggregator/aggregator.services.yml index 2ba3fdf..c00b2ac 100644 --- a/core/modules/aggregator/aggregator.services.yml +++ b/core/modules/aggregator/aggregator.services.yml @@ -16,8 +16,3 @@ services: aggregator.category.storage: class: Drupal\aggregator\CategoryStorageController arguments: ['@database'] - logger.channel.aggregator: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['aggregator'] - tags: - - { name: logger_channel } diff --git a/core/modules/block/custom_block/custom_block.services.yml b/core/modules/block/custom_block/custom_block.services.yml deleted file mode 100644 index faa8500..0000000 --- a/core/modules/block/custom_block/custom_block.services.yml +++ /dev/null @@ -1,6 +0,0 @@ -services: - logger.channel.custom_block: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['custom block'] - tags: - - { name: logger_channel } diff --git a/core/modules/contact/contact.services.yml b/core/modules/contact/contact.services.yml index 0776ae9..acb2866 100644 --- a/core/modules/contact/contact.services.yml +++ b/core/modules/contact/contact.services.yml @@ -4,8 +4,3 @@ services: tags: - { name: access_check, applies_to: _access_contact_personal_tab } arguments: ['@config.factory', '@user.data'] - logger.channel.contact: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['contact'] - tags: - - { name: logger_channel } diff --git a/core/modules/content_translation/content_translation.services.yml b/core/modules/content_translation/content_translation.services.yml index e2ff53d..da2acb2 100644 --- a/core/modules/content_translation/content_translation.services.yml +++ b/core/modules/content_translation/content_translation.services.yml @@ -24,9 +24,3 @@ services: content_translation.manager: class: Drupal\content_translation\ContentTranslationManager arguments: ['@entity.manager'] - - logger.channel.content_translation: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['content translation'] - tags: - - { name: logger_channel } diff --git a/core/modules/filter/filter.services.yml b/core/modules/filter/filter.services.yml index 7d3d157..c355c0c 100644 --- a/core/modules/filter/filter.services.yml +++ b/core/modules/filter/filter.services.yml @@ -13,8 +13,3 @@ services: plugin.manager.filter: class: Drupal\filter\FilterPluginManager parent: default_plugin_manager - logger.channel.filter: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['filter'] - tags: - - { name: logger_channel } diff --git a/core/modules/forum/forum.services.yml b/core/modules/forum/forum.services.yml index 1b8f516..f27ffc0 100644 --- a/core/modules/forum/forum.services.yml +++ b/core/modules/forum/forum.services.yml @@ -7,8 +7,3 @@ services: arguments: ['@entity.manager', '@config.factory', '@forum_manager'] tags: - { name: breadcrumb_builder, priority: 1001 } - logger.channel.forum: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['forum'] - tags: - - { name: logger_channel } diff --git a/core/modules/language/language.services.yml b/core/modules/language/language.services.yml index fb68808..422644a 100644 --- a/core/modules/language/language.services.yml +++ b/core/modules/language/language.services.yml @@ -11,8 +11,3 @@ services: class: Drupal\language\EventSubscriber\ConfigSubscriber tags: - { name: event_subscriber } - logger.channel.language: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['language'] - tags: - - { name: logger_channel } diff --git a/core/modules/locale/locale.services.yml b/core/modules/locale/locale.services.yml index cb074bf..b5f4cf4 100644 --- a/core/modules/locale/locale.services.yml +++ b/core/modules/locale/locale.services.yml @@ -16,8 +16,3 @@ services: tags: - { name: string_translator } - { name: needs_destruction } - logger.channel.locale: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['locale'] - tags: - - { name: logger_channel } diff --git a/core/modules/menu/menu.services.yml b/core/modules/menu/menu.services.yml deleted file mode 100644 index 32a2a76..0000000 --- a/core/modules/menu/menu.services.yml +++ /dev/null @@ -1,6 +0,0 @@ -services: - logger.channel.menu: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['menu'] - tags: - - { name: logger_channel } diff --git a/core/modules/node/node.services.yml b/core/modules/node/node.services.yml index 07f2076..7329685 100644 --- a/core/modules/node/node.services.yml +++ b/core/modules/node/node.services.yml @@ -12,13 +12,3 @@ services: arguments: ['@entity.manager'] tags: - { name: access_check, applies_to: _node_add_access } - logger.channel.content: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['content'] - tags: - - { name: logger_channel } - logger.channel.node: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['node'] - tags: - - { name: logger_channel } diff --git a/core/modules/rest/rest.services.yml b/core/modules/rest/rest.services.yml index c829bf7..7c63426 100644 --- a/core/modules/rest/rest.services.yml +++ b/core/modules/rest/rest.services.yml @@ -22,8 +22,3 @@ services: rest.link_manager.relation: class: Drupal\rest\LinkManager\RelationLinkManager arguments: ['@cache.cache'] - logger.channel.rest: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['rest'] - tags: - - { name: logger_channel } diff --git a/core/modules/search/search.services.yml b/core/modules/search/search.services.yml index 4b5e59e..a92d161 100644 --- a/core/modules/search/search.services.yml +++ b/core/modules/search/search.services.yml @@ -6,8 +6,3 @@ services: search.search_page_repository: class: Drupal\search\SearchPageRepository arguments: ['@config.factory', '@entity.manager'] - logger.channel.search: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['search'] - tags: - - { name: logger_channel } diff --git a/core/modules/system/system.services.yml b/core/modules/system/system.services.yml index fe5bafb..179bb27 100644 --- a/core/modules/system/system.services.yml +++ b/core/modules/system/system.services.yml @@ -24,68 +24,3 @@ services: class: Drupal\system\SystemConfigSubscriber tags: - { name: event_subscriber } - logger.channel.access_denied: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['access denied'] - tags: - - { name: logger_channel } - logger.channel.ajax: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['ajax'] - tags: - - { name: logger_channel } - logger.channel.cron: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['cron'] - tags: - - { name: logger_channel } - logger.channel.debug: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['debug'] - tags: - - { name: logger_channel } - logger.channel.file: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['file'] - tags: - - { name: logger_channel } - logger.channel.file_system: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['file system'] - tags: - - { name: logger_channel } - logger.channel.form: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['form'] - tags: - - { name: logger_channel } - logger.channel.image: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['image'] - tags: - - { name: logger_channel } - logger.channel.mail: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['mail'] - tags: - - { name: logger_channel } - logger.channel.theme: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['theme'] - tags: - - { name: logger_channel } - logger.channel.page_not_found: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['page not found'] - tags: - - { name: logger_channel } - logger.channel.php: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['php'] - tags: - - { name: logger_channel } - logger.channel.security: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['security'] - tags: - - { name: logger_channel } diff --git a/core/modules/taxonomy/taxonomy.services.yml b/core/modules/taxonomy/taxonomy.services.yml index e4be14e..686f6ea 100644 --- a/core/modules/taxonomy/taxonomy.services.yml +++ b/core/modules/taxonomy/taxonomy.services.yml @@ -3,8 +3,3 @@ services: class: Drupal\taxonomy\TermBreadcrumbBuilder tags: - { name: breadcrumb_builder, priority: 1002 } - logger.channel.taxonomy: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['taxonomy'] - tags: - - { name: logger_channel } diff --git a/core/modules/user/user.services.yml b/core/modules/user/user.services.yml index a91c700..23d8706 100644 --- a/core/modules/user/user.services.yml +++ b/core/modules/user/user.services.yml @@ -33,8 +33,3 @@ services: user.permissions_hash: class: Drupal\user\PermissionsHash arguments: ['@private_key', '@cache.cache'] - logger.channel.user: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['user'] - tags: - - { name: logger_channel } diff --git a/core/modules/views/views.services.yml b/core/modules/views/views.services.yml index b8c9d48..30aa96a 100644 --- a/core/modules/views/views.services.yml +++ b/core/modules/views/views.services.yml @@ -93,8 +93,3 @@ services: - { name: 'access_check' } views.exposed_form_cache: class: Drupal\views\ExposedFormCache - logger.channel.views: - class: Drupal\Core\Logger\LoggerChannel - arguments: ['views'] - tags: - - { name: logger_channel }