7
7
use CEvent ;
8
8
use CJavaScript ;
9
9
use CMap ;
10
+ use Sentry \Breadcrumb ;
10
11
use Sentry \Severity ;
11
12
use Sentry \State \HubAdapter ;
12
13
use Sentry \State \HubInterface ;
@@ -111,6 +112,19 @@ public function captureMessage(string $message, ?Severity $level = null, ?Scope
111
112
return $ this ->getSentry ()->getClient ()->captureMessage ($ message , $ level , $ scope );
112
113
}
113
114
115
+ public function addBreadcrumb (
116
+ string $ level ,
117
+ string $ type ,
118
+ string $ category ,
119
+ ?string $ message = null ,
120
+ array $ metadata = [],
121
+ ?float $ timestamp = null
122
+ ): bool {
123
+ return $ this ->getSentry ()->addBreadcrumb (
124
+ new Breadcrumb ($ level , $ type , $ category , $ message , $ metadata , $ timestamp )
125
+ );
126
+ }
127
+
114
128
/**
115
129
* Logs an exception.
116
130
*
@@ -121,7 +135,9 @@ public function captureMessage(string $message, ?Severity $level = null, ?Scope
121
135
*/
122
136
public function captureException (\Throwable $ exception , ?Scope $ scope = null ): ?string
123
137
{
124
- $ this ->rootTransaction ->setHttpStatus (500 );
138
+ if ($ this ->rootTransaction !== null ) {
139
+ $ this ->rootTransaction ->setHttpStatus (500 );
140
+ }
125
141
return $ this ->getSentry ()->getClient ()->captureException ($ exception , $ scope );
126
142
}
127
143
@@ -291,6 +307,8 @@ public function handleBeginRequestEvent(\CEvent $event): void
291
307
*/
292
308
public function handleEndRequestEvent (\CEvent $ event ): void
293
309
{
310
+ $ this ->grabPushLogsFromLoggerToBreadcrumbs ();
311
+
294
312
if ($ this ->appSpan !== null ) {
295
313
$ this ->appSpan ->finish ();
296
314
}
@@ -305,13 +323,17 @@ public function handleEndRequestEvent(\CEvent $event): void
305
323
306
324
public function handleExceptionEvent (\CEvent $ event ): void
307
325
{
308
- $ this ->rootTransaction ->setHttpStatus (500 );
326
+ $ this ->grabPushLogsFromLoggerToBreadcrumbs ();
327
+
328
+ if ($ this ->rootTransaction !== null ) {
329
+ $ this ->rootTransaction ->setHttpStatus (500 );
330
+ }
309
331
}
310
332
311
333
/**
312
334
* @param string[]|int[]|bool[] $data
313
335
*/
314
- public function startRootTransaction (string $ description , array $ data = []): Transaction
336
+ private function startRootTransaction (string $ description , array $ data = []): Transaction
315
337
{
316
338
$ context = new TransactionContext ();
317
339
$ context ->setOp ('yii-app ' );
@@ -374,4 +396,28 @@ private function operationNameFromBeginRequestEvent(CEvent $event): string
374
396
}
375
397
376
398
//endregion
399
+ private function grabPushLogsFromLoggerToBreadcrumbs (): void
400
+ {
401
+ $ logs = Yii::getLogger ()->getLogs ();
402
+
403
+ foreach ($ logs as $ log ) {
404
+ /**
405
+ * @var string $message
406
+ * @var string $level
407
+ * @var string $category
408
+ */
409
+ list ($ message , $ level , $ category ) = $ log ;
410
+
411
+ // remove stack trace from message
412
+ if (($ pos = strpos ($ message , 'Stack trace: ' )) !== false ) {
413
+ $ message = substr ($ message , 0 , $ pos );
414
+ }
415
+
416
+ if ($ level === 'trace ' ) {
417
+ $ level = Breadcrumb::LEVEL_DEBUG ;
418
+ }
419
+
420
+ $ this ->addBreadcrumb ($ level , Breadcrumb::TYPE_DEFAULT , $ category , $ message );
421
+ }
422
+ }
377
423
}
0 commit comments