-
Notifications
You must be signed in to change notification settings - Fork 7
/
SentryHandler.php
199 lines (171 loc) · 5.86 KB
/
SentryHandler.php
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
declare(strict_types=1);
namespace BGalati\MonologSentryHandler;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
use Sentry\Breadcrumb;
use Sentry\FlushableClientInterface;
use Sentry\Severity;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
class SentryHandler extends AbstractProcessingHandler
{
protected $hub;
private $breadcrumbsBuffer = [];
/**
* @param HubInterface $hub The sentry hub used to send event to Sentry
* @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(HubInterface $hub, int $level = Logger::DEBUG, bool $bubble = true)
{
parent::__construct($level, $bubble);
$this->hub = $hub;
}
/**
* {@inheritdoc}
*/
public function handleBatch(array $records): void
{
if (!$records) {
return;
}
// filter records
$records = array_filter(
$records,
function ($record) {
// Keep record that matches the minimum level
return $record['level'] >= $this->level;
}
);
// the record with the highest severity is the "main" one
$main = array_reduce(
$records,
static function ($highest, $record) {
if ($record['level'] > $highest['level']) {
return $record;
}
return $highest;
}
);
// the other ones are added as a context items
foreach ($records as $record) {
$record = $this->processRecord($record);
$record['formatted'] = $this->getFormatter()->format($record);
$this->breadcrumbsBuffer[] = $record;
}
$this->handle($main);
$this->breadcrumbsBuffer = [];
}
/**
* {@inheritdoc}
*/
protected function write(array $record): void
{
$sentryEvent = [
'level' => $sentryLevel = $this->getSeverityFromLevel($record['level']),
'message' => (new LineFormatter('%channel%.%level_name%: %message%'))->format($record),
];
if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Throwable) {
$sentryEvent['exception'] = $record['context']['exception'];
}
$this->hub->withScope(function (Scope $scope) use ($record, $sentryEvent, $sentryLevel): void {
$scope->setLevel($sentryLevel);
$scope->setExtra('monolog.formatted', $record['formatted'] ?? '');
foreach ($this->breadcrumbsBuffer as $breadcrumbRecord) {
$scope->addBreadcrumb(new Breadcrumb(
$this->getBreadcrumbLevelFromLevel($breadcrumbRecord['level']),
$this->getBreadcrumbTypeFromLevel($breadcrumbRecord['level']),
$breadcrumbRecord['channel'] ?? 'N/A',
$breadcrumbRecord['formatted'] ?? 'N/A'
));
}
$this->processScope($scope, $record, $sentryEvent);
$this->hub->captureEvent($sentryEvent);
});
$this->afterWrite();
}
/**
* Extension point.
*
* This method is called when Sentry event is captured by the handler.
* Override it if you want to add custom data to Sentry $scope.
*
* @param Scope $scope Sentry scope where you can add custom data
* @param array $record Current monolog record
* @param array $sentryEvent Current sentry event that will be captured
*/
protected function processScope(Scope $scope, array $record, array $sentryEvent): void
{
}
/**
* Extension point.
*
* Overridable method that for example can be used to:
* - disable Sentry event flush
* - add some custom logic after monolog write process
* - ...
*/
protected function afterWrite(): void
{
$client = $this->hub->getClient();
if ($client instanceof FlushableClientInterface) {
$client->flush();
}
}
/**
* Translates the Monolog level into the Sentry severity.
*
* @param int $level The Monolog log level
*/
private function getSeverityFromLevel(int $level): Severity
{
switch ($level) {
case Logger::DEBUG:
return Severity::debug();
case Logger::INFO:
case Logger::NOTICE:
return Severity::info();
case Logger::WARNING:
return Severity::warning();
case Logger::ERROR:
return Severity::error();
default:
return Severity::fatal();
}
}
/**
* Translates the Monolog level into the Sentry breadcrumb level.
*
* @param int $level The Monolog log level
*/
private function getBreadcrumbLevelFromLevel(int $level): string
{
switch ($level) {
case Logger::DEBUG:
return Breadcrumb::LEVEL_DEBUG;
case Logger::INFO:
case Logger::NOTICE:
return Breadcrumb::LEVEL_INFO;
case Logger::WARNING:
return Breadcrumb::LEVEL_WARNING;
case Logger::ERROR:
return Breadcrumb::LEVEL_ERROR;
default:
return Breadcrumb::LEVEL_FATAL;
}
}
/**
* Translates the Monolog level into the Sentry breadcrumb type.
*
* @param int $level The Monolog log level
*/
private function getBreadcrumbTypeFromLevel(int $level): string
{
if ($level >= Logger::ERROR) {
return Breadcrumb::TYPE_ERROR;
}
return Breadcrumb::TYPE_DEFAULT;
}
}