Skip to content

Commit

Permalink
Merge pull request #543 from InSitu-Software/fix-transformer-master
Browse files Browse the repository at this point in the history
Add transformer option
  • Loading branch information
bxsx authored Nov 2, 2021
2 parents 666a1a2 + 677c571 commit ac3a9c2
Showing 1 changed file with 52 additions and 51 deletions.
103 changes: 52 additions & 51 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ class Config
'minimum_level',
'verbose',
'verbose_logger',
'raise_on_error'
'raise_on_error',
'transformer',
);

private string $accessToken;

/**
Expand Down Expand Up @@ -145,12 +146,12 @@ class Config
*/
private $dataBuilder;
private $configArray;

/**
* @var LevelFactory
*/
private $levelFactory;

/**
* @var TransformerInterface
*/
Expand All @@ -159,12 +160,12 @@ class Config
* @var FilterInterface
*/
private $filter;

/**
* @var int
*/
private int $minimumLevel;

/**
* @var ResponseHandlerInterface
*/
Expand All @@ -185,13 +186,13 @@ class Config
private int $maxNestingDepth = 10;

private array $custom = array();

/**
* @var callable with parameters $toLog, $contextDataMethodContext. The return
* value of the callable will be appended to the custom field of the item.
*/
private $customDataMethod;

/**
* @var callable
*/
Expand All @@ -202,27 +203,27 @@ class Config

private $includedErrno;
private bool $useErrorReporting = false;

/**
* @var boolean Should debug_backtrace() data be sent with string messages
* sent through RollbarLogger::log().
*/
private bool $sendMessageTrace = false;

/**
* @var string (fully qualified class name) The name of the your custom
* truncation strategy class. The class should inherit from
* Rollbar\Truncation\AbstractStrategy.
*/
private $customTruncation;

/**
* @var boolean Should the SDK raise an exception after logging an error.
* This is useful in test and development enviroments.
* https://github.com/rollbar/rollbar-php/issues/448
*/
private bool $raiseOnError = false;

