Logger.php 1.33 KB
Newer Older
Pawel G's avatar
Pawel G committed
1 2 3 4 5 6 7
<?php

namespace Drupal\simple_sitemap;

use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
Pawel G's avatar
Pawel G committed
8 9
 * Class Logger.
 *
Pawel G's avatar
Pawel G committed
10 11 12 13 14 15 16 17 18 19 20
 * @package Drupal\simple_sitemap
 */
class Logger {

  use StringTranslationTrait;

  protected $logger;
  protected $currentUser;

  /**
   * Logger constructor.
Pawel G's avatar
Pawel G committed
21
   *
Pawel G's avatar
Pawel G committed
22 23 24 25 26 27 28 29 30 31 32
   * @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
Pawel G's avatar
Pawel G committed
33 34 35
   *   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.
Pawel G's avatar
Pawel G committed
36
   * @param string $display
Pawel G's avatar
Pawel G committed
37 38
   *   Message type (status/warning/error), if set, message is displayed to
   *   privileged user in addition to being logged.
Pawel G's avatar
Pawel G committed
39 40 41 42 43 44 45 46 47 48
   */
  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);
    }
  }
Pawel G's avatar
Pawel G committed
49

Pawel G's avatar
Pawel G committed
50
}