* @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:----T4tSnYucw715zW4GibXtRt32zEAI4mZoE06JqnQiwy5HyOSXibj/FHWZVSWKn/yqePUjM+P5SiO38Xhp5Mdfpxzxc3jg75ZB2GyMbUCjJ4SwDaLnZKwWEITG1PqV26lstQI/eHm4wUpqjzqrVzJcN8wkWPtJ295UsfGV82ImUoX/rtouVnmWAFrAnTDEXA9nbiiO9Kj6ukSehZvUGN5EDKhq81x+OjJGjZeu2oEuxwIeeA3qQ7Kb8rQYpKFvSfe2Vu7lIOajSdTpBZMjQSRmD1XtwKdcqMXMhrGTxgLyYow3GGstR5WUXyo7OtB8oEnK3Zs+MWkXdOnMSjr7KqjJSSkFhsJMOanIt8Xt5PSmhOQsljIE0fDtJcO51bxGtxKSzErZy8Y2ChY07gujKoXqFCFgSU2GcPJM/BsbcjzU7fMFcvVQJC5NDPNOI5cMIB4GF09DfQJzibsdnf3kGT0mT/DLjQdWLIzzt03tc85myGEwuobJsLrjBa/fpf+DiGPutl71pDCIIge79L3IvEsiGo3fvqpp1Jc1l2DPHaJ1Oc+/jY6OL42h0DHix4MD4AqieRI3H47JFEWShkH0PPIN1St8TvFDmQl91SKKnhqG6nBvhEtEBGNm5YhKk8Pq18Z1atTQ+t3TDZbCJRshICp1IY1KCt213WAlXeiVN0wl+aU=----ATTACHMENT:----NjU0MTkzMzgxNTk5MTU3MyA5MDQzNDc2NjA5MDU3MjQwIDcwMjY5MDMxMDEyMzkwMDc=