Skip to content

Commit

Permalink
Refactor logger option
Browse files Browse the repository at this point in the history
  • Loading branch information
cleptric committed Nov 6, 2023
1 parent 661cffb commit 0c41928
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 135 deletions.
12 changes: 11 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ parameters:
count: 1
path: src/Integration/RequestIntegration.php

-
message: "#^Parameter \\#2 \\.\\.\\.\\$values of function sprintf expects bool\\|float\\|int\\|string\\|null, mixed given\\.$#"
count: 1
path: src/Logger/DebugFileLogger.php

-
message: "#^Parameter \\#2 \\.\\.\\.\\$values of function sprintf expects bool\\|float\\|int\\|string\\|null, mixed given\\.$#"
count: 1
path: src/Logger/DebugStdOutLogger.php

-
message: "#^Parameter \\#1 \\$level of method Monolog\\\\Handler\\\\AbstractHandler\\:\\:__construct\\(\\) expects 100\\|200\\|250\\|300\\|400\\|500\\|550\\|600\\|'ALERT'\\|'alert'\\|'CRITICAL'\\|'critical'\\|'DEBUG'\\|'debug'\\|'EMERGENCY'\\|'emergency'\\|'ERROR'\\|'error'\\|'INFO'\\|'info'\\|'NOTICE'\\|'notice'\\|'WARNING'\\|'warning'\\|Monolog\\\\Level, int\\|Monolog\\\\Level\\|string given\\.$#"
count: 1
Expand Down Expand Up @@ -136,7 +146,7 @@ parameters:
path: src/Options.php

-
message: "#^Method Sentry\\\\Options\\:\\:getLogger\\(\\) should return string but returns mixed\\.$#"
message: "#^Method Sentry\\\\Options\\:\\:getLogger\\(\\) should return Psr\\\\Log\\\\LoggerInterface\\|null but returns mixed\\.$#"
count: 1
path: src/Options.php

Expand Down
11 changes: 4 additions & 7 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,12 @@ public function __construct(
) {
$this->options = $options;
$this->transport = $transport;
$this->logger = $logger ?? new NullLogger();
$this->integrations = IntegrationRegistry::getInstance()->setupIntegrations($options, $this->logger);
$this->stacktraceBuilder = new StacktraceBuilder($options, $representationSerializer ?? new RepresentationSerializer($this->options));
$this->sdkIdentifier = $sdkIdentifier ?? self::SDK_IDENTIFIER;
$this->sdkVersion = $sdkVersion ?? self::SDK_VERSION;
$this->stacktraceBuilder = new StacktraceBuilder($options, $representationSerializer ?? new RepresentationSerializer($this->options));
$this->logger = $logger ?? new NullLogger();

$this->integrations = IntegrationRegistry::getInstance()->setupIntegrations($options, $this->logger);
}

/**
Expand Down Expand Up @@ -269,10 +270,6 @@ private function prepareEvent(Event $event, ?EventHint $hint = null, ?Scope $sco
$event->setEnvironment($this->options->getEnvironment() ?? Event::DEFAULT_ENVIRONMENT);
}

if ($event->getLogger() === null) {
$event->setLogger($this->options->getLogger(false));
}

$isTransaction = EventType::transaction() === $event->getType();
$sampleRate = $this->options->getSampleRate();

Expand Down
7 changes: 7 additions & 0 deletions src/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public function __construct(?Options $options = null)
{
$this->options = $options ?? new Options();

$this->logger = $this->options->getLogger() ?? null;

$this->httpClient = $this->options->getHttpClient() ?? new HttpClient($this->sdkIdentifier, $this->sdkVersion);
$this->transport = $this->options->getTransport() ?? new HttpTransport(
$this->options,
Expand Down Expand Up @@ -92,6 +94,11 @@ public function setRepresentationSerializer(RepresentationSerializerInterface $r
return $this;
}

public function getLogger(): ?LoggerInterface

Check warning on line 97 in src/ClientBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/ClientBuilder.php#L97

Added line #L97 was not covered by tests
{
return $this->logger;

Check warning on line 99 in src/ClientBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/ClientBuilder.php#L99

Added line #L99 was not covered by tests
}

public function setLogger(LoggerInterface $logger): self
{
$this->logger = $logger;
Expand Down
29 changes: 29 additions & 0 deletions src/Logger/DebugFileLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Sentry\Logger;

use Psr\Log\AbstractLogger;

class DebugFileLogger extends AbstractLogger
{
/**
* @var string
*/
private $filePath;

public function __construct(string $filePath)
{
$this->filePath = $filePath;
}

Check warning on line 19 in src/Logger/DebugFileLogger.php

View check run for this annotation

Codecov / codecov/patch

src/Logger/DebugFileLogger.php#L16-L19

Added lines #L16 - L19 were not covered by tests

/**
* @param mixed $level
* @param mixed[] $context
*/
public function log($level, \Stringable|string $message, array $context = []): void
{
file_put_contents($this->filePath, sprintf("sentry/sentry: [%s] %s\n", $level, (string) $message), \FILE_APPEND);
}

Check warning on line 28 in src/Logger/DebugFileLogger.php

View check run for this annotation

Codecov / codecov/patch

src/Logger/DebugFileLogger.php#L25-L28

Added lines #L25 - L28 were not covered by tests
}
19 changes: 19 additions & 0 deletions src/Logger/DebugStdOutLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Sentry\Logger;

use Psr\Log\AbstractLogger;

