You are here

class LogMessage in File Log 8

Same name and namespace in other branches
  1. 2.0.x src/LogMessage.php \Drupal\filelog\LogMessage

Represents a single log event.

Hierarchy

Expanded class hierarchy of LogMessage

3 files declare their use of LogMessage
FileLog.php in src/Logger/FileLog.php
FileLogTest.php in tests/src/Unit/FileLogTest.php
FileLogTokenTest.php in tests/src/Unit/FileLogTokenTest.php

File

src/LogMessage.php, line 16

Namespace

Drupal\filelog
View source
class LogMessage {

  /**
   * Untranslated level strings.
   *
   * @var string[]
   */
  protected static $levels;

  /**
   * The log message, with placeholders.
   *
   * @var string
   */
  protected $message;

  /**
   * The processed log message, with placeholders replaced.
   *
   * @var string
   */
  protected $text;

  /**
   * Placeholders of the log message.
   *
   * @var array
   */
  protected $placeholders;

  /**
   * Variables of the log message.
   *
   * Identical to placeholders, but with format prefixes stripped.
   *
   * @var array
   */
  protected $variables;

  /**
   * Context variables of the log message.
   *
   * @var array
   */
  protected $context;

  /**
   * Severity level.
   *
   * @var int
   */
  protected $level;

  /**
   * User who triggered the event.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $user;

  /**
   * LogMessage constructor.
   *
   * @param int $level
   *   Severity level.
   * @param string $message
   *   Message content.
   * @param array $variables
   *   Placeholder variables.
   * @param array $context
   *   Context variables.
   */
  public function __construct($level, $message, array $variables, array $context) {
    $this->level = $level;

    // Store the original placeholders for rendering the message.
    $this->placeholders = $variables;
    $this->message = $message;

    // Strip the variable format prefixes.
    foreach ($variables as $key => $value) {
      if (in_array($key[0], [
        '%',
        '!',
        '@',
        ':',
      ], TRUE)) {
        $variables[substr($key, 1)] = $value;
        unset($variables[$key]);
      }
    }
    $this->variables = $variables;
    $this->context = $context + [
      'uid' => NULL,
      'channel' => NULL,
      'ip' => NULL,
      'request_uri' => NULL,
      'referer' => NULL,
      'timestamp' => NULL,
    ];
  }

  /**
   * Get untranslated level strings.
   *
   * @return string[]
   *   An associative array of RFC levels to labels.
   */
  public static function getLevels() : array {
    if (!static::$levels) {
      static::$levels = RfcLogLevel::getLevels();
      foreach (static::$levels as $id => $label) {

        /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $label */
        static::$levels[$id] = $label
          ->getUntranslatedString();
      }
    }
    return static::$levels;
  }

  /**
   * Get the log channel.
   *
   * @return string
   *   The log channel.
   */
  public function getType() : string {
    return $this->context['channel'];
  }

  /**
   * Get the severity level.
   *
   * @return string
   *   The severity level.
   */
  public function getLevel() : string {
    return static::getLevels()[$this->level];
  }

  /**
   * Get the rendered text of the message.
   *
   * @return string
   *   The rendered text.
   */
  public function getText() : string {
    if (!$this->text) {
      $this->text = $this->message;
      if (!empty($this->placeholders)) {
        $this->text = strtr($this->text, $this->placeholders);
      }
      $this->text = str_replace("\n", '\\n', strip_tags($this->text));
    }
    return $this->text;
  }

  /**
   * Get the request URI of the message.
   *
   * @return string
   *   The request URI.
   */
  public function getLocation() : string {
    return $this->context['request_uri'];
  }

  /**
   * Get the IP that triggered the message.
   *
   * @return string
   *   The IP.
   */
  public function getIp() : string {
    return $this->context['ip'] ?: '0.0.0.0';
  }

  /**
   * Get the referrer of the message.
   *
   * @return string|null
   *   The referrer, or NULL.
   */
  public function getReferrer() : ?string {
    return $this->context['referer'] ?? NULL;
  }

  /**
   * Get an arbitrary variable.
   *
   * @param string $name
   *   The variable name (without format prefix).
   *
   * @return string|null
   *   The value.
   */
  public function getVariable($name) : ?string {
    return $this->variables[$name] ?? NULL;
  }

  /**
   * Get an arbitrary context variable.
   *
   * @param string $name
   *   The variable name.
   *
   * @return string|null
   *   The value.
   */
  public function getContext($name) : ?string {
    return $this->context[$name] ?? NULL;
  }

  /**
   * Get the user who triggered the message.
   *
   * @return \Drupal\user\UserInterface
   *   The user object.
   */
  public function getUser() : UserInterface {
    if (!$this->user) {
      $this->user = User::load($this->context['uid']);
    }
    return $this->user;
  }

  /**
   * Get the timestamp of the message.
   *
   * @return int
   *   The timestamp.
   */
  public function getTimestamp() : int {
    return $this->context['timestamp'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LogMessage::$context protected property Context variables of the log message.
LogMessage::$level protected property Severity level.
LogMessage::$levels protected static property Untranslated level strings.
LogMessage::$message protected property The log message, with placeholders.
LogMessage::$placeholders protected property Placeholders of the log message.
LogMessage::$text protected property The processed log message, with placeholders replaced.
LogMessage::$user protected property User who triggered the event.
LogMessage::$variables protected property Variables of the log message.
LogMessage::getContext public function Get an arbitrary context variable.
LogMessage::getIp public function Get the IP that triggered the message.
LogMessage::getLevel public function Get the severity level.
LogMessage::getLevels public static function Get untranslated level strings.
LogMessage::getLocation public function Get the request URI of the message.
LogMessage::getReferrer public function Get the referrer of the message.
LogMessage::getText public function Get the rendered text of the message.
LogMessage::getTimestamp public function Get the timestamp of the message.
LogMessage::getType public function Get the log channel.
LogMessage::getUser public function Get the user who triggered the message.
LogMessage::getVariable public function Get an arbitrary variable.
LogMessage::__construct public function LogMessage constructor.