You are here

ErrorLogMessageFormatter.php in Logging and alerts 2.0.x

Same filename and directory in other branches
  1. 8 errorlog/src/Logger/ErrorLogMessageFormatter.php

File

errorlog/src/Logger/ErrorLogMessageFormatter.php
View source
<?php

namespace Drupal\errorlog\Logger;

use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Logger\RfcLoggerTrait;
use Drupal\Core\Render\Renderer;
use Psr\Log\LoggerInterface;

/**
 * Logger class for errorlog module.
 */
class ErrorLogMessageFormatter implements LoggerInterface {
  use RfcLoggerTrait;

  /**
   * Configuration object.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * Renderer object.
   *
   * @var \Drupal\Core\Render\Renderer
   */
  protected $renderer;

  /**
   * Constructs a SysLog object.
   *
   * @param \Drupal\Core\Config\ConfigFactory $config_factory
   *   The configuration factory object.
   * @param \Drupal\Core\Render\Renderer $renderer
   *   The Renderer object.
   */
  public function __construct(ConfigFactory $config_factory, Renderer $renderer) {
    $this->config = $config_factory
      ->get('errorlog.settings');
    $this->renderer = $renderer;
  }

  /**
   * {@inheritdoc}
   */
  public function log($level, $message, array $context = []) {
    if ($this->config
      ->get('errorlog_' . $level)) {
      $log = [
        'level' => $level,
        'context' => $context,
        'message' => $message,
      ];

      // Send themed alert to the web server's log.
      if (\Drupal::hasService('theme.manager')) {
        $errorlog_theme_element = [
          '#theme' => 'errorlog_format',
          '#log' => $log,
        ];
        $message = $this->renderer
          ->renderPlain($errorlog_theme_element);
      }
      error_log($message, 0);
    }
  }

}

Classes

Namesort descending Description
ErrorLogMessageFormatter Logger class for errorlog module.