1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace hiqdev\yii2\monitoring\targets; |
5
|
|
|
|
6
|
|
|
use Sentry\Severity; |
7
|
|
|
use Sentry\State\HubInterface; |
8
|
|
|
use Sentry\State\Scope; |
9
|
|
|
use Throwable; |
10
|
|
|
use yii\log\Logger; |
11
|
|
|
use yii\log\Target; |
12
|
|
|
use yii\web\User; |
13
|
|
|
use Yii; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* SentryTarget records log messages in a Sentry. |
17
|
|
|
*/ |
18
|
|
|
class SentryTarget extends Target |
19
|
|
|
{ |
20
|
|
|
private HubInterface $hub; |
|
|
|
|
21
|
|
|
private User $user; |
22
|
|
|
|
23
|
|
|
public function __construct(HubInterface $hub, User $user, $config = []) |
24
|
|
|
{ |
25
|
|
|
parent::__construct($config); |
26
|
|
|
|
27
|
|
|
$this->hub = $hub; |
28
|
|
|
$this->user = $user; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
*/ |
34
|
|
|
protected function getContextMessage() |
35
|
|
|
{ |
36
|
|
|
return ''; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
|
public function export() |
43
|
|
|
{ |
44
|
|
|
foreach ($this->messages as $message) { |
45
|
|
|
[$msg, $level, $category] = $message; |
46
|
|
|
|
47
|
|
|
$payload = [ |
48
|
|
|
'level' => $this->getLogLevel($level), |
49
|
|
|
'logger' => 'monolog.' . $category, |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
if (is_array($msg)) { |
53
|
|
|
if (isset($msg['message'])) { |
54
|
|
|
$payload['message'] = $msg['message']; |
55
|
|
|
unset($msg['message']); |
56
|
|
|
} |
57
|
|
|
if (isset($msg['exception']) && $msg['exception'] instanceof Throwable) { |
58
|
|
|
$payload['exception'] = $msg['exception']; |
59
|
|
|
unset($msg['exception']); |
60
|
|
|
} |
61
|
|
|
} else if (is_string($msg)) { |
62
|
|
|
$payload['message'] = $msg; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->hub->withScope(function (Scope $scope) use ($msg, $payload): void { |
66
|
|
|
if (is_array($msg)) { |
67
|
|
|
foreach ($msg as $key => $value) { |
68
|
|
|
$scope->setExtra((string) $key, $value); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->enrichWithUserData($scope); |
73
|
|
|
$this->hub->captureEvent($payload); |
74
|
|
|
}); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Translates Yii2 log levels to Sentry Severity. |
80
|
|
|
* |
81
|
|
|
* @param int $level |
82
|
|
|
* @return Severity |
83
|
|
|
*/ |
84
|
|
|
private function getLogLevel($level): Severity |
85
|
|
|
{ |
86
|
|
|
switch ($level) { |
87
|
|
|
case Logger::LEVEL_PROFILE: |
88
|
|
|
case Logger::LEVEL_PROFILE_BEGIN: |
89
|
|
|
case Logger::LEVEL_PROFILE_END: |
90
|
|
|
case Logger::LEVEL_TRACE: |
91
|
|
|
return Severity::debug(); |
92
|
|
|
case Logger::LEVEL_WARNING: |
93
|
|
|
return Severity::warning(); |
94
|
|
|
case Logger::LEVEL_ERROR: |
95
|
|
|
return Severity::error(); |
96
|
|
|
case Logger::LEVEL_INFO: |
97
|
|
|
default: |
98
|
|
|
return Severity::info(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function enrichWithUserData(Scope $scope): void |
103
|
|
|
{ |
104
|
|
|
$data = [ |
105
|
|
|
'ip_address' => Yii::$app->getRequest()->getUserIp(), |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
if (!$this->user->isGuest) { |
109
|
|
|
$identity = $this->user->getIdentity(); |
110
|
|
|
$data['id'] = $identity->id; |
111
|
|
|
$data['login'] = $identity->login ?? $identity->username ?? $identity->email ?? null; |
112
|
|
|
$data['email'] = $identity->email ?? null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$scope->setUser(array_filter($data), true); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|