Commit 900523ee authored by Luca Lusso's avatar Luca Lusso
Browse files

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

parent e368297b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@
    },
    "require": {
        "php": "^8.1.0",
        "monolog/monolog": "^3.0.0"
        "monolog/monolog": "^3.2.0"
    },
    "autoload": {
        "psr-0": {

drupalci.yml

0 → 100644
+54 −0
Original line number Diff line number Diff line
build:
  assessment:
    validate_codebase:
      phplint:
      csslint:
        halt-on-fail: false
      eslint:
        halt-on-fail: false
      phpcs:
        # phpcs will use core's specified version of Coder.
        sniff-all-files: true
        halt-on-fail: true
      phpstan:
        halt-on-fail: true
    testing:
      container_command.commit-checks:
        commands:
          - "./vendor/bin/phpstan analyse -c modules/contrib/monolog/phpstan.neon"
        halt-on-fail: true
      # run_tests task is executed several times in order of performance speeds.
      # halt-on-fail can be set on the run_tests tasks in order to fail fast.
      # suppress-deprecations is false in order to be alerted to usages of
      # deprecated code.
      run_tests.phpunit:
        types: 'PHPUnit-Unit'
        testgroups: '--all'
        suppress-deprecations: false
        halt-on-fail: false
      run_tests.kernel:
        types: 'PHPUnit-Kernel'
        testgroups: '--all'
        suppress-deprecations: false
        halt-on-fail: false
      run_tests.simpletest:
        types: 'Simpletest'
        testgroups: '--all'
        suppress-deprecations: false
        halt-on-fail: false
      run_tests.functional:
        types: 'PHPUnit-Functional'
        testgroups: '--all'
        suppress-deprecations: false
        halt-on-fail: false
      # Functional JavaScript tests require a concurrency of 1 because there is
      # only one instance of PhantomJS on the testbot machine.
      run_tests.javascript:
        concurrency: 1
        types: 'PHPUnit-FunctionalJavascript'
        testgroups: '--all'
        suppress-deprecations: false
        halt-on-fail: false
      # Run nightwatch testing.
      # @see https://www.drupal.org/project/drupal/issues/2869825
      nightwatchjs:

phpstan.neon

0 → 100644
+6 −0
Original line number Diff line number Diff line
parameters:
    ignoreErrors:
      - "#Unsafe usage of new static#"
    level: 5
    paths:
      - %currentWorkingDirectory%/modules/contrib/monolog

src/LevelMapperTrait.php

deleted100644 → 0
+0 −57
Original line number Diff line number Diff line
<?php

namespace Drupal\monolog;

use Drupal\Core\Logger\RfcLogLevel;
use Monolog\Level;

/**
 * Methods to convert between Monolog and Drupal log levels.
 */
trait LevelMapperTrait {

  /**
   * Convert a Drupal log level to a Monolog log level.
   *
   * @param int $level
   *   The Drupal log level.
   *
   * @return \Monolog\Level
   *   The Monolog log level.
   */
  public function fromRfc5424ToMonolog(int $level): Level {
    return match ($level) {
      RfcLogLevel::EMERGENCY => Level::Emergency,
      RfcLogLevel::ALERT => Level::Alert,
      RfcLogLevel::CRITICAL => Level::Critical,
      RfcLogLevel::ERROR => Level::Error,
      RfcLogLevel::WARNING => Level::Warning,
      RfcLogLevel::NOTICE => Level::Notice,
      RfcLogLevel::INFO => Level::Info,
      default => Level::Debug,
    };
  }

  /**
   * Convert a Monolog log level to a Drupal log level.
   *
   * @param \Monolog\Level $level
   *   The Monolog log level.
   *
   * @return int
   *   The Drupal log level.
   */
  public function fromMonologToRfc5424(Level $level): int {
    return match ($level) {
      Level::Emergency => RfcLogLevel::EMERGENCY,
      Level::Alert => RfcLogLevel::ALERT,
      Level::Critical => RfcLogLevel::CRITICAL,
      Level::Error => RfcLogLevel::ERROR,
      Level::Warning => RfcLogLevel::WARNING,
      Level::Notice => RfcLogLevel::NOTICE,
      Level::Info => RfcLogLevel::INFO,
      default => RfcLogLevel::DEBUG,
    };
  }

}
+25 −6
Original line number Diff line number Diff line
@@ -4,10 +4,9 @@ declare(strict_types=1);

namespace Drupal\monolog\Logger\Handler;

use Drupal\monolog\LevelMapperTrait;
use Drupal\Core\Logger\RfcLogLevel;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Level;
use Monolog\LevelName;
use Monolog\LogRecord;
use Psr\Log\LoggerInterface;

@@ -16,8 +15,6 @@ use Psr\Log\LoggerInterface;
 */
class DrupalHandler extends AbstractProcessingHandler {

  use LevelMapperTrait;

  /**
   * The wrapped Drupal logger.
   *
@@ -30,14 +27,14 @@ class DrupalHandler extends AbstractProcessingHandler {
   *
   * @param \Psr\Log\LoggerInterface $wrapped
   *   The wrapped Drupal logger.
   * @param int|string|\Monolog\Level|\Monolog\LevelName|\Psr\Log\LogLevel $level
   * @param int|string|\Monolog\Level $level
   *   The minimum logging level at which this handler will be triggered.
   * @param bool $bubble
   *   Whether the messages that are handled can bubble up the stack or not.
   */
  public function __construct(
    LoggerInterface $wrapped,
    int|string|Level|LevelName $level = Level::Debug,
    int|string|Level $level = Level::Debug,
    bool $bubble = TRUE
  ) {
    parent::__construct($level, $bubble);
@@ -69,4 +66,26 @@ class DrupalHandler extends AbstractProcessingHandler {
    );
  }

  /**
   * Convert a Monolog log level to a Drupal log level.
   *
   * @param \Monolog\Level $level
   *   The Monolog log level.
   *
   * @return int
   *   The Drupal log level.
   */
  private function fromMonologToRfc5424(Level $level): int {
    return match ($level) {
      Level::Emergency => RfcLogLevel::EMERGENCY,
      Level::Alert => RfcLogLevel::ALERT,
      Level::Critical => RfcLogLevel::CRITICAL,
      Level::Error => RfcLogLevel::ERROR,
      Level::Warning => RfcLogLevel::WARNING,
      Level::Notice => RfcLogLevel::NOTICE,
      Level::Info => RfcLogLevel::INFO,
      default => RfcLogLevel::DEBUG,
    };
  }

}
Loading