|
@@ -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 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();
|
|
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)
|
|
|
|
|
- && mt_rand(1, $this->factor) === 1)
|
|
|
|
|
- {
|
|
|
|
|
- return $this->delegate->handle($record);
|
|
|
|
|
- }
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ 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");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- public function pushProcessor($callback)
|
|
|
|
|
- {
|
|
|
|
|
- $this->delegate->pushProcessor($callback);
|
|
|
|
|
- return $this;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if ($this->processors) {
|
|
|
|
|
+ foreach ($this->processors as $processor) {
|
|
|
|
|
+ $record = call_user_func($processor, $record);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- public function popProcessor()
|
|
|
|
|
- {
|
|
|
|
|
- return $this->delegate->popProcessor();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public function setFormatter(FormatterInterface $formatter)
|
|
|
|
|
- {
|
|
|
|
|
- $this->delegate->setFormatter($formatter);
|
|
|
|
|
- return $this;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ $this->handler->handle($record);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- public function getFormatter()
|
|
|
|
|
- {
|
|
|
|
|
- return $this->delegate->getFormatter();
|
|
|
|
|
|
|
+ return false === $this->bubble;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|