JsonLog.php in JSONlog 3.x
File
src/Logger/JsonLog.php
View source
<?php
namespace Drupal\jsonlog\Logger;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LogMessageParserInterface;
use Drupal\Core\Logger\RfcLoggerTrait;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Psr\Log\LoggerInterface;
class JsonLog implements LoggerInterface {
use RfcLoggerTrait;
const JSONLOG_NEWLINE_PREPEND = FALSE;
private $config;
private $parser;
private $moduleHandler;
private $requestStack;
private $threshold;
private $site_id;
private $canonical;
private $stdout;
private $file;
private $dir;
private $truncate;
private $tags_server;
private $tags_site;
public function __construct(ConfigFactoryInterface $config_factory, LogMessageParserInterface $parser, ModuleHandlerInterface $moduleHandler, RequestStack $requestStack) {
$this->config = $config_factory
->get('jsonlog.settings');
$this->parser = $parser;
$this->moduleHandler = $moduleHandler;
$this->requestStack = $requestStack;
$this
->loadDefaultSettings();
}
public function log($level, $message, array $context = []) {
if (!($log_entry = $this
->prepareLog($level, $message, $context))) {
return;
}
if ($this->stdout) {
if (FALSE === file_put_contents('php://stdout', $log_entry
->getJson())) {
error_log('Drupal jsonlog, site ID[' . $this->site_id . '], failed to write to STDOUT.');
}
unset($log_entry);
return;
}
if ($this->config
->get('jsonlog_newline_prepend') ?? JsonLog::JSONLOG_NEWLINE_PREPEND) {
$prepend = "\n";
$append = '';
}
else {
$prepend = '';
$append = "\n";
}
if (!file_put_contents($this->file, $prepend . $log_entry
->getJson() . $append, FILE_APPEND | LOCK_EX)) {
error_log('Drupal jsonlog, site ID[' . $this->site_id . '], failed to write to file[' . $this->file . '].');
}
unset($log_entry);
}
public function prepareLog($level, $message, array $context = []) {
if ($level > $this->threshold || empty($context)) {
return FALSE;
}
$variables = $this->parser
->parseMessagePlaceholders($message, $context);
$method = empty($context['request_uri']) ? '' : $this->requestStack
->getCurrentRequest()
->getRealMethod();
$entry = new JsonLogData($this->site_id, $this->canonical);
$entry
->setTags($this->tags_server, $this->tags_site);
$entry
->setMessage($message, $this->truncate, $variables);
$entry
->setSeverity($level);
$entry
->setSubType($context['channel']);
$entry
->setMethod($method);
$entry
->setRequest_uri($context['request_uri']);
$entry
->setReferer($context['referer']);
$entry
->setAccount(isset($context['user']) ? $context['user'] : NULL);
$entry
->setUid($context['uid']);
$entry
->setClient_ip($context['ip']);
$entry
->setLink($context['link']);
return $entry;
}
public function getFileName($file_time_format) {
return $this->dir . '/' . $this->site_id . ($file_time_format == 'none' ? '' : '.' . date($file_time_format)) . '.json.log';
}
private function loadDefaultSettings() {
if (!($this->threshold = getenv('drupal_jsonlog_severity_threshold'))) {
$this->threshold = $this->config
->get('jsonlog_severity_threshold');
}
if (!($this->site_id = getenv('drupal_jsonlog_siteid'))) {
if (!($this->site_id = $this->config
->get('jsonlog_siteid'))) {
$this->moduleHandler
->loadInclude('jsonlog', 'inc');
$this->site_id = jsonlog_default_site_id();
}
}
if (!($this->canonical = getenv('drupal_jsonlog_canonical'))) {
$this->canonical = $this->config
->get('jsonlog_canonical');
}
if (!($this->stdout = getenv('drupal_jsonlog_stdout'))) {
$this->stdout = $this->config
->get('jsonlog_stdout') ?? FALSE;
}
if (!$this->stdout) {
if (!($this->dir = getenv('drupal_jsonlog_dir'))) {
if (!($this->dir = $this->config
->get('jsonlog_dir'))) {
$this->moduleHandler
->loadInclude('jsonlog', 'inc');
if (!($this->dir = jsonlog_default_dir())) {
error_log('Drupal jsonlog, site ID[' . $this->dir . '], failed to establish server\'s default log dir.');
}
}
}
if (!($file_time = getenv('drupal_jsonlog_file_time'))) {
$file_time = $this->config
->get('jsonlog_file_time');
}
$this->file = $this
->getFileName($file_time);
}
if (($this->truncate = getenv('drupal_jsonlog_truncate')) === FALSE) {
$this->truncate = $this->config
->get('jsonlog_truncate');
}
$this->tags_server = ($tags = getenv('drupal_jsonlog_tags')) !== FALSE ? $tags : '';
$this->tags_site = $this->config
->get('jsonlog_tags');
}
}
Classes
Name |
Description |
JsonLog |
Redirects logging messages to jsonlog. |