Skip to content

Commit

Permalink
Merge 25de458 into 182f725
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuchar committed Oct 27, 2018
2 parents 182f725 + 25de458 commit 9e43a45
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/Sentry/SentryLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ final class SentryLogger implements ILogger
/** @var array */
private $config;

/** @var Raven_Client|null */
private $client;

private $severityMapping = [
ILogger::DEBUG => Raven_Client::DEBUG,
ILogger::INFO => Raven_Client::INFO,
ILogger::WARNING => Raven_Client::WARNING,
ILogger::ERROR => Raven_Client::ERROR,
ILogger::EXCEPTION => Raven_Client::ERROR,
ILogger::CRITICAL => Raven_Client::FATAL
];

/**
* @param array $config
*/
Expand All @@ -27,21 +39,31 @@ public function __construct(array $config)
*/
public function log($message, $priority)
{
if (!in_array($priority, [ILogger::ERROR, ILogger::EXCEPTION, ILogger::CRITICAL], TRUE)) return;
if (!($message instanceof \Throwable)) return;
if (
in_array($priority, [ILogger::ERROR, ILogger::EXCEPTION, ILogger::CRITICAL], TRUE)
&& ($message instanceof \Throwable)
) {
$this->getClient()->captureException($message);
return;
}

// Send to Sentry
$this->makeRequest($message);
$this->getClient()->breadcrumbs->record([
'message' => (string) $message,
'category' => 'logged-message',
'level' => $this->mapPriority($priority),
]);
}

/**
* @param \Throwable $message
* @return void
*/
protected function makeRequest(\Throwable $message)
private function getClient(): Raven_Client
{
$client = new Raven_Client($this->config['url']);
$client->captureException($message);
if (!$this->client) {
$this->client = new Raven_Client($this->config['url']);
}
return $this->client;
}

private function mapPriority(string $contributtePriority): string
{
return $this->severityMapping[$contributtePriority] ?? Raven_Client::INFO;
}
}

0 comments on commit 9e43a45

Please sign in to comment.