1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Facile\SentryModule\Log\Writer; |
6
|
|
|
|
7
|
|
|
use Facile\SentryModule\Exception; |
8
|
|
|
use Sentry\Severity; |
9
|
|
|
use Sentry\State\Hub; |
10
|
|
|
use Sentry\State\HubInterface; |
11
|
|
|
use Sentry\State\Scope; |
12
|
|
|
use Traversable; |
13
|
|
|
use Zend\Log\Logger; |
14
|
|
|
use Zend\Log\Writer\AbstractWriter; |
15
|
|
|
|
16
|
|
|
final class Sentry extends AbstractWriter |
17
|
|
|
{ |
18
|
|
|
/** @var HubInterface|null */ |
19
|
|
|
private $hub; |
20
|
|
|
|
21
|
11 |
|
private function getSeverityFromLevel(int $level): Severity |
22
|
|
|
{ |
23
|
11 |
|
switch ($level) { |
24
|
|
|
case Logger::DEBUG: |
25
|
1 |
|
return Severity::debug(); |
26
|
|
|
case Logger::WARN: |
27
|
1 |
|
return Severity::warning(); |
28
|
|
|
case Logger::ERR: |
29
|
4 |
|
return Severity::error(); |
30
|
|
|
case Logger::CRIT: |
31
|
|
|
case Logger::ALERT: |
32
|
|
|
case Logger::EMERG: |
33
|
3 |
|
return Severity::fatal(); |
34
|
|
|
default: |
35
|
2 |
|
return Severity::info(); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Sentry constructor. |
41
|
|
|
* |
42
|
|
|
* @param array|Traversable $options |
43
|
|
|
* |
44
|
|
|
* @throws \Zend\Log\Exception\InvalidArgumentException |
45
|
|
|
* @throws Exception\InvalidArgumentException |
46
|
|
|
*/ |
47
|
12 |
|
public function __construct($options = null) |
48
|
|
|
{ |
49
|
12 |
|
parent::__construct($options); |
50
|
|
|
|
51
|
12 |
|
if ($options instanceof Traversable) { |
52
|
11 |
|
$options = \iterator_to_array($options); |
53
|
|
|
} |
54
|
|
|
|
55
|
12 |
|
$hub = $options['hub'] ?? null; |
56
|
|
|
|
57
|
12 |
|
if (null !== $hub && ! $hub instanceof HubInterface) { |
58
|
1 |
|
throw new Exception\InvalidArgumentException('Invalid Sentry Hub'); |
59
|
|
|
} |
60
|
|
|
|
61
|
11 |
|
$this->hub = $hub; |
62
|
11 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Write a message to the log. |
66
|
|
|
* |
67
|
|
|
* @param array $event log data event |
68
|
|
|
*/ |
69
|
11 |
|
protected function doWrite(array $event): void |
70
|
|
|
{ |
71
|
11 |
|
$hub = $this->hub ?: Hub::getCurrent(); |
72
|
|
|
|
73
|
11 |
|
$context = $event['extra'] ?? []; |
74
|
|
|
|
75
|
11 |
|
if ($context instanceof Traversable) { |
76
|
1 |
|
$context = \iterator_to_array($context); |
77
|
10 |
|
} elseif (! \is_array($context)) { |
78
|
1 |
|
$context = []; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$payload = [ |
82
|
11 |
|
'level' => $this->getSeverityFromLevel($event['priority']), |
83
|
11 |
|
'message' => $event['message'], |
84
|
|
|
]; |
85
|
|
|
|
86
|
11 |
|
$exception = $context['exception'] ?? null; |
87
|
|
|
|
88
|
11 |
|
if ($exception instanceof \Throwable) { |
89
|
1 |
|
$payload['exception'] = $exception; |
90
|
1 |
|
unset($context['exception']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$hub->withScope(static function (Scope $scope) use ($hub, $event, $context, $payload): void { |
94
|
11 |
|
$scope->setExtra('zend.priority', $event['priority']); |
95
|
|
|
|
96
|
11 |
|
foreach ($context as $key => $value) { |
97
|
10 |
|
$scope->setExtra((string) $key, $value); |
98
|
|
|
} |
99
|
|
|
|
100
|
11 |
|
$hub->captureEvent($payload); |
101
|
11 |
|
}); |
102
|
11 |
|
} |
103
|
|
|
} |
104
|
|
|
|