<?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Formatter\LineFormatter; use Monolog\Formatter\FormatterInterface; use Monolog\Logger; use Raven_Client; /** * Handler to send messages to a Sentry (https://github.com/getsentry/sentry) server * using sentry-php (https://github.com/getsentry/sentry-php) * * @author Marc Abramowitz <marc@marc-abramowitz.com> */ class RavenHandler extends AbstractProcessingHandler { /** * Translates Monolog log levels to Raven log levels. */ protected $logLevels = array( Logger::DEBUG => Raven_Client::DEBUG, Logger::INFO => Raven_Client::INFO, Logger::NOTICE => Raven_Client::INFO, Logger::WARNING => Raven_Client::WARNING, Logger::ERROR => Raven_Client::ERROR, Logger::CRITICAL => Raven_Client::FATAL, Logger::ALERT => Raven_Client::FATAL, Logger::EMERGENCY => Raven_Client::FATAL, ); /** * @var string should represent the current version of the calling * software. Can be any string (git commit, version number) */ protected $release; /** * @var Raven_Client the client object that sends the message to the server */ protected $ravenClient; /** * @var LineFormatter The formatter to use for the logs generated via handleBatch() */ protected $batchFormatter; /** * @param Raven_Client $ravenClient * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not */ public function __construct(Raven_Client $ravenClient, $level = Logger::DEBUG, $bubble = true) { parent::__construct($level, $bubble); $this->ravenClient = $ravenClient; } /** * {@inheritdoc} */ public function handleBatch(array $records) { $level = $this->level; // filter records based on their level $records = array_filter($records, function ($record) use ($level) { return $record['level'] >= $level; }); if (!$records) { return; } // the record with the highest severity is the "main" one $record = array_reduce($records, function ($highest, $record) { if ($record['level'] > $highest['level']) { return $record; } return $highest; }); // the other ones are added as a context item $logs = array(); foreach ($records as $r) { $logs[] = $this->processRecord($r); } if ($logs) { $record['context']['logs'] = (string) $this->getBatchFormatter()->formatBatch($logs); } $this->handle($record); } /** * Sets the formatter for the logs generated by handleBatch(). * * @param FormatterInterface $formatter */ public function setBatchFormatter(FormatterInterface $formatter) { $this->batchFormatter = $formatter; } /** * Gets the formatter for the logs generated by handleBatch(). * * @return FormatterInterface */ public function getBatchFormatter() { if (!$this->batchFormatter) { $this->batchFormatter = $this->getDefaultBatchFormatter(); } return $this->batchFormatter; } /** * {@inheritdoc} */ protected function write(array $record) { $previousUserContext = false; $options = array(); $options['level'] = $this->logLevels[$record['level']]; $options['tags'] = array(); if (!empty($record['extra']['tags'])) { $options['tags'] = array_merge($options['tags'], $record['extra']['tags']); unset($record['extra']['tags']); } if (!empty($record['context']['tags'])) { $options['tags'] = array_merge($options['tags'], $record['context']['tags']); unset($record['context']['tags']); } if (!empty($record['context']['fingerprint'])) { $options['fingerprint'] = $record['context']['fingerprint']; unset($record['context']['fingerprint']); } if (!empty($record['context']['logger'])) { $options['logger'] = $record['context']['logger']; unset($record['context']['logger']); } else { $options['logger'] = $record['channel']; } foreach ($this->getExtraParameters() as $key) { foreach (array('extra', 'context') as $source) { if (!empty($record[$source][$key])) { $options[$key] = $record[$source][$key]; unset($record[$source][$key]); } } } if (!empty($record['context'])) { $options['extra']['context'] = $record['context']; if (!empty($record['context']['user'])) { $previousUserContext = $this->ravenClient->context->user; $this->ravenClient->user_context($record['context']['user']); unset($options['extra']['context']['user']); } } if (!empty($record['extra'])) { $options['extra']['extra'] = $record['extra']; } if (!empty($this->release) && !isset($options['release'])) { $options['release'] = $this->release; } if (isset($record['context']['exception']) && ($record['context']['exception'] instanceof \Exception || (PHP_VERSION_ID >= 70000 && $record['context']['exception'] instanceof \Throwable))) { $options['message'] = $record['formatted']; $this->ravenClient->captureException($record['context']['exception'], $options); } else { $this->ravenClient->captureMessage($record['formatted'], array(), $options); } if ($previousUserContext !== false) { $this->ravenClient->user_context($previousUserContext); } } /** * {@inheritDoc} */ protected function getDefaultFormatter() { return new LineFormatter('[%channel%] %message%'); } /** * Gets the default formatter for the logs generated by handleBatch(). * * @return FormatterInterface */ protected function getDefaultBatchFormatter() { return new LineFormatter(); } /** * Gets extra parameters supported by Raven that can be found in "extra" and "context" * * @return array */ protected function getExtraParameters() { return array('contexts', 'checksum', 'release', 'event_id'); } /** * @param string $value * @return self */ public function setRelease($value) { $this->release = $value; return $this; } }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
Curl | Folder | 0755 |
|
|
FingersCrossed | Folder | 0755 |
|
|
Slack | Folder | 0755 |
|
|
SyslogUdp | Folder | 0755 |
|
|
AbstractHandler.php | File | 4.28 KB | 0644 |
|
AbstractProcessingHandler.php | File | 1.5 KB | 0644 |
|
AbstractSyslogHandler.php | File | 3.29 KB | 0644 |
|
AmqpHandler.php | File | 3.78 KB | 0644 |
|
BrowserConsoleHandler.php | File | 7.21 KB | 0644 |
|
BufferHandler.php | File | 3.58 KB | 0644 |
|
ChromePHPHandler.php | File | 5.48 KB | 0644 |
|
CouchDBHandler.php | File | 1.91 KB | 0644 |
|
CubeHandler.php | File | 4.52 KB | 0644 |
|
DeduplicationHandler.php | File | 5.35 KB | 0644 |
|
DoctrineCouchDBHandler.php | File | 1000 B | 0644 |
|
DynamoDbHandler.php | File | 2.38 KB | 0644 |
|
ElasticSearchHandler.php | File | 3.33 KB | 0644 |
|
ErrorLogHandler.php | File | 2.31 KB | 0644 |
|
FilterHandler.php | File | 4.32 KB | 0644 |
|
FingersCrossedHandler.php | File | 5.77 KB | 0644 |
|
FirePHPHandler.php | File | 5.33 KB | 0644 |
|
FleepHookHandler.php | File | 3.28 KB | 0644 |
|
FlowdockHandler.php | File | 3.28 KB | 0644 |
|
GelfHandler.php | File | 1.91 KB | 0644 |
|
GroupHandler.php | File | 2.69 KB | 0644 |
|
HandlerInterface.php | File | 2.53 KB | 0644 |
|
HandlerWrapper.php | File | 2.31 KB | 0644 |
|
HipChatHandler.php | File | 10.51 KB | 0644 |
|
IFTTTHandler.php | File | 2.06 KB | 0644 |
|
InsightOpsHandler.php | File | 1.83 KB | 0644 |
|
LogEntriesHandler.php | File | 1.58 KB | 0644 |
|
LogglyHandler.php | File | 2.56 KB | 0644 |
|
MailHandler.php | File | 1.58 KB | 0644 |
|
MandrillHandler.php | File | 2.11 KB | 0644 |
|
MissingExtensionException.php | File | 450 B | 0644 |
|
MongoDBHandler.php | File | 1.57 KB | 0644 |
|
NativeMailerHandler.php | File | 5.08 KB | 0644 |
|
NewRelicHandler.php | File | 6.06 KB | 0644 |
|
NullHandler.php | File | 953 B | 0644 |
|
PHPConsoleHandler.php | File | 9.76 KB | 0644 |
|
PsrHandler.php | File | 1.4 KB | 0644 |
|
PushoverHandler.php | File | 6.47 KB | 0644 |
|
RavenHandler.php | File | 6.96 KB | 0644 |
|
RedisHandler.php | File | 2.82 KB | 0644 |
|
RollbarHandler.php | File | 3.85 KB | 0644 |
|
RotatingFileHandler.php | File | 5.75 KB | 0644 |
|
SamplingHandler.php | File | 2.61 KB | 0644 |
|
SlackHandler.php | File | 6.3 KB | 0644 |
|
SlackWebhookHandler.php | File | 3.73 KB | 0644 |
|
SlackbotHandler.php | File | 1.98 KB | 0644 |
|
SocketHandler.php | File | 9.58 KB | 0644 |
|
StreamHandler.php | File | 5.1 KB | 0644 |
|
SwiftMailerHandler.php | File | 3.36 KB | 0644 |
|
SyslogHandler.php | File | 1.8 KB | 0644 |
|
SyslogUdpHandler.php | File | 2.51 KB | 0644 |
|
TestHandler.php | File | 5.01 KB | 0644 |
|
WhatFailureGroupHandler.php | File | 1.76 KB | 0644 |
|
ZendMonitorHandler.php | File | 2.19 KB | 0644 |
|