Use logger channel services consistently, instead of the factory object

Created on 12 July 2022, over 2 years ago
Updated 25 January 2023, about 2 years ago

Problem/Motivation

Spotted while understanding #3254307: [PP-1] Make clearer that classes in contributed modules should not generally add the LoggerChannelTrait trait β†’

Core does not consistently use logger services. Many modules create logger.channel.[module] and then inject the channel directly, e.g. logger.channel.user or logger.channel.layout_builder.

However in other places we inject the logger factory instead and the module retrieves the channel in the constructor, for example Drupal\media\OEmbed\ProviderRepository - there is no logger.channel.media defined:

    $this->logger = $logger_factory->get('media');

We are even inconsistent within modules, e.g. rest.module declares a channel service:

  logger.channel.rest:
    parent: logger.channel_base
    arguments: ['rest']

But then some plugins use the factory instead:

core/modules/user/src/Plugin/rest/resource/UserRegistrationResource.php:      $container->get('logger.factory')->get('rest'),
core/modules/rest/src/Plugin/ResourceBase.php:      $container->get('logger.factory')->get('rest')
core/modules/rest/src/Plugin/rest/resource/EntityResource.php:      $container->get('logger.factory')->get('rest'),
core/modules/file/src/Plugin/rest/resource/FileUploadResource.php:      $container->get('logger.factory')->get('rest'),

This mixture of patterns is not a good example for contrib or custom code to follow.

Steps to reproduce

Proposed resolution

Consistently use the logger.channel.[module] format everywhere, declaring new services where appropriate.

Consider if we can make logger.factory private so it can only be injected into logger channel services but not retrieved from the container.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Needs work

Version

10.1 ✨

Component
BaseΒ  β†’

Last updated about 1 hour ago

Created by

πŸ‡¬πŸ‡§United Kingdom longwave UK

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024