/**
* @var int The maximum number of items reported to Rollbar within one
* request.
Expand All @@ -232,16 +233,16 @@ class Config
public function __construct(array $configArray)
{
$this->includedErrno = Defaults::get()->includedErrno();

$this->levelFactory = new LevelFactory();

$this->updateConfig($configArray);

$this->errorSampleRates = Defaults::get()->errorSampleRates();
if (isset($configArray['error_sample_rates'])) {
$this->errorSampleRates = $configArray['error_sample_rates'];
}

$this->exceptionSampleRates = Defaults::get()->exceptionSampleRates();
if (isset($configArray['exception_sample_rates'])) {
$this->exceptionSampleRates = $configArray['exception_sample_rates'];
Expand All @@ -262,7 +263,7 @@ public function __construct(array $configArray)
}
$this->mtRandmax = mt_getrandmax();
}

public static function listOptions(): array
{
return self::$options;
Expand Down Expand Up @@ -318,16 +319,16 @@ protected function updateConfig(array $config): void
if (isset($config['use_error_reporting'])) {
$this->useErrorReporting = $config['use_error_reporting'];
}

$this->maxItems = Defaults::get()->maxItems();
if (isset($config['max_items'])) {
$this->maxItems = $config['max_items'];
}

if (isset($config['custom_truncation'])) {
$this->customTruncation = $config['custom_truncation'];
}

$this->customDataMethod = Defaults::get()->customDataMethod();
if (isset($config['custom_data_method'])) {
$this->customDataMethod = $config['custom_data_method'];
Expand Down Expand Up @@ -370,7 +371,7 @@ private function setLogPayloadLogger(array $config): void
{
$this->logPayloadLogger = $config['log_payload_logger'] ??
new \Monolog\Logger('rollbar.payload', array(new \Monolog\Handler\ErrorLogHandler()));

if (!($this->logPayloadLogger instanceof \Psr\Log\LoggerInterface)) {
throw new \Exception('Log Payload Logger must implement \Psr\Log\LoggerInterface');
}
Expand All @@ -390,17 +391,17 @@ private function setVerboseLogger(array $config): void
$handler->setLevel($this->verboseInteger());
$this->verboseLogger = new \Monolog\Logger('rollbar.verbose', array($handler));
}

if (!($this->verboseLogger instanceof \Psr\Log\LoggerInterface)) {
throw new \Exception('Verbose logger must implement \Psr\Log\LoggerInterface');
}
}

public function enable(): void
{
$this->enabled = true;
}

public function disable(): void
{
$this->enabled = false;
Expand All @@ -411,11 +412,11 @@ private function setDataBuilder(array $config): void
if (!isset($config['levelFactory'])) {
$config['levelFactory'] = $this->levelFactory;
}

if (!isset($config['utilities'])) {
$config['utilities'] = $this->utilities();
}

$exp = DataBuilderInterface::class;
$def = DataBuilder::class;
$this->setupWithOptions($config, "dataBuilder", $exp, $def, true);
Expand All @@ -430,10 +431,10 @@ private function setTransformer(array $config): void
private function setMinimumLevel(array $config): void
{
$this->minimumLevel = \Rollbar\Defaults::get()->minimumLevel();

$override = array_key_exists('minimum_level', $config) ? $config['minimum_level'] : null;
$override = array_key_exists('minimumLevel', $config) ? $config['minimumLevel'] : $override;

if ($override instanceof Level) {
$this->minimumLevel = $override->toInt();
} elseif (is_string($override)) {
Expand Down Expand Up @@ -489,7 +490,7 @@ private function setBatched(array $config): void
$this->batched = $config['batched'];
}
}

private function setRaiseOnError(array $config): void
{
if (array_key_exists('raise_on_error', $config)) {
Expand Down Expand Up @@ -517,12 +518,12 @@ public function setCustom(array $config): void
{
$this->dataBuilder->setCustom($config);
}

public function addCustom($key, $data)
{
$this->dataBuilder->addCustom($key, $data);
}

public function removeCustom($key)
{
$this->dataBuilder->removeCustom($key);
Expand Down Expand Up @@ -550,22 +551,22 @@ public function verboseInteger(): int
}
return \Monolog\Logger::toMonologLevel($this->verbose);
}

public function getCustom()
{
return $this->dataBuilder->getCustom();
}

public function getAllowedCircularReferenceTypes()
{
return $this->allowedCircularReferenceTypes;
}

public function setCustomTruncation($type)
{
$this->customTruncation = $type;
}

public function getCustomTruncation()
{
return $this->customTruncation;
Expand Down Expand Up @@ -641,7 +642,7 @@ private function setCheckIgnoreFunction(array $config): void
if (isset($config['checkIgnore'])) {
$this->checkIgnore = $config['checkIgnore'];
}

if (isset($config['check_ignore'])) {
$this->checkIgnore = $config['check_ignore'];
}
Expand Down Expand Up @@ -736,12 +737,12 @@ public function getDataBuilder()
{
return $this->dataBuilder;
}

public function getLevelFactory()
{
return $this->levelFactory;
}

public function getSender()
{
return $this->sender;
Expand All @@ -766,7 +767,7 @@ public function getMaxNestingDepth(): int
{
return $this->maxNestingDepth;
}

public function getMaxItems(): int
{
return $this->maxItems;
Expand All @@ -776,7 +777,7 @@ public function getMinimumLevel(): int
{
return $this->minimumLevel;
}

public function getRaiseOnError(): bool
{
return $this->raiseOnError;
Expand Down Expand Up @@ -814,7 +815,7 @@ public function enabled(): bool
{
return $this->enabled === true;
}

public function disabled(): bool
{
return !$this->enabled();
Expand All @@ -841,7 +842,7 @@ public function checkIgnored($payload, string $accessToken, $toLog, bool $isUnca
$this->checkIgnore = null;
}
}

if ($this->payloadLevelTooLow($payload)) {
$this->verboseLogger()->debug("Occurrence's level is too low");
return true;
Expand Down Expand Up @@ -871,11 +872,11 @@ public function internalCheckIgnored(string $level, mixed $toLog): bool
if ($toLog instanceof ErrorWrapper) {
return $this->shouldIgnoreErrorWrapper($toLog);
}

if ($toLog instanceof \Exception) {
return $this->shouldIgnoreException($toLog);
}

return false;
}

Expand Down Expand Up @@ -911,7 +912,7 @@ public function shouldIgnoreError(int $errno): bool
return true;
}
}

return false;
}

Expand All @@ -927,7 +928,7 @@ protected function shouldIgnoreErrorWrapper(ErrorWrapper $toLog): bool
{
return $this->shouldIgnoreError($toLog->errorLevel);
}

/**
* Check if the exception should be ignored due to configured exception
* sample rates.
Expand All @@ -946,10 +947,10 @@ public function shouldIgnoreException(\Exception $toLog): bool
$this->verboseLogger()->debug("Skip exception due to exception sample rating");
return true;
}

return false;
}

/**
* Calculate what's the chance of logging this exception according to
* exception sampling.
Expand All @@ -964,22 +965,22 @@ public function exceptionSampleRate(\Exception $toLog): float
if (count($this->exceptionSampleRates) == 0) {
return $sampleRate;
}

$exceptionClasses = array();

$class = get_class($toLog);
while ($class) {
$exceptionClasses []= $class;
$class = get_parent_class($class);
}
$exceptionClasses = array_reverse($exceptionClasses);

foreach ($exceptionClasses as $exceptionClass) {
if (isset($this->exceptionSampleRates["$exceptionClass"])) {
$sampleRate = $this->exceptionSampleRates["$exceptionClass"];
}
}

return $sampleRate;
}

Expand Down

0 comments on commit ac3a9c2

Please sign in to comment.