Commit 16d3daeb authored by Sven Decabooter's avatar Sven Decabooter Committed by Sven Decabooter
Browse files

Issue #3263403 by svendecabooter: Add wrapper class for multiple logs against an entity

parent 4c3cdf77
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -2,6 +2,12 @@ services:
  entity_logger:
    class: Drupal\entity_logger\EntityLogger
    arguments: [ '@entity_type.manager', '@config.factory', '@logger.factory', '@logger.log_message_parser' ]
  entity_logger.instance:
    class: Drupal\entity_logger\EntityLoggerInstance
    arguments: [ '@entity_logger' ]
  entity_logger.instance.factory:
    class: Drupal\entity_logger\EntityLoggerInstanceFactory
    arguments: [ '@entity_logger.instance' ]
  entity_logger.route_subscriber:
    class: Drupal\entity_logger\Routing\RouteSubscriber
    arguments: [ '@entity_type.manager' ]
+103 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\entity_logger;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Logger\RfcLogLevel;

/**
 * Defines an instance class, to log against a specified entity and log channel.
 */
class EntityLoggerInstance implements EntityLoggerInstanceInterface {

  /**
   * The entity logger service.
   *
   * @var \Drupal\entity_logger\EntityLoggerInterface
   */
  protected $entityLogger;

  /**
   * The entity for this entity logger instance.
   *
   * @var \Drupal\Core\Entity\EntityInterface
   */
  protected $entity;

  /**
   * The optional Drupal logging channel.
   *
   * @var string|null
   */
  protected $channel;

  /**
   * Constructs a EntityLoggerInstance object.
   *
   * @param \Drupal\entity_logger\EntityLoggerInterface $entity_logger
   *   The entity logger service.
   */
  public function __construct(EntityLoggerInterface $entity_logger) {
    $this->entityLogger = $entity_logger;
  }

  /**
   * {@inheritdoc}
   */
  public function setEntity(EntityInterface $entity) {
    $this->entity = $entity;
  }

  /**
   * {@inheritdoc}
   */
  public function setLoggerChannel(string $channel) {
    $this->channel = $channel;
  }

  /**
   * {@inheritdoc}
   */
  public function addLogWithSeverity(string $message, array $context = [], int $severity = RfcLogLevel::INFO) {
    if (!$this->entity) {
      return NULL;
    }
    $this->entityLogger->log($this->entity, $message, $context, $severity, $this->channel);
  }

  /**
   * {@inheritdoc}
   */
  public function addLog(string $message, array $context = []) {
    $this->addLogWithSeverity($message, $context);
  }

  /**
   * {@inheritdoc}
   */
  public function addInfoLog(string $message, array $context = []) {
    $this->addLogWithSeverity($message, $context);
  }

  /**
   * {@inheritdoc}
   */
  public function addNoticeLog(string $message, array $context = []) {
    $this->addLogWithSeverity($message, $context, RfcLogLevel::NOTICE);
  }

  /**
   * {@inheritdoc}
   */
  public function addWarningLog(string $message, array $context = []) {
    $this->addLogWithSeverity($message, $context, RfcLogLevel::WARNING);
  }

  /**
   * {@inheritdoc}
   */
  public function addErrorLog(string $message, array $context = []) {
    $this->addLogWithSeverity($message, $context, RfcLogLevel::ERROR);
  }

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

namespace Drupal\entity_logger;

use Drupal\Core\Entity\EntityInterface;

/**
 * Factory class for entity logger instances.
 */
class EntityLoggerInstanceFactory implements EntityLoggerInstanceFactoryInterface {

  /**
   * The entity logger instance service.
   *
   * @var \Drupal\entity_logger\EntityLoggerInstanceInterface
   */
  protected $entityLoggerInstance;

  /**
   * Constructs a new EntityLogger Instance object.
   *
   * @param \Drupal\entity_logger\EntityLoggerInstanceInterface $entity_logger_instance
   *   The entity logger instance service.
   */
  public function __construct(EntityLoggerInstanceInterface $entity_logger_instance) {
    $this->entityLoggerInstance = $entity_logger_instance;
  }

  /**
   * {@inheritdoc}
   */
  public function get(EntityInterface $entity, string $channel = NULL): EntityLoggerInstance {
    $this->entityLoggerInstance->setEntity($entity);
    if ($channel) {
      $this->entityLoggerInstance->setLoggerChannel($channel);
    }
    return $this->entityLoggerInstance;
  }

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

namespace Drupal\entity_logger;

use Drupal\Core\Entity\EntityInterface;

/**
 * Interface for entity_logger.instance.factory service.
 */
interface EntityLoggerInstanceFactoryInterface {

  /**
   * Get an entity logger instance.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity to log to.
   * @param string|null $channel
   *   The optional Drupal log channel to log to.
   *
   * @return \Drupal\entity_logger\EntityLoggerInstance
   *   The entity logger instance.
   */
  public function get(EntityInterface $entity, string $channel = NULL): EntityLoggerInstance;

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

namespace Drupal\entity_logger;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Logger\RfcLogLevel;

/**
 * Interface for entity_logger.instance service.
 */
interface EntityLoggerInstanceInterface {

  /**
   * Set the entity for this instance.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity for this instance.
   */
  public function setEntity(EntityInterface $entity);

  /**
   * Set the optional channel for regular Drupal logging.
   *
   * @param string $channel
   *   The optional channel to use for Drupal logging.
   */
  public function setLoggerChannel(string $channel);

  /**
   * Add log message to entity logger, with given severity.
   *
   * @param string $message
   *   The log message.
   * @param array $context
   *   The log message context.
   * @param int $severity
   *   The severity level.
   */
  public function addLogWithSeverity(string $message, array $context = [], int $severity = RfcLogLevel::INFO);

  /**
   * Add a default log to the entity.
   *
   * @param string $message
   *   The log message.
   * @param array $context
   *   The log message context.
   */
  public function addLog(string $message, array $context = []);

  /**
   * Add an info log to the entity.
   *
   * @param string $message
   *   The log message.
   * @param array $context
   *   The log message context.
   */
  public function addInfoLog(string $message, array $context = []);

  /**
   * Add a notice log to the entity.
   *
   * @param string $message
   *   The log message.
   * @param array $context
   *   The log message context.
   */
  public function addNoticeLog(string $message, array $context = []);

  /**
   * Add a warning log to the entity.
   *
   * @param string $message
   *   The log message.
   * @param array $context
   *   The log message context.
   */
  public function addWarningLog(string $message, array $context = []);

  /**
   * Add an error log to the entity.
   *
   * @param string $message
   *   The log message.
   * @param array $context
   *   The log message context.
   */
  public function addErrorLog(string $message, array $context = []);

}