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

Amended PR: Add to config minimumLevel option #442

Merged
merged 8 commits into from
Mar 24, 2019
33 changes: 23 additions & 10 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ class Config
'include_raw_request_body',
'local_vars_dump',
'max_nesting_depth',
'max_items'
'max_items',
'minimum_level'
);

private $accessToken;
Expand Down Expand Up @@ -88,7 +89,12 @@ class Config
* @var FilterInterface
*/
private $filter;

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

/**
* @var ResponseHandlerInterface
*/
Expand Down Expand Up @@ -301,18 +307,20 @@ private function setTransformer($config)

private function setMinimumLevel($config)
{
$this->minimumLevel = 0;
if (empty($config['minimumLevel'])) {
$this->minimumLevel = 0;
} elseif ($config['minimumLevel'] instanceof Level) {
$this->minimumLevel = $config['minimumLevel']->toInt();
} elseif (is_string($config['minimumLevel'])) {
$level = $this->levelFactory->fromName($config['minimumLevel']);
$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)) {
$level = $this->levelFactory->fromName($override);
if ($level !== null) {
$this->minimumLevel = $level->toInt();
}
} elseif (is_int($config['minimumLevel'])) {
$this->minimumLevel = $config['minimumLevel'];
} elseif (is_int($override)) {
$this->minimumLevel = $override;
}
}

Expand Down Expand Up @@ -600,6 +608,11 @@ public function getMaxItems()
return $this->maxItems;
}

public function getMinimumLevel()
{
return $this->minimumLevel;
}

/**
* @param Payload $payload
* @param Level $level
Expand Down
8 changes: 7 additions & 1 deletion src/Defaults.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace Rollbar;

use Rollbar\Utilities;
use Monolog\Logger;
use Rollbar\Payload\Notifier;
use Psr\Log\LogLevel;

Expand Down Expand Up @@ -313,6 +313,11 @@ public function maxItems($value = null)
return $value !== null ? $value : $this->maxItems;
}

public function minimumLevel($value = null)
{
return $value !== null ? $value : $this->minimumLevel;
}

private $psrLevels;
private $errorLevels;
private $autodetectBranch = false;
Expand Down Expand Up @@ -358,4 +363,5 @@ public function maxItems($value = null)
private $scrubFields;
private $customTruncation = null;
private $maxItems = 10;
private $minimumLevel = 0;
}
64 changes: 33 additions & 31 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,43 +152,45 @@ public function testTransformer()

public function testMinimumLevel()
{
$c = new Config(array(
"access_token" => $this->getTestAccessToken(),
"environment" => $this->env,
"minimumLevel" => "warning"
$config = new Config(array(
'access_token' => $this->getTestAccessToken(),
'environment' => $this->env
));

$this->assertPayloadNotIgnored($config, $this->prepareMockPayload(Level::DEBUG()));

$config->configure(array('minimum_level' => Level::WARNING()));

$this->assertPayloadIgnored($config, $this->prepareMockPayload(Level::DEBUG()));
$this->assertPayloadNotIgnored($config, $this->prepareMockPayload(Level::WARNING()));

$config = new Config(array(
'access_token' => $this->getTestAccessToken(),
'environment' => $this->env,
'minimumLevel' => Level::ERROR()
));
$this->runConfigTest($c);

$c->configure(array("minimumLevel" => Level::WARNING));
$this->runConfigTest($c);

$c->configure(array("minimumLevel" => Level::WARNING()->toInt()));
$this->runConfigTest($c);
$this->assertPayloadIgnored($config, $this->prepareMockPayload(Level::WARNING()));
$this->assertPayloadNotIgnored($config, $this->prepareMockPayload(Level::ERROR()));
}

private function runConfigTest($config)
public function assertPayloadIgnored($config, $payload)
{
$accessToken = $config->getAccessToken();
$debugData = m::mock("Rollbar\Payload\Data")
->shouldReceive('getLevel')
->andReturn(Level::DEBUG())
->mock();
$debug = new Payload($debugData, $accessToken);
$this->assertTrue($config->checkIgnored($debug, null, $this->error, false));

$criticalData = m::mock("Rollbar\Payload\Data")
->shouldReceive('getLevel')
->andReturn(Level::CRITICAL())
->mock();
$critical = new Payload($criticalData, $accessToken);
$this->assertFalse($config->checkIgnored($critical, null, $this->error, false));

$warningData = m::mock("Rollbar\Payload\Data")
$this->assertTrue($config->checkIgnored($payload, null, $this->error, false));
}

public function assertPayloadNotIgnored($config, $payload)
{
$this->assertFalse($config->checkIgnored($payload, null, $this->error, false));
}

private function prepareMockPayload($level)
{
$data = m::mock("Rollbar\Payload\Data")
->shouldReceive('getLevel')
->andReturn(Level::warning())
->andReturn($level)
->mock();
$warning = new Payload($warningData, $accessToken);
$this->assertFalse($config->checkIgnored($warning, null, $this->error, false));
return new Payload($data, $this->getTestAccessToken());
}

public function testReportSuppressed()
Expand Down