You are here

trait LoggerTrait in Search API 8

Provides helper methods for logging with dependency injection.

Hierarchy

19 files declare their use of LoggerTrait
BackendPluginBase.php in src/Backend/BackendPluginBase.php
Basic.php in src/Plugin/search_api/tracker/Basic.php
ContentAccess.php in src/Plugin/search_api/processor/ContentAccess.php
ContentEntity.php in src/Plugin/search_api/datasource/ContentEntity.php
Field.php in src/Item/Field.php

... See full list

File

src/LoggerTrait.php, line 12

Namespace

Drupal\search_api
View source
trait LoggerTrait {

  /**
   * The logging channel to use.
   *
   * @var \Psr\Log\LoggerInterface|null
   */
  protected $logger;

  /**
   * Retrieves the logger.
   *
   * @return \Psr\Log\LoggerInterface
   *   The logger.
   */
  public function getLogger() {
    return $this->logger ?: \Drupal::service('logger.channel.search_api');
  }

  /**
   * Sets the logger.
   *
   * @param \Psr\Log\LoggerInterface $logger
   *   The new logger.
   *
   * @return $this
   */
  public function setLogger(LoggerInterface $logger) {
    $this->logger = $logger;
    return $this;
  }

  /**
   * Logs an exception.
   *
   * @param \Exception $exception
   *   The exception that is going to be logged.
   * @param string|null $message
   *   (optional) The message to display in the log, which can use variables
   *   retrieved from the exception (like "%type" or "@message"). Or NULL to use
   *   the default message.
   * @param array $variables
   *   (optional) Array of variables to replace in the message when it is
   *   displayed, or NULL if the message should not be translated. The normal
   *   patterns for translation variables can be used.
   * @param int $severity
   *   (optional) The severity of the message, as per RFC 3164.
   * @param string|null $link
   *   (optional) A link to associate with the message, if any.
   *
   * @see watchdog_exception()
   * @see \Drupal\Core\Utility\Error::decodeException()
   */
  protected function logException(\Exception $exception, $message = NULL, array $variables = [], $severity = RfcLogLevel::ERROR, $link = NULL) {

    // Use a default value if $message is not set.
    if (empty($message)) {
      $message = '%type: @message in %function (line %line of %file).';
    }
    if ($link) {
      $variables['link'] = $link;
    }
    $variables += Error::decodeException($exception);
    $this
      ->getLogger()
      ->log($severity, $message, $variables);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LoggerTrait::$logger protected property The logging channel to use.
LoggerTrait::getLogger public function Retrieves the logger.
LoggerTrait::logException protected function Logs an exception.
LoggerTrait::setLogger public function Sets the logger.