mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-05 20:57:36 +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
|
class SamplingHandler extends AbstractHandler
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var HandlerInterface $delegate
|
* @var callable|HandlerInterface $handler
|
||||||
*/
|
*/
|
||||||
protected $delegate;
|
protected $handler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int $factor
|
* @var int $factor
|
||||||
@@ -40,50 +40,47 @@ class SamplingHandler extends AbstractHandler
|
|||||||
protected $factor;
|
protected $factor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param HandlerInterface $handler Wrapped handler
|
* @param callable|HandlerInterface $handler Handler or factory callable($record, $fingersCrossedHandler).
|
||||||
* @param int $factor Sample factor
|
* @param int $factor Sample factor
|
||||||
*/
|
*/
|
||||||
public function __construct(HandlerInterface $handler, $factor)
|
public function __construct($handler, $factor)
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
$this->delegate = $handler;
|
$this->handler = $handler;
|
||||||
$this->factor = $factor;
|
$this->factor = $factor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isHandling(array $record)
|
public function isHandling(array $record)
|
||||||
{
|
{
|
||||||
return $this->delegate->isHandling($record);
|
return $this->handler->isHandling($record);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handle(array $record)
|
public function handle(array $record)
|
||||||
{
|
{
|
||||||
if ($this->isHandling($record)
|
if ($this->isHandling($record) && mt_rand(1, $this->factor) === 1) {
|
||||||
&& mt_rand(1, $this->factor) === 1)
|
// The same logic as in FingersCrossedHandler
|
||||||
{
|
if (!$this->handler instanceof HandlerInterface) {
|
||||||
return $this->delegate->handle($record);
|
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)
|
return false === $this->bubble;
|
||||||
{
|
|
||||||
$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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -23,8 +23,7 @@ class SamplingHandlerTest extends TestCase
|
|||||||
{
|
{
|
||||||
$testHandler = new TestHandler();
|
$testHandler = new TestHandler();
|
||||||
$handler = new SamplingHandler($testHandler, 2);
|
$handler = new SamplingHandler($testHandler, 2);
|
||||||
for ($i=0; $i<10000; $i++)
|
for ($i = 0; $i < 10000; $i++) {
|
||||||
{
|
|
||||||
$handler->handle($this->getRecord());
|
$handler->handle($this->getRecord());
|
||||||
}
|
}
|
||||||
$count = count($testHandler->getRecords());
|
$count = count($testHandler->getRecords());
|
||||||
|
Reference in New Issue
Block a user