Commit 32116412 authored by Jürgen Haas's avatar Jürgen Haas
Browse files

Issue #3261305 by jurgenhaas: Add an action to write a log message

parent ec5e3850
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
name: 'ECA Log'
type: module
description: 'Events and actions for Drupal log messages.'
core_version_requirement: ^9
package: ECA
dependencies:
  - eca:eca
+13 −0
Original line number Diff line number Diff line
services:

  eca_log.subscriber:
    class: Drupal\eca_log\EventSubscriber\EcaLog
    arguments: ['@eca.processor']
    tags:
      - { name: event_subscriber }

  logger.eca_log:
    class: Drupal\eca_log\Logger\EcaLog
    arguments: ['@event_dispatcher']
    tags:
      - { name: logger }
+88 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\eca_log\Event;

use Drupal\Component\EventDispatcher\Event;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\eca\Event\ConditionalApplianceInterface;

/**
 * Class LogMessageEvent
 *
 * @package Drupal\eca_log\Event
 */
class LogMessageEvent extends Event implements ConditionalApplianceInterface {

  /**
   * @var int
   */
  protected int $severity;

  /**
   * @var string
   */
  protected string $message;

  /**
   * @var array
   */
  protected array $context;

  /**
   * Construct a LogMessageEvent.
   *
   * @param int $severity
   * @param string $message
   * @param array $context
   */
  public function __construct(int $severity, string $message, array $context) {
    $this->severity = $severity;
    $this->message = $message;
    $this->context = $context;
  }

  /**
   * @return string[]
   */
  public static function fields(): array {
    return [
      [
        'name' => 'channel',
        'label' => 'Channel',
        'type' => 'String',
      ],
      [
        'name' => 'min_severity',
        'label' => 'Minimum severity',
        'type' => 'Dropdown',
        'extras' => [
          'choices' => self::severities(),
        ],
      ]
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function applies(string $id, array $arguments): bool {
    return (($arguments['channel'] === '') || ($arguments['channel'] === $this->context['channel'])) && $this->severity <= $arguments['min_severity'];
  }

  /**
   * Prepare log levels for drop down fields.
   *
   * @return array
   */
  protected static function severities(): array {
    $severities = [];
    foreach (RfcLogLevel::getLevels() as $level => $label) {
      $severities[] = [
        'name' => $label,
        'value' => (string) $level,
      ];
    }
    return $severities;
  }

}
+24 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\eca_log\EventSubscriber;

use Drupal\eca\EventSubscriber\EcaBase;
use Drupal\eca_log\Plugin\ECA\Event\LogEvent;

/**
 * ECA log event subscriber.
 */
class EcaLog extends EcaBase {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents(): array {
    $events = [];
    foreach (LogEvent::actions() as $action) {
      $events[$action['drupal_id']][] = ['onEvent'];
    }
    return $events;
  }

}
+19 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\eca_log;

/**
 * Defines events provided by the ECA Log module.
 */
final class LogEvents {

  /**
   * Dispatches an event when a log message is being created.
   *
   * @Event
   *
   * @var string
   */
  public const MESSAGE = 'eca_log.message';

}
Loading