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

Added support for psr/log v3 #577

Merged
merged 2 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"require": {
"php": ">=8.0.0 <9.0",
"ext-curl": "*",
"psr/log": "^1 || ^2",
"psr/log": "^1 || ^2 || ^3",
"monolog/monolog": "^2"
},

Expand Down
83 changes: 61 additions & 22 deletions src/Rollbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Rollbar;

use Psr\Log\InvalidArgumentException;
use Rollbar\Payload\Level;
use Rollbar\Handlers\FatalHandler;
use Rollbar\Handlers\ErrorHandler;
use Rollbar\Handlers\ExceptionHandler;
use Stringable;
use Throwable;

class Rollbar
Expand Down Expand Up @@ -90,12 +92,49 @@ public static function scope($config)
return self::$logger->scope($config);
}

public static function log($level, $toLog, $extra = array())
/**
* Logs a message to the Rollbar service with the specified level.
*
* @param Level|string $level The severity level of the message.
* Must be one of the levels as defined in
* the {@see Level} constants.
* @param string|Stringable $message The log message.
* @param array $context Arbitrary data.
*
* @return void
*
* @throws InvalidArgumentException If $level is not a valid level.
* @throws Throwable Rethrown $message if it is {@see Throwable} and {@see Config::raiseOnError} is true.
*/
public static function log($level, string|Stringable $message, array $context = array()): void
danielmorell marked this conversation as resolved.
Show resolved Hide resolved
{
if (is_null(self::$logger)) {
return;
}
self::$logger->log($level, $message, $context);
}

/**
* Creates the {@see Response} object and reports the message to the Rollbar
* service.
*
* @param string|Level $level The severity level to send to Rollbar.
* @param string|Stringable $message The log message.
* @param array $context Any additional context data.
*
* @return Response
*
* @throws InvalidArgumentException If $level is not a valid level.
* @throws Throwable Rethrown $message if it is {@see Throwable} and {@see Config::raiseOnError} is true.
*
* @since 4.0.0
*/
public static function report($level, string|Stringable $message, array $context = array()): Response
{
if (is_null(self::$logger)) {
return self::getNotInitializedResponse();
}
return self::$logger->log($level, $toLog, (array)$extra);
return self::$logger->report($level, $message, $context);
}

/**
Expand All @@ -107,49 +146,49 @@ public static function logUncaught($level, Throwable $toLog, $extra = array())
return self::getNotInitializedResponse();
}
$toLog->isUncaught = true;
$result = self::$logger->log($level, $toLog, (array)$extra);
$result = self::$logger->report($level, $toLog, (array)$extra);
unset($toLog->isUncaught);
danielmorell marked this conversation as resolved.
Show resolved Hide resolved
return $result;
}

public static function debug($toLog, $extra = array())
public static function debug(string|Stringable $message, array $context = array()): void
{
return self::log(Level::DEBUG, $toLog, $extra);
self::log(Level::DEBUG, $message, $context);
}

public static function info($toLog, $extra = array())
public static function info(string|Stringable $message, array $context = array()): void
{
return self::log(Level::INFO, $toLog, $extra);
self::log(Level::INFO, $message, $context);
}

public static function notice($toLog, $extra = array())
public static function notice(string|Stringable $message, array $context = array()): void
{
return self::log(Level::NOTICE, $toLog, $extra);
self::log(Level::NOTICE, $message, $context);
}

public static function warning($toLog, $extra = array())
public static function warning(string|Stringable $message, array $context = array()): void
{
return self::log(Level::WARNING, $toLog, $extra);
self::log(Level::WARNING, $message, $context);
}

public static function error($toLog, $extra = array())
public static function error(string|Stringable $message, array $context = array()): void
{
return self::log(Level::ERROR, $toLog, $extra);
self::log(Level::ERROR, $message, $context);
}

public static function critical($toLog, $extra = array())
public static function critical(string|Stringable $message, array $context = array()): void
{
return self::log(Level::CRITICAL, $toLog, $extra);
self::log(Level::CRITICAL, $message, $context);
}

public static function alert($toLog, $extra = array())
public static function alert(string|Stringable $message, array $context = array()): void
{
return self::log(Level::ALERT, $toLog, $extra);
self::log(Level::ALERT, $message, $context);
}

public static function emergency($toLog, $extra = array())
public static function emergency(string|Stringable $message, array $context = array()): void
{
return self::log(Level::EMERGENCY, $toLog, $extra);
self::log(Level::EMERGENCY, $message, $context);
}

public static function setupExceptionHandling()
Expand All @@ -170,7 +209,7 @@ public static function setupFatalHandling()
self::$fatalHandler->register();
}

private static function getNotInitializedResponse()
private static function getNotInitializedResponse(): Response
{
return new Response(0, "Rollbar Not Initialized");
}
Expand Down Expand Up @@ -247,7 +286,7 @@ public static function destroy()
public static function report_exception($exc, $extra_data = null, $payload_data = null)
danielmorell marked this conversation as resolved.
Show resolved Hide resolved
{
$extra_data = array_merge($extra_data ?? [], $payload_data ?? []);
return self::log(Level::ERROR, $exc, $extra_data)->getUuid();
return self::report(Level::ERROR, $exc, $extra_data)->getUuid();
}

/**
Expand All @@ -266,7 +305,7 @@ public static function report_message($message, $level = null, $extra_data = nul
{
$level = $level ?? Level::ERROR;
$extra_data = array_merge($extra_data ?? [], $payload_data ?? []);
return self::log($level, $message, $extra_data)->getUuid();
return self::report($level, $message, $extra_data)->getUuid();
}


Expand Down
67 changes: 49 additions & 18 deletions src/RollbarLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Rollbar;

use Psr\Log\InvalidArgumentException;
use Stringable;
use Throwable;
use Psr\Log\AbstractLogger;
use Rollbar\Payload\Payload;
Expand Down Expand Up @@ -85,17 +87,46 @@ public function getCustom()
}

/**
* @param Level|string $level
* @param mixed $toLog
* @param array $context
* Logs a message to the Rollbar service with the specified level.
*
* @param Level|string $level The severity level of the message.
* Must be one of the levels as defined in
* the {@see Level} constants.
* @param string|Stringable $message The log message.
* @param array $context Arbitrary data.
*
* @return void
*
* @throws InvalidArgumentException If $level is not a valid level.
* @throws Throwable Rethrown $message if it is {@see Throwable} and {@see Config::raiseOnError} is true.
*/
public function log($level, string|Stringable $message, array $context = array()): void
{
$this->report($level, $message, $context);
}

