public function JsonLogData::setMessage in JSONlog 8
Same name and namespace in other branches
- 8.2 src/Logger/JsonLogData.php \Drupal\jsonlog\Logger\JsonLogData::setMessage()
- 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
Namespace
Drupal\jsonlog\LoggerCode
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),
];
}
}
}