Skip to content

Commit 285e460

Browse files
author
Michal Cech
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # src/Client.php
2 parents e7d699c + 672506c commit 285e460

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

src/Client.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use CEvent;
88
use CJavaScript;
99
use CMap;
10+
use Sentry\Breadcrumb;
1011
use Sentry\Severity;
1112
use Sentry\State\HubAdapter;
1213
use Sentry\State\HubInterface;
@@ -115,6 +116,19 @@ public function captureMessage(string $message, ?Severity $level = null, ?Scope
115116
return $this->getSentry()->getClient()->captureMessage($message, $level, $scope);
116117
}
117118

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+
118132
/**
119133
* Logs an exception.
120134
*
@@ -302,6 +316,8 @@ public function handleBeginRequestEvent(\CEvent $event): void
302316
*/
303317
public function handleEndRequestEvent(\CEvent $event): void
304318
{
319+
$this->grabPushLogsFromLoggerToBreadcrumbs();
320+
305321
if ($this->appSpan !== null) {
306322
$this->appSpan->finish();
307323
}
@@ -316,13 +332,17 @@ public function handleEndRequestEvent(\CEvent $event): void
316332

317333
public function handleExceptionEvent(\CEvent $event): void
318334
{
319-
$this->rootTransaction->setHttpStatus(500);
335+
$this->grabPushLogsFromLoggerToBreadcrumbs();
336+
337+
if ($this->rootTransaction !== null) {
338+
$this->rootTransaction->setHttpStatus(500);
339+
}
320340
}
321341

322342
/**
323343
* @param string[]|int[]|bool[] $data
324344
*/
325-
public function startRootTransaction(string $description, array $data = []): Transaction
345+
private function startRootTransaction(string $description, array $data = []): Transaction
326346
{
327347
$context = new TransactionContext();
328348
$context->setOp('yii-app');
@@ -385,4 +405,28 @@ private function operationNameFromBeginRequestEvent(CEvent $event): string
385405
}
386406

387407
//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+
}
388432
}

0 commit comments

Comments
 (0)