Skip to content

Commit

Permalink
Merge pull request #68 from o0h/log-context
Browse files Browse the repository at this point in the history
Make CakePHP Logger context data to be recorded as extra data
  • Loading branch information
o0h authored May 29, 2021
2 parents bc92c80 + a234f00 commit 51734eb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function getHub(): Hub
/**
* Capture exception for sentry.
*
* @param mixed $level error level
* @param string|int $level error level
* @param string $message error message
* @param array $context subject
* @return void
Expand All @@ -87,7 +87,6 @@ public function capture($level, string $message, array $context): void
$lastEventId = $this->hub->captureException($exception);
} else {
$hint = new EventHint();
$hint->extra = $context;

$stackTrace = $context['stackTrace'] ?? false;
if ($stackTrace) {
Expand All @@ -96,7 +95,11 @@ public function capture($level, string $message, array $context): void
$stackTrace[0]['file'],
$stackTrace[0]['line']
);
unset($context['stackTrace']);
}
$this->hub->configureScope(function (\Sentry\State\Scope $scope) use ($context) {
$scope->setExtras($context);
});

$severity = $this->convertLevelToSeverity($level);
$lastEventId = $this->hub->captureMessage($message, $severity, $hint);
Expand Down
39 changes: 31 additions & 8 deletions tests/TestCase/Http/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ public function testCaptureException(): void
}

/**
* Test capture error
* Test capture other than exception
*
* @return array // FIXME: In fact array<string,MethodProphecy[]>, but getMethodProphecies declare as MethodProphecy[]
*/
public function testCaptureError(): array
public function testCaptureNotHavingException(): array
{
$subject = new Client([]);
$sentryClientP = $this->prophesize(ClientInterface::class);
Expand Down Expand Up @@ -185,14 +185,14 @@ public function testCaptureError(): array
}

/**
* Test capture error compatible with the error-level is specified by int or string
* Test capture compatible with the error-level is specified by int or string
*
* @depends testCaptureError
* @depends testCaptureNotHavingException
* @param array&array<string,MethodProphecy[]> $mockMethodList
*/
public function testCaptureErrorWithErrorLevelInteger(array $mockMethodList): void
public function testCaptureWithErrorLevelInteger(array $mockMethodList): void
{
// Rebuild ObjectProphecy in the same context with testCaptureError.
// Rebuild ObjectProphecy in the same context with testCaptureNotHavingException.
$sentryClientP = $this->prophesize(ClientInterface::class);
foreach ($mockMethodList as $mockMethod) {
$sentryClientP->addMethodProphecy($mockMethod[0]);
Expand All @@ -205,9 +205,9 @@ public function testCaptureErrorWithErrorLevelInteger(array $mockMethodList): vo
}

/**
* Test capture error fill with injected breadcrumbs
* Test capture fill with injected breadcrumbs
*/
public function testCaptureErrorBuildBreadcrumbs(): void
public function testCaptureBuildBreadcrumbs(): void
{
$stackTrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT);

Expand Down Expand Up @@ -243,6 +243,29 @@ public function testCaptureErrorBuildBreadcrumbs(): void
$subject->capture('warning', 'some error', compact('stackTrace'));
}

/**
* Test capture pass cakephp-log's context as additional data
*/
public function testCaptureWithAdditionalData(): void
{
$callback = function (\Sentry\Event $event, ?\Sentry\EventHint $hint) use (&$actualEvent) {
$actualEvent = $event;
};

$userConfig = [
'dsn' => false,
'before_send' => $callback,
];

Configure::write('Sentry', $userConfig);
$subject = new Client([]);

$context = ['this is' => 'additional'];
$subject->capture('warning', 'some error', $context);

$this->assertSame($context, $actualEvent->getExtra());
}

/**
* Check capture dispatch beforeCapture
*/
Expand Down

0 comments on commit 51734eb

Please sign in to comment.