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 ;
@@ -115,6 +116,19 @@ public function captureMessage(string $message, ?Severity $level = null, ?Scope
115
116
return $ this ->getSentry ()->getClient ()->captureMessage ($ message , $ level , $ scope );
116
117
}
117
118
119
+ public function addBreadcrumb (
120
+ string $ level ,
121
+ string $ type ,
122
+ string $ category ,
123
+ ?string $ message = null ,
124
+ array $ metadata = [],
125
+ ?float $ timestamp = null
126
+ ): bool {
127
+ return $ this ->getSentry ()->addBreadcrumb (
128
+ new Breadcrumb ($ level , $ type , $ category , $ message , $ metadata , $ timestamp )
129
+ );
130
+ }
131
+
118
132
/**
119
133
* Logs an exception.
120
134
*
@@ -302,6 +316,8 @@ public function handleBeginRequestEvent(\CEvent $event): void
302
316
*/
303
317
public function handleEndRequestEvent (\CEvent $ event ): void
304
318
{
319
+ $ this ->grabPushLogsFromLoggerToBreadcrumbs ();
320
+
305
321
if ($ this ->appSpan !== null ) {
306
322
$ this ->appSpan ->finish ();
307
323
}
@@ -316,13 +332,17 @@ public function handleEndRequestEvent(\CEvent $event): void
316
332
317
333
public function handleExceptionEvent (\CEvent $ event ): void
318
334
{
319
- $ this ->rootTransaction ->setHttpStatus (500 );
335
+ $ this ->grabPushLogsFromLoggerToBreadcrumbs ();
336
+
337
+ if ($ this ->rootTransaction !== null ) {
338
+ $ this ->rootTransaction ->setHttpStatus (500 );
339
+ }
320
340
}
321
341
322
342
/**
323
343
* @param string[]|int[]|bool[] $data
324
344
*/
325
- public function startRootTransaction (string $ description , array $ data = []): Transaction
345
+ private function startRootTransaction (string $ description , array $ data = []): Transaction
326
346
{
327
347
$ context = new TransactionContext ();
328
348
$ context ->setOp ('yii-app ' );
@@ -385,4 +405,28 @@ private function operationNameFromBeginRequestEvent(CEvent $event): string
385
405
}
386
406
387
407
//endregion
408
+ private function grabPushLogsFromLoggerToBreadcrumbs (): void
409
+ {
410
+ $ logs = Yii::getLogger ()->getLogs ();
411
+
412
+ foreach ($ logs as $ log ) {
413
+ /**
414
+ * @var string $message
415
+ * @var string $level
416
+ * @var string $category
417
+ */
418
+ list ($ message , $ level , $ category ) = $ log ;
419
+
420
+ // remove stack trace from message
421
+ if (($ pos = strpos ($ message , 'Stack trace: ' )) !== false ) {
422
+ $ message = substr ($ message , 0 , $ pos );
423
+ }
424
+
425
+ if ($ level === 'trace ' ) {
426
+ $ level = Breadcrumb::LEVEL_DEBUG ;
427
+ }
428
+
429
+ $ this ->addBreadcrumb ($ level , Breadcrumb::TYPE_DEFAULT , $ category , $ message );
430
+ }
431
+ }
388
432
}
0 commit comments