-
-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathHandler.php
More file actions
135 lines (108 loc) · 3.58 KB
/
Handler.php
File metadata and controls
135 lines (108 loc) · 3.58 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
130
131
132
133
134
135
<?php
declare(strict_types=1);
namespace Sentry\Monolog;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
use Monolog\LogRecord;
use Sentry\Event;
use Sentry\EventHint;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
/**
* This Monolog handler logs every message to a Sentry's server using the given
* hub instance.
*
* @deprecated since version 4.24. To be removed in version 5.0. Use {@see LogsHandler}
* with the `enable_logs` SDK option for Sentry logs, {@see ExceptionToSentryIssueHandler}
* to send Monolog exceptions to Sentry issues, and {@see LogToSentryIssueHandler}
* to send Monolog log messages to Sentry issues.
*
* @author Stefano Arlandini <sarlandini@alice.it>
*/
final class Handler extends AbstractProcessingHandler
{
use CompatibilityProcessingHandlerTrait;
private const CONTEXT_EXCEPTION_KEY = 'exception';
/**
* @var HubInterface
*/
private $hub;
/**
* @var bool
*/
private $fillExtraContext;
/**
* {@inheritdoc}
*
* @param HubInterface $hub The hub to which errors are reported
*/
public function __construct(HubInterface $hub, $level = Logger::DEBUG, bool $bubble = true, bool $fillExtraContext = false)
{
parent::__construct($level, $bubble);
$this->hub = $hub;
$this->fillExtraContext = $fillExtraContext;
}
/**
* @param array<string, mixed>|LogRecord $record
*/
protected function doWrite($record): void
{
$event = Event::createEvent();
$event->setLevel(self::getSeverityFromLevel($record['level']));
$event->setMessage($record['message']);
$event->setLogger(\sprintf('monolog.%s', $record['channel']));
$hint = new EventHint();
if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Throwable) {
$hint->exception = $record['context']['exception'];
}
$this->hub->withScope(function (Scope $scope) use ($record, $event, $hint): void {
$scope->setExtra('monolog.channel', $record['channel']);
$scope->setExtra('monolog.level', $record['level_name']);
$monologContextData = $this->getMonologContextData($record['context']);
if ($monologContextData !== []) {
$scope->setExtra('monolog.context', $monologContextData);
}
$monologExtraData = $this->getMonologExtraData($record['extra']);
if ($monologExtraData !== []) {
$scope->setExtra('monolog.extra', $monologExtraData);
}
$this->hub->captureEvent($event, $hint);
});
}
/**
* @param mixed[] $context
*
* @return mixed[]
*/
private function getMonologContextData(array $context): array
{
if (!$this->fillExtraContext) {
return [];
}
$contextData = [];
foreach ($context as $key => $value) {
// We skip the `exception` field because it goes in its own context
if ($key === self::CONTEXT_EXCEPTION_KEY) {
continue;
}
$contextData[$key] = $value;
}
return $contextData;
}
/**
* @param mixed[] $context
*
* @return mixed[]
*/
private function getMonologExtraData(array $context): array
{
if (!$this->fillExtraContext) {
return [];
}
$extraData = [];
foreach ($context as $key => $value) {
$extraData[$key] = $value;
}
return $extraData;
}
}