You are here

public function JsonLogData::setMessage in JSONlog 8

Same name and namespace in other branches
  1. 8.2 src/Logger/JsonLogData.php \Drupal\jsonlog\Logger\JsonLogData::setMessage()
  2. 3.x src/Logger/JsonLogData.php \Drupal\jsonlog\Logger\JsonLogData::setMessage()

Parameters

string|TranslatableMarkup $entry:

int|false $truncate:

array $variables:

File

src/Logger/JsonLogData.php, line 82

Class

JsonLogData

Namespace

Drupal\jsonlog\Logger

Code

public function setMessage($entry, $truncate = FALSE, $variables = []) {
  if ($truncate) {

    // Kb to bytes.
    $truncate *= 1024;

    // Substract estimated max length of everything but message content.
    $truncate -= 768;

    // Message will get longer when JSON encoded, because of hex encoding of
    // <>&" chars.
    $truncate *= 7 / 8;
  }
  if ($entry) {
    if ($entry instanceof TranslatableMarkup) {

      /** @var TranslatableMarkup $entry */
      $this->message = $entry
        ->getUntranslatedString();
    }
    else {

      /** @var string message */
      $this->message = empty($variables) ? $entry : strtr($entry, $variables);
    }

    // Strip tags if message starts with < (Inspect logs in tag).
    if ($this->message[0] === '<') {
      $this->message = strip_tags($this->message);
    }

    // Escape null byte.
    $this->message = str_replace("\0", '_NUL_', $this->message);

    // Truncate message.
    // Deliberately multi-byte length.
    if ($truncate && ($le = strlen($this->message)) > $truncate) {

      // Truncate multi-byte safe until ASCII length is
      // equal to/less than max byte length.
      $this->message = Unicode::truncateBytes($this->message, (int) $truncate);
      $this->trunc = [
        $le,
        strlen($this->message),
      ];
    }
  }
}