You are here

class ErrorLog in Error Log 8

Logs events to the PHP error log.

Hierarchy

Expanded class hierarchy of ErrorLog

1 string reference to 'ErrorLog'
error_log.services.yml in ./error_log.services.yml
error_log.services.yml
1 service uses ErrorLog
logger.error_log in ./error_log.services.yml
Drupal\error_log\Logger\ErrorLog

File

src/Logger/ErrorLog.php, line 15

Namespace

Drupal\error_log\Logger
View source
class ErrorLog implements LoggerInterface {
  use DependencySerializationTrait {
    __sleep as protected dependencySleep;
    __wakeup as protected dependencyWakeup;
  }
  use RfcLoggerTrait;

  /**
   * Provides untranslated log levels.
   */
  const LOG_LEVELS = [
    RfcLogLevel::EMERGENCY => 'emergency',
    RfcLogLevel::ALERT => 'alert',
    RfcLogLevel::CRITICAL => 'critical',
    RfcLogLevel::ERROR => 'error',
    RfcLogLevel::WARNING => 'warning',
    RfcLogLevel::NOTICE => 'notice',
    RfcLogLevel::INFO => 'info',
    RfcLogLevel::DEBUG => 'debug',
  ];

  /**
   * A configuration object containing syslog settings.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * Config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The message's placeholders parser.
   *
   * @var \Drupal\Core\Logger\LogMessageParserInterface
   */
  protected $parser;

  /**
   * Constructs an Error Log 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->configFactory = $config_factory;
    $this->config = $this->configFactory
      ->get('error_log.settings');
    $this->parser = $parser;
  }

  /**
   * {@inheritdoc}
   */
  public function log($level, $message, array $context = []) {
    if (empty($this->config
      ->get('log_levels')["level_{$level}"])) {
      return;
    }
    if (in_array($context['channel'], $this->config
      ->get('ignored_channels') ?: [])) {
      return;
    }

    // Drush handles error logging for us, so disable redundant logging.
    if (function_exists('drush_main') && !ini_get('error_log')) {
      return;
    }
    $level = static::LOG_LEVELS[$level];
    $message_placeholders = $this->parser
      ->parseMessagePlaceholders($message, $context);
    $message = empty($message_placeholders) ? $message : strtr($message, $message_placeholders);
    $message = "[{$level}] [{$context['channel']}] [{$context['ip']}] [uid:{$context['uid']}] [{$context['request_uri']}] [{$context['referer']}] {$message}";
    error_log($message);
  }

  /**
   * {@inheritdoc}
   */
  public function __sleep() {
    return array_diff($this
      ->dependencySleep(), [
      'config',
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function __wakeup() {
    $this
      ->dependencyWakeup();
    $this->config = $this->configFactory
      ->get('error_log.settings');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function Aliased as: dependencySleep 1
DependencySerializationTrait::__wakeup public function Aliased as: dependencyWakeup 2
ErrorLog::$config protected property A configuration object containing syslog settings.
ErrorLog::$configFactory protected property Config factory.
ErrorLog::$parser protected property The message's placeholders parser.
ErrorLog::log public function Overrides RfcLoggerTrait::log
ErrorLog::LOG_LEVELS constant Provides untranslated log levels.
ErrorLog::__construct public function Constructs an Error Log object.
ErrorLog::__sleep public function
ErrorLog::__wakeup public function
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