NullLogger not implements LoggerChannelInterface

Created on 12 June 2023, over 1 year ago
Updated 5 July 2024, 4 months ago

Problem/Motivation

\Drupal\monolog\Logger\MonologLoggerChannelFactory implements \Drupal\Core\Logger\LoggerChannelFactoryInterface. The get method should return a \Drupal\Core\Logger\LoggerChannelInterface (based on the interface), but the implementation in Monolog can return a \Psr\Log\NullLogger which does not implement \Drupal\Core\Logger\LoggerChannelInterface. This can result in errors in other modules;

TypeError: Cannot assign Psr\Log\NullLogger to property Foo::$logger of type Drupal\Core\Logger\LoggerChannelInterface

Steps to reproduce

Create a class with a property of type \Drupal\Core\Logger\LoggerChannelInterface. In the constructor, use the 'logger.factory' service and assign the value of get() to the property. This will fail for example during a site install, as get() returns \Psr\Log\NullLogger which not implements \Drupal\Core\Logger\LoggerChannelFactoryInterface.

Proposed resolution

Add a custom NullLogger to the module, which extends \Psr\Log\NullLogger and implements \Drupal\Core\Logger\LoggerChannelInterface. Use the custom NullLogger instead of the PSRs one in \Drupal\monolog\Logger\MonologLoggerChannelFactory.

I added a patch.

🐛 Bug report
Status

Fixed

Version

3.0

Component

Code

Created by

🇳🇱Netherlands Rik Wijnen

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