View source
<?php
namespace Drupal\simple_sitemap;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Session\AccountProxyInterface;
use Psr\Log\LoggerInterface;
class Logger {
use StringTranslationTrait;
protected const LOG_SEVERITY_LEVEL_DEFAULT = 'notice';
protected const DISPLAY_MESSAGE_TYPE_DEFAULT = 'status';
protected $logger;
protected $messenger;
protected $currentUser;
protected $message = '';
protected $substitutions = [];
public function __construct(LoggerInterface $logger, MessengerInterface $messenger, AccountProxyInterface $current_user) {
$this->logger = $logger;
$this->messenger = $messenger;
$this->currentUser = $current_user;
}
public function m($message, array $substitutions = []) : Logger {
$this->message = $message;
$this->substitutions = $substitutions;
return $this;
}
public function log(string $logSeverityLevel = self::LOG_SEVERITY_LEVEL_DEFAULT) : Logger {
$this->logger
->{$logSeverityLevel}(strtr($this->message, $this->substitutions));
return $this;
}
public function display(string $displayMessageType = self::DISPLAY_MESSAGE_TYPE_DEFAULT, string $permission = '') : Logger {
if (empty($permission) || $this->currentUser
->hasPermission($permission)) {
$this->messenger
->addMessage($this
->t($this->message, $this->substitutions), $displayMessageType);
}
return $this;
}
}