|
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
|
3 |
|
private function getSeverityFromLevel(int $level): Severity |
|
22
|
|
|
{ |
|
23
|
|
|
switch ($level) { |
|
24
|
3 |
|
case Logger::DEBUG: |
|
25
|
|
|
return Severity::debug(); |
|
26
|
3 |
|
case Logger::WARN: |
|
27
|
|
|
return Severity::warning(); |
|
28
|
3 |
|
case Logger::ERR: |
|
29
|
3 |
|
return Severity::error(); |
|
30
|
|
|
case Logger::CRIT: |
|
31
|
|
|
case Logger::ALERT: |
|
32
|
|
|
case Logger::EMERG: |
|
33
|
|
|
return Severity::fatal(); |
|
34
|
|
|
default: |
|
35
|
|
|
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
|
3 |
|
public function __construct($options = null) |
|
48
|
|
|
{ |
|
49
|
3 |
|
parent::__construct($options); |
|
50
|
|
|
|
|
51
|
3 |
|
if ($options instanceof Traversable) { |
|
52
|
3 |
|
$options = \iterator_to_array($options); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
3 |
|
$hub = $options['hub'] ?? null; |
|
56
|
|
|
|
|
57
|
3 |
|
if (null !== $hub && ! $hub instanceof HubInterface) { |
|
58
|
|
|
throw new Exception\InvalidArgumentException('Invalid Sentry Hub'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
3 |
|
$this->hub = $hub; |
|
62
|
3 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Write a message to the log. |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $event log data event |
|
68
|
|
|
*/ |
|
69
|
3 |
|
protected function doWrite(array $event): void |
|
70
|
|
|
{ |
|
71
|
3 |
|
$hub = $this->hub ?: Hub::getCurrent(); |
|
72
|
|
|
|
|
73
|
3 |
|
$context = $event['extra'] ?? []; |
|
74
|
|
|
|
|
75
|
3 |
|
if ($context instanceof Traversable) { |
|
76
|
1 |
|
$context = \iterator_to_array($context); |
|
77
|
2 |
|
} elseif (! \is_array($context)) { |
|
78
|
1 |
|
$context = []; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$payload = [ |
|
82
|
3 |
|
'level' => $this->getSeverityFromLevel($event['priority']), |
|
83
|
3 |
|
'message' => $event['message'], |
|
84
|
|
|
]; |
|
85
|
|
|
|
|
86
|
3 |
|
$exception = $context['exception'] ?? null; |
|
87
|
|
|
|
|
88
|
3 |
|
if ($exception instanceof \Throwable) { |
|
89
|
|
|
$payload['exception'] = $exception; |
|
90
|
|
|
unset($context['exception']); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$hub->withScope(static function (Scope $scope) use ($hub, $event, $context, $payload): void { |
|
94
|
3 |
|
$scope->setExtra('zend.priority', $event['priority']); |
|
95
|
|
|
|
|
96
|
3 |
|
foreach ($context as $key => $value) { |
|
97
|
2 |
|
$scope->setExtra((string) $key, $value); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
3 |
|
$hub->captureEvent($payload); |
|
101
|
3 |
|
}); |
|
102
|
3 |
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|