Add autoconfigure for module loggers

Created on 18 October 2023, about 1 year ago
Updated 21 March 2024, 9 months ago

Problem/Motivation

I started dipping my toes into autowiring yesterday and one of the first things I butted up against was how to autowire loggers.

If a module needs to log something, most of the time it will do something like this:

services:
  logger.channel.my_module:
    parent: logger.channel_base
    arguments: ['my_module']

  my_module.service:
    class: Drupal\my_module\Foo
    arguments: ['@logger.channel.my_module']

I discovered in my own discussions internally that we can use calls in the service to avoid having to pass the logger into the constructor by using LoggerAwareTrait

  my_module.service:
    class: Drupal\my_module\Foo
    calls:
      - [setLogger, ['@logger.channel.my_module']]

That works well, but what if we automated it?

We could detect services implementing LoggerAwareInterface and autoconfigure a logger for the module and automate the calls part of the service.

Proposed resolution

Add a compiler pass that finds services implementing LoggerAwareInterface and autoconfigure a logger for them using setLogger with a logger service that matches the service's module name.

Remaining tasks

Agree
Code
Test

User interface changes

API changes

Data model changes

Release notes snippet

✨ Feature request
Status

Fixed

Version

10.2 ✨

Component
BaseΒ  β†’

Last updated 2 days ago

Created by

πŸ‡¦πŸ‡ΊAustralia acbramley

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024