Commit ea4ae75f authored by Luca Lusso's avatar Luca Lusso
Browse files

Issue #3294236 by lussoluca: Move level mapping between rfc and monolog upstream

parent 900523ee
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -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 = [
+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);
  }

}
+22 −3
Original line number Diff line number Diff line
@@ -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;
@@ -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 = [];

@@ -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}
   */
@@ -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]);
  }

  /**
@@ -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();
+1 −0
Original line number Diff line number Diff line
@@ -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.