1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Facade\FlareClient; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Throwable; |
7
|
|
|
use Illuminate\Pipeline\Pipeline; |
8
|
|
|
use Facade\FlareClient\Glows\Glow; |
9
|
|
|
use Facade\FlareClient\Http\Client; |
10
|
|
|
use Facade\FlareClient\Glows\Recorder; |
11
|
|
|
use Facade\FlareClient\Concerns\HasContext; |
12
|
|
|
use Facade\FlareClient\Enums\MessageLevels; |
13
|
|
|
use Facade\FlareClient\Middleware\AddGlows; |
14
|
|
|
use Illuminate\Contracts\Container\Container; |
15
|
|
|
use Facade\FlareClient\Middleware\AnonymizeIp; |
16
|
|
|
use Facade\FlareClient\Context\ContextContextDetector; |
17
|
|
|
use Facade\FlareClient\Context\ContextDetectorInterface; |
18
|
|
|
|
19
|
|
|
class Flare |
20
|
|
|
{ |
21
|
|
|
use HasContext; |
22
|
|
|
|
23
|
|
|
/** @var \Facade\FlareClient\Http\Client */ |
24
|
|
|
private $client; |
25
|
|
|
|
26
|
|
|
/** @var \Facade\FlareClient\Api */ |
27
|
|
|
private $api; |
28
|
|
|
|
29
|
|
|
/** @var array */ |
30
|
|
|
private $middleware = []; |
31
|
|
|
|
32
|
|
|
/** @var \Facade\FlareClient\Glows\Recorder */ |
33
|
|
|
private $recorder; |
34
|
|
|
|
35
|
|
|
/** @var string */ |
36
|
|
|
private $applicationPath; |
37
|
|
|
|
38
|
|
|
/** @var \Illuminate\Contracts\Container\Container|null */ |
39
|
|
|
private $container; |
40
|
|
|
|
41
|
|
|
/** @var ContextDetectorInterface */ |
42
|
|
|
private $contextDetector; |
43
|
|
|
|
44
|
|
|
public static function register(string $apiKey, string $apiSecret = null, ContextDetectorInterface $contextDetector = null, Container $container = null) |
45
|
|
|
{ |
46
|
|
|
$client = new Client($apiKey, $apiSecret); |
47
|
|
|
|
48
|
|
|
return new static($client, $contextDetector, $container); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function __construct(Client $client, ContextDetectorInterface $contextDetector = null, Container $container = null, array $middleware = []) |
52
|
|
|
{ |
53
|
|
|
$this->client = $client; |
54
|
|
|
$this->recorder = new Recorder(); |
55
|
|
|
$this->contextDetector = $contextDetector ?? new ContextContextDetector(); |
56
|
|
|
$this->container = $container; |
57
|
|
|
$this->middleware = $middleware; |
58
|
|
|
$this->api = new Api($this->client); |
59
|
|
|
|
60
|
|
|
$this->registerDefaultMiddleware(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getMiddleware(): array |
64
|
|
|
{ |
65
|
|
|
return $this->middleware; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function registerExceptionHandler() |
69
|
|
|
{ |
70
|
|
|
set_exception_handler([$this, 'handleException']); |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function registerDefaultMiddleware() |
76
|
|
|
{ |
77
|
|
|
return $this->registerMiddleware(new AddGlows($this->recorder)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function registerMiddleware($callable) |
81
|
|
|
{ |
82
|
|
|
$this->middleware[] = $callable; |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getMiddlewares(): array |
88
|
|
|
{ |
89
|
|
|
return $this->middleware; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function glow( |
93
|
|
|
string $name, |
94
|
|
|
string $messageLevel = MessageLevels::INFO, |
95
|
|
|
array $metaData = [] |
96
|
|
|
) { |
97
|
|
|
$this->recorder->record(new Glow($name, $messageLevel, $metaData)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function handleException(Throwable $throwable) |
101
|
|
|
{ |
102
|
|
|
$this->report($throwable); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function applicationPath(string $applicationPath) |
106
|
|
|
{ |
107
|
|
|
$this->applicationPath = $applicationPath; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function report(Throwable $throwable, callable $callback = null) |
113
|
|
|
{ |
114
|
|
|
$report = $this->createReport($throwable); |
115
|
|
|
|
116
|
|
|
if (! is_null($callback)) { |
117
|
|
|
call_user_func($callback, $report); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$this->sendReportToApi($report); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function reportMessage(string $message, string $logLevel, callable $callback = null) |
124
|
|
|
{ |
125
|
|
|
$report = $this->createReportFromMessage($message, $logLevel); |
126
|
|
|
|
127
|
|
|
if (! is_null($callback)) { |
128
|
|
|
call_user_func($callback, $report); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->sendReportToApi($report); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function sendTestReport(Throwable $throwable) |
135
|
|
|
{ |
136
|
|
|
$this->api->sendTestReport($this->createReport($throwable)); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
private function sendReportToApi(Report $report) |
140
|
|
|
{ |
141
|
|
|
try { |
142
|
|
|
$this->api->report($report); |
143
|
|
|
} catch (Exception $exception) { |
|
|
|
|
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function reset() |
148
|
|
|
{ |
149
|
|
|
$this->api->sendQueuedReports(); |
150
|
|
|
|
151
|
|
|
$this->userProvidedContext = []; |
152
|
|
|
$this->recorder->reset(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
private function applyAdditionalParameters(Report $report) |
156
|
|
|
{ |
157
|
|
|
$report |
158
|
|
|
->stage($this->stage) |
159
|
|
|
->messageLevel($this->messageLevel) |
160
|
|
|
->setApplicationPath($this->applicationPath) |
161
|
|
|
->userProvidedContext($this->userProvidedContext); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function anonymizeIp() |
165
|
|
|
{ |
166
|
|
|
$this->registerMiddleware(new AnonymizeIp); |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function createReport(Throwable $throwable): Report |
172
|
|
|
{ |
173
|
|
|
$report = Report::createForThrowable( |
174
|
|
|
$throwable, |
175
|
|
|
$this->contextDetector->detectCurrentContext(), |
176
|
|
|
$this->applicationPath |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
return $this->applyMiddlewareToReport($report); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function createReportFromMessage(string $message, string $logLevel): Report |
183
|
|
|
{ |
184
|
|
|
$report = Report::createForMessage( |
185
|
|
|
$message, |
186
|
|
|
$logLevel, |
187
|
|
|
$this->contextDetector->detectCurrentContext(), |
188
|
|
|
$this->applicationPath |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
return $this->applyMiddlewareToReport($report); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
protected function applyMiddlewareToReport(Report $report): Report |
195
|
|
|
{ |
196
|
|
|
$this->applyAdditionalParameters($report); |
197
|
|
|
|
198
|
|
|
$report = (new Pipeline($this->container)) |
199
|
|
|
->send($report) |
200
|
|
|
->through($this->middleware) |
201
|
|
|
->then(function ($report) { |
202
|
|
|
return $report; |
203
|
|
|
}); |
204
|
|
|
|
205
|
|
|
return $report; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|