Skip to content

Commit

Permalink
feat(dev options): add verbose and verbose_logger
Browse files Browse the repository at this point in the history
Closes re #453
  • Loading branch information
ArturMoczulski committed Apr 28, 2019
1 parent 46730f3 commit 92782a3
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 18 deletions.
64 changes: 61 additions & 3 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class Config
{
const VERBOSE_NONE = 'none';

private static $options = array(
'access_token',
'agent_log_location',
Expand Down Expand Up @@ -59,7 +61,9 @@ class Config
'local_vars_dump',
'max_nesting_depth',
'max_items',
'minimum_level'
'minimum_level',
'verbose',
'verbose_logger'
);

private $accessToken;
Expand All @@ -86,13 +90,37 @@ class Config
private $output;

/**
* @var Monolog\Logger $outputLogger Logger responsible for logging request
* @var \Psr\Log\Logger $outputLogger Logger responsible for logging request
* payload and response dumps on. The messages logged can be controlled with
* `output` config options.
* Default: \Monolog\Logger with \Monolog\Handler\ErrorLogHandler
*/
private $outputLogger;

/**
* @var string $verbose If this is set to any of the \Psr\Log\LogLevel options
* then we output messages related to the processing of items that might be
* useful to someone trying to understand what Rollbar is doing. The logged
* messages are dependent on the level of verbosity. The supported options are
* all the log levels of \Psr\Log\LogLevel
* (https://github.com/php-fig/log/blob/master/Psr/Log/LogLevel.php) plus
* an additional Rollbar\Config::VERBOSE_NONE option which makes the SDK quiet
* (excluding `output` option configured separetely).
* Essentially this option controls the level of verbosity of the default
* `verbose_logger`. If you override the default `verbose_logger`, you need
* to implement obeying the `verbose` config option yourself.
* Default: Rollbar\Config::VERBOSE_NONE
*/
private $verbose;

/**
* @var \Psr\Log\Logger $versbosity_logger The logger object used to log
* the internal messages of the SDK. The verbosity level of the default
* $verbosityLogger can be controlled with `verbose` config option.
* Default: \Rollbar\VerboseLogger
*/
private $verboseLogger;

/**
* @var DataBuilder
*/
Expand Down Expand Up @@ -243,6 +271,8 @@ protected function updateConfig($config)
$this->setTransmit($config);
$this->setOutput($config);
$this->setOutputLogger($config);
$this->setVerbose($config);
$this->setVerboseLogger($config);
$this->setAccessToken($config);
$this->setDataBuilder($config);
$this->setTransformer($config);
Expand Down Expand Up @@ -323,12 +353,30 @@ private function setOutputLogger($config)
{
$this->outputLogger = isset($config['output_logger']) ?
$config['output_logger'] :
\Rollbar\Defaults::get()->outputLogger();
new \Monolog\Logger('rollbar.output', array(new \Monolog\Handler\ErrorLogHandler()));

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

private function setVerbose($config)
{
$this->verbose = isset($config['verbose']) ?
$config['verbose'] :
\Rollbar\Defaults::get()->verbose();
}

private function setVerboseLogger($config)
{
$this->verboseLogger = isset($config['verbose_logger']) ?
$config['verbose_logger'] :
new VerboseLogger('rollbar.verbose', $this);

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

public function enable()
{
Expand Down Expand Up @@ -462,6 +510,11 @@ public function outputting()
{
return $this->output;
}

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

public function getCustom()
{
Expand Down Expand Up @@ -634,6 +687,11 @@ public function outputLogger()
return $this->outputLogger;
}

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

public function getRollbarData($level, $toLog, $context)
{
return $this->dataBuilder->makeData($level, $toLog, $context);
Expand Down
7 changes: 3 additions & 4 deletions src/Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,9 @@ public function output($value = null)
return $value !== null ? $value : $this->output;
}

public function outputLogger($value = null)
public function verbose($value = null)
{
$logger = new \Monolog\Logger('rollbar.output');
$logger->pushHandler(new \Monolog\Handler\ErrorLogHandler());
return $logger;
return $value !== null ? $value : $this->verbose;
}

public function environment($value = null)
Expand Down Expand Up @@ -367,6 +365,7 @@ public function minimumLevel($value = null)
private $enabled = true;
private $transmit = true;
private $output = false;
private $verbose = \Rollbar\Config::VERBOSE_NONE;
private $environment = 'production';
private $fluentHost = '127.0.0.1';
private $fluentPort = 24224;
Expand Down
45 changes: 45 additions & 0 deletions src/VerboseLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* \Rollbar\VerboseLogger is a class used as the internal verbosity logger
* for the SDK. A custom class was needed to support level
* \Rollbar\Config::VERBOSE_NONE in the `verbose` config option which
* makes the logger completely quiet.
*
* Using this logger, if the SDK is configured with
* `verbose` == \Rollbar\Config::VERBOSE_NONE any logging calls like
* $verboseLogger->info("Message") will be ignored.
*
* @package \Rollbar
* @author Artur Moczulski <[email protected]>
* @author Rollbar, Inc.
*/
namespace Rollbar;

class VerboseLogger extends \Monolog\Logger
{
private $config;

public function __construct($name, $rollbarConfig, array $handlers = array(), array $processors = array())
{
$this->config = $rollbarConfig;
parent::__construct($name, $handlers, $processors);
}

/**
* Adds a log record at an arbitrary level.
*
* @param mixed $level The log level. Supported log level are
* \Psr\Log\LogLevel plus \Rollbar\Config::VERBOSE_NONE
* @param string $message The log message
* @param array $context The log context
* @return bool Whether the record has been processed
*/
public function addRecord($level, $message, array $context = array())
{
if ($this->config->verbose() == Config::VERBOSE_NONE) {
return false;
}

return parent::addRecord($level, $message, $context);
}
}
35 changes: 34 additions & 1 deletion tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ public function testConfigureOutputLogger()
'access_token' => $this->getTestAccessToken(),
'environment' => $this->env
));
$this->assertInstanceOf('\Monolog\Logger', $config->outputLogger());
$outputLogger = $config->outputLogger();
$this->assertInstanceOf('\Monolog\Logger', $outputLogger);
$handlers = $outputLogger->getHandlers();
$this->assertInstanceOf('\Monolog\Handler\ErrorLogHandler', $handlers[0]);

$config = new Config(array(
'access_token' => $this->getTestAccessToken(),
Expand All @@ -113,6 +116,36 @@ public function testConfigureOutputLogger()
$this->assertInstanceOf('\Psr\Log\NullLogger', $config->outputLogger());
}

public function testVerbose()
{
$config = new Config(array(
'access_token' => $this->getTestAccessToken(),
'environment' => $this->env
));
$this->assertEquals(Config::VERBOSE_NONE, $config->verbose());
$this->assertInstanceOf('\Psr\Log\NullLogger', $config->verboseLogger());

$config->configure(array('verbose' => \Psr\Log\LogLevel::INFO));
$this->assertEquals(\Psr\Log\LogLevel::INFO, $config->verbose());
$this->assertInstanceOf('\Monolog\Logger', $config->verboseLogger());
}

public function testConfigureVerboseLogger()
{
$config = new Config(array(
'access_token' => $this->getTestAccessToken(),
'environment' => $this->env
));
$this->assertInstanceOf('\Rollbar\VerboseLogger', $config->verboseLogger());

$config = new Config(array(
'access_token' => $this->getTestAccessToken(),
'environment' => $this->env,
'verbose_logger' => new \Psr\Log\NullLogger()
));
$this->assertInstanceOf('\Psr\Log\NullLogger', $config->verboseLogger());
}

public function testAccessTokenFromEnvironment()
{
$_ENV['ROLLBAR_ACCESS_TOKEN'] = $this->getTestAccessToken();
Expand Down
10 changes: 0 additions & 10 deletions tests/DefaultsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,6 @@ public function testOutput()
{
$this->assertFalse($this->defaults->output());
}

public function testOutputLogger()
{
$logger = $this->defaults->outputLogger();
$this->assertInstanceOf('Psr\Log\LoggerInterface', $logger);
$this->assertEquals('rollbar.output', $logger->getName());
$handlers = $logger->getHandlers();
$this->assertCount(1, $handlers);
$this->assertInstanceOf('Monolog\Handler\ErrorLogHandler', $handlers[0]);
}

public function testEnvironment()
{
Expand Down
25 changes: 25 additions & 0 deletions tests/VerboseLoggerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php namespace Rollbar;

use \Mockery as m;

class VerboseLoggerTest extends BaseRollbarTest
{
public function testLog()
{
// verbose == VERBOSE_NONE
$config = new Config(array(
'access_token' => $this->getTestAccessToken(),
'environment' => "test",
'verbose' => Config::VERBOSE_NONE
));
$handler = m::mock('\Monolog\Handler\AbstractHandler')->makePartial();
$handler->shouldNotReceive('handle');
$subject = new VerboseLogger('verbose', $config, array($handler));
$this->assertFalse($subject->info("test log"));

// verbose == INFO
$handler->shouldReceive('handle');
$config->configure(array('verbose' => \Psr\Log\LogLevel::INFO));
$this->assertTrue($subject->info("test log"));
}
}

0 comments on commit 92782a3

Please sign in to comment.