Raven.php in Raven: Sentry Integration 8
File
src/Logger/Raven.php
View source
<?php
namespace Drupal\raven\Logger;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LogMessageParserInterface;
use Drupal\Core\Logger\RfcLoggerTrait;
use Psr\Log\LoggerInterface;
use Drupal\Core\Logger\RfcLogLevel;
use Raven_Client;
use Raven_ErrorHandler;
class Raven implements LoggerInterface {
use RfcLoggerTrait;
protected $client;
protected $config;
protected $parser;
public function __construct(ConfigFactoryInterface $config_factory, LogMessageParserInterface $parser) {
if (!class_exists('Raven_Client')) {
return;
}
$this->config = $config_factory
->get('raven.settings');
$this->parser = $parser;
$this->client = new Raven_Client($this->config
->get('client_key'), [
'auto_log_stacks' => $this->config
->get('stack'),
'curl_method' => 'async',
'processorOptions' => [
'Raven_SanitizeDataProcessor' => [
'fields_re' => '/(SESS|pass|authorization|password|passwd|secret|password_confirmation|card_number|auth_pw)/i',
],
],
'timeout' => $this->config
->get('timeout'),
'trace' => $this->config
->get('trace'),
]);
if ($this->config
->get('fatal_error_handler')) {
$error_handler = new Raven_ErrorHandler($this->client);
$error_handler
->registerShutdownFunction($this->config
->get('fatal_error_handler_memory'));
}
}
public function log($level, $message, array $context = []) {
if (!class_exists('Raven_Client')) {
return;
}
if (empty($this->config
->get('log_levels')[$level + 1])) {
return;
}
$levels = [
RfcLogLevel::EMERGENCY => Raven_Client::FATAL,
RfcLogLevel::ALERT => Raven_Client::FATAL,
RfcLogLevel::CRITICAL => Raven_Client::FATAL,
RfcLogLevel::ERROR => Raven_Client::ERROR,
RfcLogLevel::WARNING => Raven_Client::WARNING,
RfcLogLevel::NOTICE => Raven_Client::INFO,
RfcLogLevel::INFO => Raven_Client::INFO,
RfcLogLevel::DEBUG => Raven_Client::DEBUG,
];
$data['level'] = $levels[$level];
$message_placeholders = $this->parser
->parseMessagePlaceholders($message, $context);
$data['message'] = empty($message_placeholders) ? $message : strtr($message, $message_placeholders);
$data['tags']['channel'] = $context['channel'];
$data['extra']['link'] = $context['link'];
$data['extra']['referer'] = $context['referer'];
$data['extra']['request_uri'] = $context['request_uri'];
$data['extra']['timestamp'] = $context['timestamp'];
$data['user']['id'] = $context['uid'];
$data['user']['ip_address'] = $context['ip'];
$this->client
->capture($data, NULL);
}
}