WatchdogLogger.php in Ultimate Cron 8.2
File
src/Logger/WatchdogLogger.php
View source
<?php
namespace Drupal\ultimate_cron\Logger;
use Drupal\Core\Logger\LogMessageParserInterface;
use Drupal\Core\Logger\RfcLoggerTrait;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Psr\Log\LoggerInterface as PsrLoggerInterface;
class WatchdogLogger implements PsrLoggerInterface {
use RfcLoggerTrait;
use StringTranslationTrait;
protected $logEntries = [];
protected $parser;
protected $shutdownRegistered = FALSE;
public function __construct(LogMessageParserInterface $parser) {
$this->parser = $parser;
}
public function log($level, $message, array $context = array()) {
if ($this->logEntries) {
unset($context['backtrace']);
$message_placeholders = $this->parser
->parseMessagePlaceholders($message, $context);
foreach ($this->logEntries as $log_entry) {
$log_entry
->log($message, $message_placeholders, $level);
}
}
}
public function catchMessages(LogEntry $log_entry) {
if (!$this->shutdownRegistered) {
ultimate_cron_register_shutdown_function(array(
$this,
'catchMessagesShutdownWrapper',
), 'catch_messages');
$this->shutdownRegistered = TRUE;
}
$this->logEntries[$log_entry->lid] = $log_entry;
}
public function unCatchMessages(LogEntry $log_entry) {
unset($this->logEntries[$log_entry->lid]);
}
public function catchMessagesShutdownWrapper() {
foreach ($this->logEntries as $log_entry) {
$this
->catchMessagesShutdown($log_entry);
}
}
public function catchMessagesShutdown(LogEntry $log_entry) {
$this
->unCatchMessages($log_entry);
if ($log_entry->finished) {
return;
}
$error = error_get_last();
if ($error) {
$message = $error['message'] . ' (line ' . $error['line'] . ' of ' . $error['file'] . ').' . "\n";
$severity = RfcLogLevel::INFO;
if ($error['type'] && (E_NOTICE || E_USER_NOTICE || E_USER_WARNING)) {
$severity = RfcLogLevel::NOTICE;
}
if ($error['type'] && (E_WARNING || E_CORE_WARNING || E_USER_WARNING)) {
$severity = RfcLogLevel::WARNING;
}
if ($error['type'] && (E_ERROR || E_CORE_ERROR || E_USER_ERROR || E_RECOVERABLE_ERROR)) {
$severity = RfcLogLevel::ERROR;
}
$log_entry
->log($message, NULL, $severity);
}
$log_entry
->finish();
}
}