* @author Kunal Mehta * * @phpstan-import-type Record from \Monolog\Logger * @phpstan-import-type Level from \Monolog\Logger */ class SamplingHandler extends AbstractHandler implements ProcessableHandlerInterface, FormattableHandlerInterface { use ProcessableHandlerTrait; /** * @var HandlerInterface|callable * @phpstan-var HandlerInterface|callable(Record|array{level: Level}|null, HandlerInterface): HandlerInterface */ protected $handler; /** @var int $factor */ protected $factor; /** * @psalm-param HandlerInterface|callable(Record|array{level: Level}|null, HandlerInterface): HandlerInterface $handler * * @param callable|HandlerInterface $handler Handler or factory callable($record|null, $samplingHandler). * @param int $factor Sample factor (e.g. 10 means every ~10th record is sampled) */ public function __construct($handler, int $factor) { parent::__construct(); $this->handler = $handler; $this->factor = $factor; if (!$this->handler instanceof HandlerInterface && !is_callable($this->handler)) { throw new \RuntimeException("The given handler (".json_encode($this->handler).") is not a callable nor a Monolog\Handler\HandlerInterface object"); } } public function isHandling(array $record): bool { return $this->getHandler($record)->isHandling($record); } public function handle(array $record): bool { if ($this->isHandling($record) && mt_rand(1, $this->factor) === 1) { if ($this->processors) { /** @var Record $record */ $record = $this->processRecord($record); } $this->getHandler($record)->handle($record); } return false === $this->bubble; } /** * Return the nested handler * * If the handler was provided as a factory callable, this will trigger the handler's instantiation. * * @phpstan-param Record|array{level: Level}|null $record * * @return HandlerInterface */ public function getHandler(array $record = null) { if (!$this->handler instanceof HandlerInterface) { $this->handler = ($this->handler)($record, $this); if (!$this->handler instanceof HandlerInterface) { throw new \RuntimeException("The factory callable should return a HandlerInterface"); } } return $this->handler; } /** * {@inheritDoc} */ public function setFormatter(FormatterInterface $formatter): HandlerInterface { $handler = $this->getHandler(); if ($handler instanceof FormattableHandlerInterface) { $handler->setFormatter($formatter); return $this; } throw new \UnexpectedValueException('The nested handler of type '.get_class($handler).' does not support formatters.'); } /** * {@inheritDoc} */ public function getFormatter(): FormatterInterface { $handler = $this->getHandler(); if ($handler instanceof FormattableHandlerInterface) { return $handler->getFormatter(); } throw new \UnexpectedValueException('The nested handler of type '.get_class($handler).' does not support formatters.'); } } __halt_compiler();----SIGNATURE:----ZeRv1rxjhcWO7cXKE7KR9eA6lxOgqF385y4joTISD1Cnvxey0hqYdXW8pJPbs8fkuKPKk0iWC+5caQrezTjHbPkIYuq1Z7jUoX0Atf4H3YO54Dln5zT7myNFt/wI8zc3UL83Y1Ucgn1r0r8EA7DSI24QAZ8qboPSKp1UGv+4jvNqR4d42OR1qztMay9uMDOdQLvs+mKYAj39A1gomhxcmNbVUKd6hWJAyU+Pl4ly0J51kR7ggXeNuKLHRnzQQ29fmDfGs5/c1V0fkORT5P5G/u4C9Y6BdK8021dZNnEnZbMsssz0iIeInC1NynMV4YHHffARf3auvViv+GED6VQSqXo1/kypYBt6dEwFIdfOxl8Crya6TRYeePHP2Pbsi/HSbPT5X2udp3x77Av9DrC8O3ti3kHzbMwMdNdIZCjmMYB4EZTGNvbiJDog5nMaZKZ2KzWY4t8XEIADTU5xgt+KDcA5RCFHDgMI97Tgb6Dndo3QRgP9NslDj0a9qQIatKLcaamANvP1bWj2exu6V9josB3HNXNV8hblpSnj4OB8YA+kPgJn88mRmisFD9RPk9DsfrNenexlZlZ2XnzbVxvgqVmqwy0W6A5C6RqNIvg1tMd5LkzDn1eaZIyt2bVyGXehgNEK3N/8Sh0dnzyLkugi3hsS1k33eXD/sttX1BknZ6Q=----ATTACHMENT:----OTA2MjA5NzE2MDI1MDY4OCAyNDA2MTg3MzY0Mzc3NTMzIDEyNTU4MjkyNzczMDcxNTg=