/**
* Creates the {@see Response} object and reports the message to the Rollbar
* service.
*
* @param string|Level $level The severity level to send to Rollbar.
* @param string|Stringable $message The log message.
* @param array $context Any additional context data.
*
* @return Response
*
* @throws InvalidArgumentException If $level is not a valid level.
* @throws Throwable Rethrown $message if it is {@see Throwable} and {@see Config::raiseOnError} is true.
*
* @since 4.0.0
*/
public function log($level, $toLog, array $context = array())
public function report($level, string|Stringable $message, array $context = array()): Response
{
if ($this->disabled()) {
$this->verboseLogger()->notice('Rollbar is disabled');
return new Response(0, "Disabled");
}

// Convert a Level proper into a string proper, as the code paths that
// follow have allowed both only by virtue that a Level downcasts to a
// string. With strict types, that no longer happens. We should consider
Expand All @@ -104,23 +135,23 @@ public function log($level, $toLog, array $context = array())
if ($level instanceof Level) {
$level = (string)$level;
} elseif (!$this->levelFactory->isValidLevel($level)) {
$exception = new \Psr\Log\InvalidArgumentException("Invalid log level '$level'.");
$exception = new InvalidArgumentException("Invalid log level '$level'.");
$this->verboseLogger()->error($exception->getMessage());
throw $exception;
}

$this->verboseLogger()->info("Attempting to log: [$level] " . $toLog);
$this->verboseLogger()->info("Attempting to log: [$level] " . $message);

if ($this->config->internalCheckIgnored($level, $toLog)) {
if ($this->config->internalCheckIgnored($level, $message)) {
$this->verboseLogger()->info('Occurrence ignored');
return new Response(0, "Ignored");
}

$accessToken = $this->getAccessToken();
$payload = $this->getPayload($accessToken, $level, $toLog, $context);
$isUncaught = $this->isUncaughtLogData($toLog);
if ($this->config->checkIgnored($payload, $accessToken, $toLog, $isUncaught)) {
$payload = $this->getPayload($accessToken, $level, $message, $context);

$isUncaught = $this->isUncaughtLogData($message);
if ($this->config->checkIgnored($payload, $accessToken, $message, $isUncaught)) {
$this->verboseLogger()->info('Occurrence ignored');
$response = new Response(0, "Ignored");
} else {
Expand All @@ -131,10 +162,10 @@ public function log($level, $toLog, array $context = array())
$encoded = $this->encode($scrubbed);

$truncated = $this->truncate($encoded);

$response = $this->send($truncated, $accessToken);
}

$this->handleResponse($payload, $response);

if ($response->getStatus() === 0) {
Expand All @@ -147,11 +178,11 @@ public function log($level, $toLog, array $context = array())
} else {
$this->verboseLogger()->info('Occurrence successfully logged');
}
if ($toLog instanceof Throwable && $this->config->getRaiseOnError()) {
throw $toLog;

if ($message instanceof Throwable && $this->config->getRaiseOnError()) {
throw $message;
}

return $response;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/CurlSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function testCurlError(): void
"environment" => "testing-php",
"endpoint" => "fake-endpoint"
));
$response = $logger->log(Level::WARNING, "Testing PHP Notifier", array());
$response = $logger->report(Level::WARNING, "Testing PHP Notifier", array());

$this->assertContains(
$response->getInfo(),
Expand Down
10 changes: 5 additions & 5 deletions tests/ReadmeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testQuickStart(): void
);

// If you want to check if logging with Rollbar was successful
$response = Rollbar::log(Level::INFO, 'testing wasSuccessful()');
$response = Rollbar::report((new LevelFactory)->fromName(Level::INFO), 'testing wasSuccessful()');
if (!$response->wasSuccessful()) {
throw new \Exception('logging with Rollbar failed');
}
Expand Down Expand Up @@ -98,9 +98,9 @@ public function testBasicUsage(): void
try {
do_something();
} catch (\Exception $e) {
$result1 = Rollbar::log(Level::ERROR, $e);
$result1 = Rollbar::report(Level::ERROR, $e);
// or
$result2 = Rollbar::log(Level::ERROR, $e, array("my" => "extra", "data" => 42));
$result2 = Rollbar::report(Level::ERROR, $e, array("my" => "extra", "data" => 42));
}

$this->assertEquals(200, $result1->getStatus());
Expand All @@ -116,8 +116,8 @@ public function testBasicUsage2(): void
)
);

$result1 = Rollbar::log(Level::WARNING, 'could not connect to mysql server');
$result2 = Rollbar::log(
$result1 = Rollbar::report(Level::WARNING, 'could not connect to mysql server');
$result2 = Rollbar::report(
Level::INFO,
'Here is a message with some additional data',
array('x' => 10, 'code' => 'blue')
Expand Down
Loading