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

Updated the TransformerInterface #593

Merged
merged 2 commits into from
Jan 12, 2023
Merged
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
2 changes: 1 addition & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ public function transform(
Level|string $level,
mixed $toLog,
array $context = array ()
): ?Payload {
): Payload {
if (count($this->custom) > 0) {
$this->verboseLogger()->debug("Adding custom data to the payload.");
$data = $payload->getData();
Expand Down
4 changes: 2 additions & 2 deletions src/RollbarLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,14 @@ protected function send(EncodedPayload $payload, string $accessToken): Response
* @param string|Stringable $toLog The message or error to be sent to Rollbar.
* @param array $context Additional data to send with the message.
*
* @return Payload|null
* @return Payload
*/
protected function getPayload(
string $accessToken,
string $level,
string|Stringable $toLog,
array $context,
): ?Payload {
): Payload {
$data = $this->config->getRollbarData($level, $toLog, $context);
$payload = new Payload($data, $accessToken);
return $this->config->transform($payload, $level, $toLog, $context);
Expand Down
29 changes: 26 additions & 3 deletions src/TransformerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,37 @@

use Rollbar\Payload\Level;
use Rollbar\Payload\Payload;
use Throwable;

/**
* The scrubber interface allows changes to be made to the error or log {@see Payload} before it is checked to see if
* it will be ignored, is serialized, scrubbed of PII or many other processes. The transform method is executed
* directly after the payload is created.
*
* Note: transforms are only applied to payloads that were created through calling one of the {@see Rollbar} or
* {@see RollbarLogger} logging methods or for errors and exceptions that where caught by Rollbar.
*
* An optional constructor can be included in the implementing class. The constructor should accept a single array
* argument. The value of the argument will be the value of `transformerOptions` from the configs array, or an empty
* array if `transformerOptions` does not exist.
*/
interface TransformerInterface
{
/**
* The transform method allows changes to be made to a {@see Payload} just after it is created and before it is
* passed to anything else in the standard logging and error catching process.
*
* @param Payload $payload The payload that has just been created.
* @param Level|string $level The severity of the log message or error.
* @param mixed $toLog The error or message that is being logged.
* @param array $context Additional context data that may be sent with the message. In accordance with
* PSR-3 an exception may be in $context['exception'] not $toLog.
*
* @return Payload
*/
public function transform(
Payload $payload,
Level|string $level,
mixed $toLog,
array $context = array ()
): ?Payload;
array $context = array()
): Payload;
}
5 changes: 3 additions & 2 deletions tests/TestHelpers/MalformedPayloadDataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
use Rollbar\Payload\Level;
use Rollbar\Payload\Payload;
use Rollbar\Payload\Data;
use Rollbar\TransformerInterface;

class MalformedPayloadDataTransformer implements \Rollbar\TransformerInterface
class MalformedPayloadDataTransformer implements TransformerInterface
{
public function transform(
Payload $payload,
Level|string $level,
mixed $toLog,
array $context = array()
): ?Payload {
): Payload {
$mock = \Mockery::mock(Data::class)->makePartial();
$mock->shouldReceive("serialize")->andReturn(array());
$mock->setLevel(\Rollbar\LevelFactory::fromName($level));
Expand Down