You are here

class SysLog in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/syslog/src/Logger/SysLog.php \Drupal\syslog\Logger\SysLog

Redirects logging messages to syslog.

Hierarchy

Expanded class hierarchy of SysLog

1 string reference to 'SysLog'
syslog.services.yml in core/modules/syslog/syslog.services.yml
core/modules/syslog/syslog.services.yml
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 18
Contains \Drupal\syslog\Logger\SysLog.

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');
      if ($facility === '') {
        $facility = defined('LOG_LOCAL0') ? LOG_LOCAL0 : LOG_USER;
      }
      $this->connectionOpened = openlog($this->config
        ->get('identity'), LOG_NDELAY, $facility);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function log($level, $message, array $context = array()) {
    global $base_url;

    // 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($this->config
      ->get('format'), array(
      '!base_url' => $base_url,
      '!timestamp' => $context['timestamp'],
      '!type' => $context['channel'],
      '!ip' => $context['ip'],
      '!request_uri' => $context['request_uri'],
      '!referer' => $context['referer'],
      '!uid' => $context['uid'],
      '!link' => strip_tags($context['link']),
      '!message' => strip_tags($message),
    ));
    syslog($level, $entry);
  }

}

Members

Namesort descending Modifiers Type Description 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 Logs with an arbitrary level. Overrides RfcLoggerTrait::log
SysLog::openConnection protected function Opens a connection to the system logger.
SysLog::__construct public function Constructs a SysLog object.