class DebugStdOutLogger extends AbstractLogger
{
/**
* @param mixed $level
* @param mixed[] $context
*/
public function log($level, \Stringable|string $message, array $context = []): void
{
file_put_contents('php://stdout', sprintf("sentry/sentry: [%s] %s\n", $level, (string) $message));
}

Check warning on line 18 in src/Logger/DebugStdOutLogger.php

View check run for this annotation

Codecov / codecov/patch

src/Logger/DebugStdOutLogger.php#L15-L18

Added lines #L15 - L18 were not covered by tests
}
33 changes: 7 additions & 26 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sentry;

use Psr\Log\LoggerInterface;
use Sentry\HttpClient\HttpClientInterface;
use Sentry\Integration\ErrorListenerIntegration;
use Sentry\Integration\IntegrationInterface;
Expand Down Expand Up @@ -309,30 +310,18 @@ public function setInAppIncludedPaths(array $paths): self
}

/**
* Gets the logger used by Sentry.
*
* @deprecated since version 3.2, to be removed in 4.0
* Gets a PSR-3 compatible logger to log internal debug messages.
*/
public function getLogger(/* bool $triggerDeprecation = true */): string
public function getLogger(): ?LoggerInterface
{
if (\func_num_args() === 0 || func_get_arg(0) !== false) {
@trigger_error(sprintf('Method %s() is deprecated since version 3.2 and will be removed in 4.0.', __METHOD__), \E_USER_DEPRECATED);
}

return $this->options['logger'];
}

/**
* Sets the logger used by Sentry.
*
* @param string $logger The logger
*
* @deprecated since version 3.2, to be removed in 4.0
* Sets a PSR-3 compatible logger to log internal debug messages.
*/
public function setLogger(string $logger): self
public function setLogger(LoggerInterface $logger): self
{
@trigger_error(sprintf('Method %s() is deprecated since version 3.2 and will be removed in 4.0.', __METHOD__), \E_USER_DEPRECATED);

$options = array_merge($this->options, ['logger' => $logger]);

$this->options = $this->resolver->resolve($options);
Expand Down Expand Up @@ -994,7 +983,7 @@ private function configureOptions(OptionsResolver $resolver): void
'attach_stacktrace' => false,
'context_lines' => 5,
'environment' => $_SERVER['SENTRY_ENVIRONMENT'] ?? null,
'logger' => 'php',
'logger' => null,
'release' => $_SERVER['SENTRY_RELEASE'] ?? null,
'dsn' => $_SERVER['SENTRY_DSN'] ?? null,
'server_name' => gethostname(),
Expand Down Expand Up @@ -1041,7 +1030,7 @@ private function configureOptions(OptionsResolver $resolver): void
$resolver->setAllowedTypes('environment', ['null', 'string']);
$resolver->setAllowedTypes('in_app_exclude', 'string[]');
$resolver->setAllowedTypes('in_app_include', 'string[]');
$resolver->setAllowedTypes('logger', ['null', 'string']);
$resolver->setAllowedTypes('logger', ['null', LoggerInterface::class]);
$resolver->setAllowedTypes('release', ['null', 'string']);
$resolver->setAllowedTypes('dsn', ['null', 'string', 'bool', Dsn::class]);
$resolver->setAllowedTypes('server_name', 'string');
Expand Down Expand Up @@ -1089,14 +1078,6 @@ private function configureOptions(OptionsResolver $resolver): void
$resolver->setNormalizer('in_app_include', function (SymfonyOptions $options, array $value) {
return array_map([$this, 'normalizeAbsolutePath'], $value);
});

$resolver->setNormalizer('logger', function (SymfonyOptions $options, ?string $value): ?string {
if ($value !== 'php') {
@trigger_error('The option "logger" is deprecated.', \E_USER_DEPRECATED);
}

return $value;
});
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Transport/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
/**
* This class contains the details of the sending operation of an event, e.g.
* if it was sent successfully or if it was skipped because of some reason.
*
* @internal
*/
final class Result
class Result
{
/**
* @var ResultStatus The status of the sending operation of the event
Expand Down
4 changes: 0 additions & 4 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ public static function captureEventDataProvider(): \Generator
{
$event = Event::createEvent();
$expectedEvent = clone $event;
$expectedEvent->setLogger('php');
$expectedEvent->setServerName('example.com');
$expectedEvent->setRelease('0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33');
$expectedEvent->setEnvironment('development');
Expand All @@ -241,7 +240,6 @@ public static function captureEventDataProvider(): \Generator
$event->setTags(['context' => 'production']);

$expectedEvent = clone $event;
$expectedEvent->setLogger('php');
$expectedEvent->setTags(['context' => 'production', 'ios_version' => '14.0']);

yield 'Options set && event properties set => event properties override options' => [
Expand All @@ -259,7 +257,6 @@ public static function captureEventDataProvider(): \Generator
$event->setServerName('example.com');

$expectedEvent = clone $event;
$expectedEvent->setLogger('php');
$expectedEvent->setEnvironment('production');

yield 'Environment option set to null && no event property set => fallback to default value' => [
Expand All @@ -274,7 +271,6 @@ public static function captureEventDataProvider(): \Generator
$event->setExceptions([new ExceptionDataBag(new \ErrorException())]);

$expectedEvent = clone $event;
$expectedEvent->setLogger('php');
$expectedEvent->setEnvironment('production');

yield 'Error level is set && exception is instance of ErrorException => preserve the error level set by the user' => [
Expand Down
Loading

0 comments on commit 0c41928

Please sign in to comment.