class SysLog

Same name in other branches
  1. 8.9.x core/modules/syslog/src/Logger/SysLog.php \Drupal\syslog\Logger\SysLog
  2. 10 core/modules/syslog/src/Logger/SysLog.php \Drupal\syslog\Logger\SysLog
  3. 11.x core/modules/syslog/src/Logger/SysLog.php \Drupal\syslog\Logger\SysLog

Redirects logging messages to syslog.

Hierarchy

Expanded class hierarchy of SysLog

1 file declares its use of SysLog
SysLogTest.php in core/modules/syslog/tests/modules/syslog_test/src/Logger/SysLogTest.php
19 string references to 'SysLog'
ConfigImportInstallProfileTest::testInstallProfileValidation in core/modules/config/tests/src/Functional/ConfigImportInstallProfileTest.php
Tests config importer cannot uninstall install profiles.
d6_syslog_settings.yml in core/modules/syslog/migrations/d6_syslog_settings.yml
core/modules/syslog/migrations/d6_syslog_settings.yml
d7_syslog_settings.yml in core/modules/syslog/migrations/d7_syslog_settings.yml
core/modules/syslog/migrations/d7_syslog_settings.yml
MultilingualReviewPageTest::getAvailablePaths in core/modules/migrate_drupal_ui/tests/src/Functional/d6/MultilingualReviewPageTest.php
Gets the available upgrade paths.
MultilingualReviewPageTest::getAvailablePaths in core/modules/migrate_drupal_ui/tests/src/Functional/d7/MultilingualReviewPageTest.php
Gets the available upgrade paths.

... See full list

1 service uses SysLog
logger.syslog in core/modules/syslog/syslog.services.yml
Drupal\syslog\Logger\SysLog

File

core/modules/syslog/src/Logger/SysLog.php, line 13

Namespace

Drupal\syslog\Logger
View source
class SysLog implements LoggerInterface {
    use RfcLoggerTrait;
    
    /**
     * A configuration object containing syslog settings.
     *
     * @var \Drupal\Core\Config\Config
     */
    protected $config;
    
    /**
     * The message's placeholders parser.
     *
     * @var \Drupal\Core\Logger\LogMessageParserInterface
     */
    protected $parser;
    
    /**
     * Stores whether there is a system logger connection opened or not.
     *
     * @var bool
     */
    protected $connectionOpened = FALSE;
    
    /**
     * Constructs a SysLog object.
     *
     * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
     *   The configuration factory object.
     * @param \Drupal\Core\Logger\LogMessageParserInterface $parser
     *   The parser to use when extracting message variables.
     */
    public function __construct(ConfigFactoryInterface $config_factory, LogMessageParserInterface $parser) {
        $this->config = $config_factory->get('syslog.settings');
        $this->parser = $parser;
    }
    
    /**
     * Opens a connection to the system logger.
     */
    protected function openConnection() {
        if (!$this->connectionOpened) {
            $facility = $this->config
                ->get('facility');
            $this->connectionOpened = openlog($this->config
                ->get('identity'), LOG_NDELAY, $facility);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function log($level, $message, array $context = []) {
        global $base_url;
        $format = $this->config
            ->get('format');
        // If no format is configured then a message will not be written to syslog
        // so return early. This occurs during installation of the syslog module
        // before configuration has been written.
        if (empty($format)) {
            return;
        }
        // Ensure we have a connection available.
        $this->openConnection();
        // Populate the message placeholders and then replace them in the message.
        $message_placeholders = $this->parser
            ->parseMessagePlaceholders($message, $context);
        $message = empty($message_placeholders) ? $message : strtr($message, $message_placeholders);
        $entry = strtr($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);
    }
    
    /**
     * A syslog wrapper to make syslog functionality testable.
     *
     * @param int $level
     *   The syslog priority.
     * @param string $entry
     *   The message to send to syslog function.
     */
    protected function syslogWrapper($level, $entry) {
        syslog($level, $entry);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
RfcLoggerTrait::alert public function
RfcLoggerTrait::critical public function
RfcLoggerTrait::debug public function
RfcLoggerTrait::emergency public function
RfcLoggerTrait::error public function
RfcLoggerTrait::info public function
RfcLoggerTrait::notice public function
RfcLoggerTrait::warning public function
SysLog::$config protected property A configuration object containing syslog settings.
SysLog::$connectionOpened protected property Stores whether there is a system logger connection opened or not.
SysLog::$parser protected property The message's placeholders parser.
SysLog::log public function Overrides RfcLoggerTrait::log
SysLog::openConnection protected function Opens a connection to the system logger.
SysLog::syslogWrapper protected function A syslog wrapper to make syslog functionality testable. 1
SysLog::__construct public function Constructs a SysLog object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.