SysLog.php in Drupal 8
File
core/modules/syslog/src/Logger/SysLog.php
View source
<?php
namespace Drupal\syslog\Logger;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LogMessageParserInterface;
use Drupal\Core\Logger\RfcLoggerTrait;
use Psr\Log\LoggerInterface;
class SysLog implements LoggerInterface {
use RfcLoggerTrait;
protected $config;
protected $parser;
protected $connectionOpened = FALSE;
public function __construct(ConfigFactoryInterface $config_factory, LogMessageParserInterface $parser) {
$this->config = $config_factory
->get('syslog.settings');
$this->parser = $parser;
}
protected function openConnection() {
if (!$this->connectionOpened) {
$facility = $this->config
->get('facility');
$this->connectionOpened = openlog($this->config
->get('identity'), LOG_NDELAY, $facility);
}
}
public function log($level, $message, array $context = []) {
global $base_url;
$this
->openConnection();
$message_placeholders = $this->parser
->parseMessagePlaceholders($message, $context);
$message = empty($message_placeholders) ? $message : strtr($message, $message_placeholders);
$entry = strtr($this->config
->get('format'), [
'!base_url' => $base_url,
'!timestamp' => $context['timestamp'],
'!type' => $context['channel'],
'!ip' => $context['ip'],
'!request_uri' => $context['request_uri'],
'!referer' => $context['referer'],
'!severity' => $level,
'!uid' => $context['uid'],
'!link' => strip_tags($context['link']),
'!message' => strip_tags($message),
]);
$this
->syslogWrapper($level, $entry);
}
protected function syslogWrapper($level, $entry) {
syslog($level, $entry);
}
}
Classes
Name |
Description |
SysLog |
Redirects logging messages to syslog. |