Loading src/Logger/Handler/DrupalMailHandler.php +2 −0 Original line number Diff line number Diff line Loading @@ -44,9 +44,11 @@ class DrupalMailHandler extends MailHandler { * {@inheritdoc} */ protected function send(string $content, array $records): void { /** @phpstan-ignore-next-line */ $mail = \Drupal::service('plugin.manager.mail'); assert($mail instanceof MailManagerInterface); /** @phpstan-ignore-next-line */ $default_language = \Drupal::languageManager()->getDefaultLanguage(); $params = [ Loading src/Logger/LoggerInterfacesAdapter.php 0 → 100644 +125 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\monolog\Logger; use Drupal\Core\Logger\LoggerChannelInterface; use Drupal\Core\Session\AccountInterface; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\RequestStack; /** * Adapt Psr\Log\LoggerInterface to Drupal\Core\Logger\LoggerChannelInterface. */ class LoggerInterfacesAdapter implements LoggerChannelInterface { /** * A Psr\Log\LoggerInterface logger. * * @var \Psr\Log\LoggerInterface */ private LoggerInterface $logger; /** * Adapter constructor. * * @param LoggerInterface $logger * A Psr\Log\LoggerInterface logger. */ public function __construct(LoggerInterface $logger) { $this->logger = $logger; } /** * {@inheritdoc} */ public function setRequestStack(RequestStack $requestStack = NULL) { // Do nothing, use a handler for this. } /** * {@inheritdoc} */ public function setCurrentUser(AccountInterface $current_user = NULL) { // Do nothing, use a handler for this. } /** * {@inheritdoc} */ public function setLoggers(array $loggers) { // Do nothing. } /** * {@inheritdoc} */ public function addLogger(LoggerInterface $logger, $priority = 0) { // Do nothing. } /** * {@inheritdoc} */ public function emergency(\Stringable|string $message, array $context = []): void { $this->logger->emergency($message, $context); } /** * {@inheritdoc} */ public function alert(\Stringable|string $message, array $context = []): void { $this->logger->alert($message, $context); } /** * {@inheritdoc} */ public function critical(\Stringable|string $message, array $context = []): void { $this->logger->critical($message, $context); } /** * {@inheritdoc} */ public function error(\Stringable|string $message, array $context = []): void { $this->logger->error($message, $context); } /** * {@inheritdoc} */ public function warning(\Stringable|string $message, array $context = []): void { $this->logger->warning($message, $context); } /** * {@inheritdoc} */ public function notice(\Stringable|string $message, array $context = []): void { $this->logger->notice($message, $context); } /** * {@inheritdoc} */ public function info(\Stringable|string $message, array $context = []): void { $this->logger->info($message, $context); } /** * {@inheritdoc} */ public function debug(\Stringable|string $message, array $context = []): void { $this->logger->debug($message, $context); } /** * {@inheritdoc} */ public function log($level, \Stringable|string $message, array $context = []): void { $this->logger->log($level, $message, $context); } } src/Logger/MonologLoggerChannelFactory.php +22 −3 Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Drupal\monolog\Logger; use Drupal\Core\Messenger\MessengerInterface; use Monolog\Logger; use Psr\Log\NullLogger; use Psr\Log\LoggerInterface; Loading Loading @@ -35,7 +36,7 @@ class MonologLoggerChannelFactory implements LoggerChannelFactoryInterface, Cont /** * Array of all instantiated logger channels keyed by channel name. * * @var \Drupal\Core\Logger\LoggerChannelInterface[] * @var \Psr\Log\LoggerInterface[] */ protected array $channels = []; Loading @@ -46,6 +47,23 @@ class MonologLoggerChannelFactory implements LoggerChannelFactoryInterface, Cont */ protected array $enabledProcessors; /** * The Messenger service. * * @var \Drupal\Core\Messenger\MessengerInterface */ private MessengerInterface $messenger; /** * MonologLoggerChannelFactory constructor. * * @param \Drupal\Core\Messenger\MessengerInterface $messenger * The Messenger service. */ public function __construct(MessengerInterface $messenger) { $this->messenger = $messenger; } /** * {@inheritdoc} */ Loading @@ -58,12 +76,12 @@ class MonologLoggerChannelFactory implements LoggerChannelFactoryInterface, Cont $this->channels[$channel] = new NullLogger(); if ($this->container->get('current_user') ->hasPermission('administer site configuration')) { \Drupal::messenger()->addError($e->getMessage()); $this->messenger->addError($e->getMessage()); } } } return $this->channels[$channel]; return new LoggerInterfacesAdapter($this->channels[$channel]); } /** Loading Loading @@ -106,6 +124,7 @@ class MonologLoggerChannelFactory implements LoggerChannelFactoryInterface, Cont * The service container or null. */ private function getContainer(): OptionalLogger { /** @phpstan-ignore-next-line */ return $this->container ? OptionalLogger::of($this->container) : OptionalLogger::none(); Loading src/MonologServiceProvider.php +1 −0 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ class MonologServiceProvider extends ServiceProviderBase { $definition = $container->getDefinition('logger.factory'); $definition ->setClass('Drupal\monolog\Logger\MonologLoggerChannelFactory') ->addArgument(new Reference('messenger')) ->clearTags(); // Allow existing Drupal loggers to be added as handlers. Loading Loading
src/Logger/Handler/DrupalMailHandler.php +2 −0 Original line number Diff line number Diff line Loading @@ -44,9 +44,11 @@ class DrupalMailHandler extends MailHandler { * {@inheritdoc} */ protected function send(string $content, array $records): void { /** @phpstan-ignore-next-line */ $mail = \Drupal::service('plugin.manager.mail'); assert($mail instanceof MailManagerInterface); /** @phpstan-ignore-next-line */ $default_language = \Drupal::languageManager()->getDefaultLanguage(); $params = [ Loading
src/Logger/LoggerInterfacesAdapter.php 0 → 100644 +125 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\monolog\Logger; use Drupal\Core\Logger\LoggerChannelInterface; use Drupal\Core\Session\AccountInterface; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\RequestStack; /** * Adapt Psr\Log\LoggerInterface to Drupal\Core\Logger\LoggerChannelInterface. */ class LoggerInterfacesAdapter implements LoggerChannelInterface { /** * A Psr\Log\LoggerInterface logger. * * @var \Psr\Log\LoggerInterface */ private LoggerInterface $logger; /** * Adapter constructor. * * @param LoggerInterface $logger * A Psr\Log\LoggerInterface logger. */ public function __construct(LoggerInterface $logger) { $this->logger = $logger; } /** * {@inheritdoc} */ public function setRequestStack(RequestStack $requestStack = NULL) { // Do nothing, use a handler for this. } /** * {@inheritdoc} */ public function setCurrentUser(AccountInterface $current_user = NULL) { // Do nothing, use a handler for this. } /** * {@inheritdoc} */ public function setLoggers(array $loggers) { // Do nothing. } /** * {@inheritdoc} */ public function addLogger(LoggerInterface $logger, $priority = 0) { // Do nothing. } /** * {@inheritdoc} */ public function emergency(\Stringable|string $message, array $context = []): void { $this->logger->emergency($message, $context); } /** * {@inheritdoc} */ public function alert(\Stringable|string $message, array $context = []): void { $this->logger->alert($message, $context); } /** * {@inheritdoc} */ public function critical(\Stringable|string $message, array $context = []): void { $this->logger->critical($message, $context); } /** * {@inheritdoc} */ public function error(\Stringable|string $message, array $context = []): void { $this->logger->error($message, $context); } /** * {@inheritdoc} */ public function warning(\Stringable|string $message, array $context = []): void { $this->logger->warning($message, $context); } /** * {@inheritdoc} */ public function notice(\Stringable|string $message, array $context = []): void { $this->logger->notice($message, $context); } /** * {@inheritdoc} */ public function info(\Stringable|string $message, array $context = []): void { $this->logger->info($message, $context); } /** * {@inheritdoc} */ public function debug(\Stringable|string $message, array $context = []): void { $this->logger->debug($message, $context); } /** * {@inheritdoc} */ public function log($level, \Stringable|string $message, array $context = []): void { $this->logger->log($level, $message, $context); } }
src/Logger/MonologLoggerChannelFactory.php +22 −3 Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Drupal\monolog\Logger; use Drupal\Core\Messenger\MessengerInterface; use Monolog\Logger; use Psr\Log\NullLogger; use Psr\Log\LoggerInterface; Loading Loading @@ -35,7 +36,7 @@ class MonologLoggerChannelFactory implements LoggerChannelFactoryInterface, Cont /** * Array of all instantiated logger channels keyed by channel name. * * @var \Drupal\Core\Logger\LoggerChannelInterface[] * @var \Psr\Log\LoggerInterface[] */ protected array $channels = []; Loading @@ -46,6 +47,23 @@ class MonologLoggerChannelFactory implements LoggerChannelFactoryInterface, Cont */ protected array $enabledProcessors; /** * The Messenger service. * * @var \Drupal\Core\Messenger\MessengerInterface */ private MessengerInterface $messenger; /** * MonologLoggerChannelFactory constructor. * * @param \Drupal\Core\Messenger\MessengerInterface $messenger * The Messenger service. */ public function __construct(MessengerInterface $messenger) { $this->messenger = $messenger; } /** * {@inheritdoc} */ Loading @@ -58,12 +76,12 @@ class MonologLoggerChannelFactory implements LoggerChannelFactoryInterface, Cont $this->channels[$channel] = new NullLogger(); if ($this->container->get('current_user') ->hasPermission('administer site configuration')) { \Drupal::messenger()->addError($e->getMessage()); $this->messenger->addError($e->getMessage()); } } } return $this->channels[$channel]; return new LoggerInterfacesAdapter($this->channels[$channel]); } /** Loading Loading @@ -106,6 +124,7 @@ class MonologLoggerChannelFactory implements LoggerChannelFactoryInterface, Cont * The service container or null. */ private function getContainer(): OptionalLogger { /** @phpstan-ignore-next-line */ return $this->container ? OptionalLogger::of($this->container) : OptionalLogger::none(); Loading
src/MonologServiceProvider.php +1 −0 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ class MonologServiceProvider extends ServiceProviderBase { $definition = $container->getDefinition('logger.factory'); $definition ->setClass('Drupal\monolog\Logger\MonologLoggerChannelFactory') ->addArgument(new Reference('messenger')) ->clearTags(); // Allow existing Drupal loggers to be added as handlers. Loading