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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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());
|
||||
|
Reference in New Issue
Block a user