I wanted to override the logger.filelog service from filelog module to make use of my own parser.
An error occurred through drush cim
and drush cr
when the new custom module that overrides the service is enabled.
The website encountered an unexpected error. Please try again later.
Symfony\Component\DependencyInjection\Exception\LogicException: Service 'logger.filelog' for consumer 'logger.factory' does not implement Psr\Log\LoggerInterface. in Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass->processServiceCollectorPass() (line 182 of /app/docroot/core/lib/Drupal/Core/DependencyInjection/Compiler/TaggedHandlersPass.php).
My custom modules structure looks like this
custom_module
- src
- Logger
- TestFilelog.php
CustomModuleServiceProvider.php
custom_module.info.yml
custom_module.module
Currently the service provider class has implemented ServiceModifierInterface
and altered the original logger.filelog service by setting its class to Drupal\custom_module\Logger\TestFilelog
.
/**
* Class to override contrib module logger.filelog service.
*/
class CustomModuleServiceProvider implements ServiceModifierInterface {
/**
* {@inheritdoc}
*/
public function alter(ContainerBuilder $container) {
if ($container->has('logger.filelog')) {
$definition = $container->getDefinition('logger.filelog');
$definition->setClass('Drupal\custom_module\Logger\TestFilelog');
}
}
}
TestFilelog.php
namespace Drupal\custom_module\Logger;
use Drupal\filelog\Logger\FileLog;
/**
* File-based logger.
*/
class TestFileLog extends FileLog {
/**
* Renders a message to a string.
*
* @param mixed $level
* Severity level of the log message.
* @param string $message
* Content of the log message.
* @param array $context
* Context of the log message.
*
* @return string
* The formatted message.
*/
protected function render($level, $message, array $context = []): string {
$plainString = parent::render($level, $message, $context);
$custom_channel = [
'filter_custom_channel'
];
if (in_array($context['channel'], $custom_channel)) {
$plainString = $this->parseStrMasking($plainString);
}
return $plainString;
}
/**
* Mask personal details from strings to logs.
*
* @param string $pureString
* The string to be parsed.
*
* @return string
* The string that has been masked.
*/
protected function parseStrMasking(string $pureString) {
// Default to return finalString.
$finalString = $pureString;
// Custom parsing goes here.
return $finalString;
}
}
I see that Drupal\filelog\Logger\FileLog;
has use RfcLoggerTrait;
which does implement the Psr\Log\LoggerInterface
, what am I missing here??