Logger.php 1.31 KB
Newer Older
Pawel G's avatar
Pawel G committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
<?php

namespace Drupal\simple_sitemap;

use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Class Logger
 * @package Drupal\simple_sitemap
 */
class Logger {

  use StringTranslationTrait;

  protected $logger;
  protected $currentUser;

  /**
   * Logger constructor.
   * @param $logger
   */
  public function __construct($logger, $current_user) {
    $this->logger = $logger;
    $this->currentUser = $current_user;
  }

  /**
   * Logs error and optionally displays it to the privileged user.
   *
   * @param $message
   *  Can be string or an array where the first value is the message string and
   *  the second value an array with arrays containing
   *  placeholder => replacement values for the message.
   * @param string $display
   *  Message type (status/warning/error), if set, message is displayed to
   *  privileged user in addition to being logged.
   */
  public function registerError($message, $display = NULL) {
    $substitutions = isset($message[1]) && is_array($message[1]) ? $message[1] : [];
    $message = is_array($message) ? $message[0] : $message;
    $this->logger->notice(strtr($message, $substitutions));
    if (!empty($display)
      && $this->currentUser->hasPermission('administer sitemap settings')) {
      drupal_set_message($this->t($message, $substitutions), $display);
    }
  }
}