ErrorLog.php in Error Log 8
File
src/Logger/ErrorLog.php
View source
<?php
namespace Drupal\error_log\Logger;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Logger\LogMessageParserInterface;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Logger\RfcLoggerTrait;
use Psr\Log\LoggerInterface;
class ErrorLog implements LoggerInterface {
use DependencySerializationTrait {
__sleep as protected dependencySleep;
__wakeup as protected dependencyWakeup;
}
use RfcLoggerTrait;
const LOG_LEVELS = [
RfcLogLevel::EMERGENCY => 'emergency',
RfcLogLevel::ALERT => 'alert',
RfcLogLevel::CRITICAL => 'critical',
RfcLogLevel::ERROR => 'error',
RfcLogLevel::WARNING => 'warning',
RfcLogLevel::NOTICE => 'notice',
RfcLogLevel::INFO => 'info',
RfcLogLevel::DEBUG => 'debug',
];
protected $config;
protected $configFactory;
protected $parser;
public function __construct(ConfigFactoryInterface $config_factory, LogMessageParserInterface $parser) {
$this->configFactory = $config_factory;
$this->config = $this->configFactory
->get('error_log.settings');
$this->parser = $parser;
}
public function log($level, $message, array $context = []) {
if (empty($this->config
->get('log_levels')["level_{$level}"])) {
return;
}
if (in_array($context['channel'], $this->config
->get('ignored_channels') ?: [])) {
return;
}
if (function_exists('drush_main') && !ini_get('error_log')) {
return;
}
$level = static::LOG_LEVELS[$level];
$message_placeholders = $this->parser
->parseMessagePlaceholders($message, $context);
$message = empty($message_placeholders) ? $message : strtr($message, $message_placeholders);
$message = "[{$level}] [{$context['channel']}] [{$context['ip']}] [uid:{$context['uid']}] [{$context['request_uri']}] [{$context['referer']}] {$message}";
error_log($message);
}
public function __sleep() {
return array_diff($this
->dependencySleep(), [
'config',
]);
}
public function __wakeup() {
$this
->dependencyWakeup();
$this->config = $this->configFactory
->get('error_log.settings');
}
}