1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-05 04:37:38 +02:00

Bring sampling handler in line with other wrapper handlers

This commit is contained in:
Jordi Boggiano
2014-12-28 17:05:54 +00:00
parent 45f5c7d2d1
commit 8160b302a2
2 changed files with 31 additions and 35 deletions

View File

@@ -30,9 +30,9 @@ use Monolog\Formatter\FormatterInterface;
class SamplingHandler extends AbstractHandler
{
/**
* @var HandlerInterface $delegate
* @var callable|HandlerInterface $handler
*/
protected $delegate;
protected $handler;
/**
* @var int $factor
@@ -40,50 +40,47 @@ class SamplingHandler extends AbstractHandler
protected $factor;
/**
* @param HandlerInterface $handler Wrapped handler
* @param int $factor Sample factor
* @param callable|HandlerInterface $handler Handler or factory callable($record, $fingersCrossedHandler).
* @param int $factor Sample factor
*/
public function __construct(HandlerInterface $handler, $factor)
public function __construct($handler, $factor)
{
parent::__construct();
$this->delegate = $handler;
$this->handler = $handler;
$this->factor = $factor;
}
public function isHandling(array $record)
{
return $this->delegate->isHandling($record);
return $this->handler->isHandling($record);
}
public function handle(array $record)
{
if ($this->isHandling($record)
&& mt_rand(1, $this->factor) === 1)
{
return $this->delegate->handle($record);
if ($this->isHandling($record) && mt_rand(1, $this->factor) === 1) {
// The same logic as in FingersCrossedHandler
if (!$this->handler instanceof HandlerInterface) {
if (!is_callable($this->handler)) {
throw new \RuntimeException(
"The given handler (" . json_encode($this->handler)
. ") is not a callable nor a Monolog\\Handler\\HandlerInterface object"
);
}
$this->handler = call_user_func($this->handler, $record, $this);
if (!$this->handler instanceof HandlerInterface) {
throw new \RuntimeException("The factory callable should return a HandlerInterface");
}
}
if ($this->processors) {
foreach ($this->processors as $processor) {
$record = call_user_func($processor, $record);
}
}
$this->handler->handle($record);
}
return false;
}
public function pushProcessor($callback)
{
$this->delegate->pushProcessor($callback);
return $this;
}
public function popProcessor()
{
return $this->delegate->popProcessor();
}
public function setFormatter(FormatterInterface $formatter)
{
$this->delegate->setFormatter($formatter);
return $this;
}
public function getFormatter()
{
return $this->delegate->getFormatter();
return false === $this->bubble;
}
}

View File

@@ -23,8 +23,7 @@ class SamplingHandlerTest extends TestCase
{
$testHandler = new TestHandler();
$handler = new SamplingHandler($testHandler, 2);
for ($i=0; $i<10000; $i++)
{
for ($i = 0; $i < 10000; $i++) {
$handler->handle($this->getRecord());
}
$count = count($testHandler->getRecords());