-
-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathExceptionToSentryIssueHandler.php
More file actions
129 lines (105 loc) · 3.25 KB
/
ExceptionToSentryIssueHandler.php
File metadata and controls
129 lines (105 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
declare(strict_types=1);
namespace Sentry\Monolog;
use Monolog\Handler\AbstractHandler;
use Monolog\Level;
use Monolog\Logger;
use Monolog\LogRecord;
use Psr\Log\LogLevel;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
/**
* This Monolog handler will collect monolog events and send them to sentry.
*/
class ExceptionToSentryIssueHandler extends AbstractHandler
{
/**
* @var HubInterface
*/
private $hub;
/**
* @phpstan-param value-of<Level::VALUES>|value-of<Level::NAMES>|Level|LogLevel::* $level
*/
public function __construct(HubInterface $hub, $level = Logger::DEBUG, bool $bubble = true)
{
$this->hub = $hub;
parent::__construct($level, $bubble);
}
/**
* @param array<string, mixed>|LogRecord $record
*/
public function handle($record): bool
{
$exception = $this->getExceptionFromRecord($record);
/** @phpstan-ignore-next-line */
if ($exception === null || !$this->isHandling($record)) {
return false;
}
$this->hub->withScope(function (Scope $scope) use ($record, $exception): void {
$scope->setExtra('monolog.channel', $record['channel']);
$scope->setExtra('monolog.level', $record['level_name']);
$scope->setExtra('monolog.message', $record['message']);
$monologContextData = $this->getMonologContextData($this->getContextFromRecord($record));
if ($monologContextData !== []) {
$scope->setExtra('monolog.context', $monologContextData);
}
$monologExtraData = $this->getExtraFromRecord($record);
if ($monologExtraData !== []) {
$scope->setExtra('monolog.extra', $monologExtraData);
}
$this->hub->captureException($exception);
});
return $this->bubble === false;
}
/**
* @param array<string, mixed>|LogRecord $record
*/
private function getExceptionFromRecord($record): ?\Throwable
{
$exception = $this->getContextFromRecord($record)['exception'] ?? null;
if ($exception instanceof \Throwable) {
return $exception;
}
return null;
}
/**
* @param array<string, mixed>|LogRecord $record
*
* @return array<string, mixed>
*/
private function getContextFromRecord($record): array
{
return $this->getArrayFieldFromRecord($record, 'context');
}
/**
* @param array<string, mixed>|LogRecord $record
*
* @return array<string, mixed>
*/
private function getExtraFromRecord($record): array
{
return $this->getArrayFieldFromRecord($record, 'extra');
}
/**
* @param array<string, mixed>|LogRecord $record
*
* @return array<string, mixed>
*/
private function getArrayFieldFromRecord($record, string $field): array
{
if (isset($record[$field]) && \is_array($record[$field])) {
return $record[$field];
}
return [];
}
/**
* @param array<string, mixed> $context
*
* @return array<string, mixed>
*/
private function getMonologContextData(array $context): array
{
unset($context['exception']);
return $context;
}
}