Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reactivate logging to Sentry #34

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions Classes/Command/SentryCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@
*/

use Flownative\Sentry\SentryClient;
use Flownative\Sentry\Test\JsonSerializableTestArgument;
use Flownative\Sentry\Test\SentryClientTestException;
use Flownative\Sentry\Test\StringableTestArgument;
use Flownative\Sentry\Test\ThrowingClass;
use Neos\Flow\Annotations\Inject;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Sentry\Severity;

final class SentryCommandController extends CommandController
{
/**
* @Inject
* @Flow\Inject
* @var SentryClient
*/
protected $sentryClient;
Expand All @@ -43,6 +42,7 @@ final class SentryCommandController extends CommandController
public function testCommand(): void
{
$this->output->outputLine('<b>Testing Sentry setup …</b>');
$this->output->outputLine();
$this->output->outputLine('Using the following configuration:');

$options = $this->sentryClient->getOptions();
Expand All @@ -58,15 +58,22 @@ public function testCommand(): void
'Value'
]);

$eventId = $this->sentryClient->captureMessage(
$captureResult = $this->sentryClient->captureMessage(
'Flownative Sentry Plugin Test',
Severity::debug(),
[
'Flownative Sentry Client Version' => 'dev'
]
);

$this->outputLine('<success>An informational message was sent to Sentry</success> Event ID: #%s', [$eventId]);
$this->outputLine();
$this->outputLine('An informational message was sent to Sentry');
if ($captureResult) {
$this->outputLine('<success>Event ID: #' . $captureResult->eventId . '</success>');
} else {
$this->outputLine('<error>' . $captureResult->message . '</error>');
}

$this->outputLine();
$this->outputLine('This command will now throw an exception for testing purposes.');
$this->outputLine();
Expand Down
4 changes: 0 additions & 4 deletions Classes/Context/DefaultUserContextService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@

class DefaultUserContextService implements UserContextServiceInterface
{
/**
* @param Context $securityContext
* @return UserContextInterface
*/
public function getUserContext(Context $securityContext): UserContextInterface
{
$userContext = new UserContext();
Expand Down
33 changes: 3 additions & 30 deletions Classes/Context/UserContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,64 +15,37 @@

class UserContext implements UserContextInterface
{
/**
* @var string
*/
private $id = '';
private string $id = '';

/**
* @var string
*/
private $username = '';
private string $username = '';

/**
* @var string
*/
private $email = '';
private string $email = '';

/**
* @return string
*/
public function getId(): string
{
return $this->id;
}

/**
* @param string $id
*/
public function setId(string $id): void
{
$this->id = $id;
}

/**
* @return string
*/
public function getUsername(): string
{
return $this->username;
}

/**
* @param string $username
*/
public function setUsername(string $username): void
{
$this->username = $username;
}

/**
* @return string
*/
public function getEmail(): string
{
return $this->email;
}

/**
* @param string $email
*/
public function setEmail(string $email): void
{
$this->email = $email;
Expand Down
11 changes: 0 additions & 11 deletions Classes/Context/UserContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,10 @@

interface UserContextInterface
{
/**
* @return string|null
*/
public function getId(): ?string;

/**
* @return string|null
*/
public function getUsername(): ?string;

/**
* @return string|null
*/
public function getEmail(): ?string;

/**
Expand All @@ -36,8 +27,6 @@ public function getEmail(): ?string;
* "id", "username", "email"
*
* The keys must exist, but the values may be empty
*
* @return array
*/
public function toArray(): array;
}
3 changes: 0 additions & 3 deletions Classes/Context/UserContextServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ interface UserContextServiceInterface
{
/**
* Returns ContextData to be added to the sentry entry
*
* @param Context $securityContext
* @return UserContextInterface
*/
public function getUserContext(Context $securityContext): UserContextInterface;
}
59 changes: 0 additions & 59 deletions Classes/Exception/DebugExceptionHandler.php

This file was deleted.

59 changes: 0 additions & 59 deletions Classes/Exception/ProductionExceptionHandler.php

This file was deleted.

12 changes: 12 additions & 0 deletions Classes/Log/CaptureResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);

namespace Flownative\Sentry\Log;

class CaptureResult {
public function __construct(
public readonly bool $suceess,
public readonly string $message,
public readonly string $eventId
) {}
}
45 changes: 18 additions & 27 deletions Classes/Log/SentryFileBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,20 @@ class SentryFileBackend extends FileBackend
{
use SentryClientTrait;

/**
* @var bool
*/
private $capturingMessage = false;
private bool $capturingMessage = false;

/**
* Appends the given message along with the additional information into the log.
*
* @param string $message The message to log
* @param int $severity One of the LOG_* constants
* @param mixed $additionalData A variable containing more information about the event to be logged
* @param string|null $packageKey Key of the package triggering the log (determined automatically if not specified)
* @param string|null $className Name of the class triggering the log (determined automatically if not specified)
* @param string|null $methodName Name of the method triggering the log (determined automatically if not specified)
* @param string|null $packageKey Key of the package triggering the log
* @param string|null $className Name of the class triggering the log
* @param string|null $methodName Name of the method triggering the log
* @return void
* @api
*/
public function append(string $message, int $severity = LOG_INFO, $additionalData = null, string $packageKey = null, string $className = null, string $methodName = null): void
public function append(string $message, int $severity = LOG_INFO, $additionalData = null, ?string $packageKey = null, ?string $className = null, ?string $methodName = null): void
{
if ($this->capturingMessage) {
return;
Expand All @@ -49,29 +45,24 @@ public function append(string $message, int $severity = LOG_INFO, $additionalDat

$sentryClient = self::getSentryClient();
if ($severity <= LOG_NOTICE && $sentryClient) {
switch ($severity) {
case LOG_WARNING:
$sentrySeverity = Severity::warning();
break;
case LOG_ERR:
$sentrySeverity = Severity::error();
break;
case LOG_CRIT:
case LOG_ALERT:
case LOG_EMERG:
$sentrySeverity = Severity::fatal();
break;
default:
$sentrySeverity = Severity::info();
$sentrySeverity = match ($severity) {
LOG_WARNING => Severity::warning(),
LOG_ERR => Severity::error(),
LOG_CRIT, LOG_ALERT, LOG_EMERG => Severity::fatal(),
default => Severity::info(),
};

$captureResult = $sentryClient->captureMessage($message, $sentrySeverity, ['Additional Data' => $additionalData]);
if ($captureResult) {
$message .= ' (Sentry: #' . $captureResult->eventId . ')';
} else {
$message .= ' (Sentry: ' . $captureResult->message . ')';
}

$sentryClient->captureMessage($message, $sentrySeverity, ['Additional Data' => $additionalData]);
}
parent::append($message, $severity, $additionalData, $packageKey, $className, $methodName);
} catch (\Throwable $throwable) {
echo sprintf('SentryFileBackend: %s (%s)', $throwable->getMessage(), $throwable->getCode());
parent::append(sprintf('%s (%s)', $throwable->getMessage(), $throwable->getCode()), LOG_ERR, 'Flownative.Sentry', __CLASS__, __METHOD__);
} finally {
parent::append($message, $severity, $additionalData, $packageKey, $className, $methodName);
$this->capturingMessage = false;
}
}
Expand Down
Loading