JsonLogData.php in JSONlog 3.x
File
src/Logger/JsonLogData.php
View source
<?php
namespace Drupal\jsonlog\Logger;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\StringTranslation\TranslatableMarkup;
class JsonLogData {
private $message;
private $message_id;
private $site_id;
private $canonical;
private $method;
private $tags;
private $type;
private $subtype;
private $severity;
private $request_uri;
private $referer;
private $uid;
private $username;
private $client_ip;
private $link;
private $code;
private $trunc;
function __construct($site_id, $canonical) {
$this->trunc = '';
$this->message_id = uniqid($site_id, TRUE);
$this->site_id = $site_id;
$this->canonical = $canonical;
$this->type = 'drupal';
$this->{'@version'} = 1;
$this
->setTimestamp();
}
public function getJson() {
return Json::encode(get_object_vars($this));
}
public function getData() {
return get_object_vars($this);
}
public function setMessage($entry, $truncate = FALSE, $variables = []) {
if ($truncate) {
$truncate *= 1024;
$truncate -= 768;
$truncate *= 7 / 8;
}
if ($entry) {
if ($entry instanceof TranslatableMarkup) {
$this->message = $entry
->getUntranslatedString();
}
else {
$this->message = empty($variables) ? $entry : strtr($entry, $variables);
}
if ($this->message[0] === '<') {
$this->message = strip_tags($this->message);
}
$this->message = str_replace("\0", '_NUL_', $this->message);
if ($truncate && ($le = strlen($this->message)) > $truncate) {
$this->message = Unicode::truncateBytes($this->message, (int) $truncate);
$this->trunc = [
$le,
strlen($this->message),
];
}
}
}
public function setSeverity($level) {
$this->severity = RfcLogLevel::getLevels()[$level];
}
public function setSubType($channel) {
$this->subtype = mb_substr($channel, 0, 64);
}
public function setMethod($realMethod) {
$this->method = $realMethod;
}
public function setUid(int $uid) {
$this->uid = $uid;
}
public function setAccount($account) {
if ($account) {
$this->uid = $account
->id();
$this->username = $account
->getAccountName();
}
else {
$this->uid = 0;
$this->username = '';
}
}
public function setRequest_uri($request_uri) {
$this->request_uri = $request_uri;
}
public function setReferer($referer) {
$this->referer = $referer;
}
public function setClient_ip($ip) {
$this->client_ip = mb_substr($ip, 0, 128);
}
public function setLink($link) {
if (!$link) {
$this->link = NULL;
$this->code = 0;
}
elseif (ctype_digit($link)) {
$this->link = NULL;
$this->code = (int) $link;
}
else {
$this->link = strip_tags($link);
$this->code = 0;
}
}
public function setTags($tags_server, $tags_site) {
if ($tags_server) {
$tags = $tags_server;
if ($tags_site) {
$tags .= ',' . $tags_site;
}
}
else {
$tags = $tags_site;
}
if ($tags) {
$this->tags = explode(',', $tags);
}
}
private function setTimestamp() {
$millis = round(microtime(TRUE) * 1000);
$seconds = (int) floor($millis / 1000);
$millis -= $seconds * 1000;
$millis = str_pad($millis, 3, '0', STR_PAD_LEFT);
$this->{'@timestamp'} = substr(gmdate('c', $seconds), 0, 19) . '.' . $millis . 'Z';
}
public function getMessage() {
return $this->message;
}
public function getMessageId() {
return $this->message_id;
}
public function getSiteId() {
return $this->site_id;
}
public function getCanonical() {
return $this->canonical;
}
public function getMethod() {
return $this->method;
}
public function getTags() {
return $this->tags;
}
public function getType() {
return $this->type;
}
public function getSubtype() {
return $this->subtype;
}
public function getSeverity() {
return $this->severity;
}
public function getRequestUri() {
return $this->request_uri;
}
public function getReferer() {
return $this->referer;
}
public function getUid() {
return $this->uid;
}
public function getUsername() {
return $this->username;
}
public function getClientIp() {
return $this->client_ip;
}
public function getLink() {
return $this->link;
}
public function getCode() {
return $this->code;
}
public function getTrunc() {
return $this->trunc;
}
}