2022-10-17 21:25:03 +02:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Monolog package.
|
|
|
|
*
|
|
|
|
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Monolog\Handler;
|
|
|
|
|
|
|
|
use Monolog\Formatter\FormatterInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sampling handler
|
|
|
|
*
|
|
|
|
* A sampled event stream can be useful for logging high frequency events in
|
|
|
|
* a production environment where you only need an idea of what is happening
|
|
|
|
* and are not concerned with capturing every occurrence. Since the decision to
|
|
|
|
* handle or not handle a particular event is determined randomly, the
|
|
|
|
* resulting sampled log is not guaranteed to contain 1/N of the events that
|
|
|
|
* occurred in the application, but based on the Law of large numbers, it will
|
|
|
|
* tend to be close to this ratio with a large number of attempts.
|
|
|
|
*
|
|
|
|
* @author Bryan Davis <bd808@wikimedia.org>
|
|
|
|
* @author Kunal Mehta <legoktm@gmail.com>
|
2023-07-02 23:57:24 +02:00
|
|
|
*
|
|
|
|
* @phpstan-import-type Record from \Monolog\Logger
|
|
|
|
* @phpstan-import-type Level from \Monolog\Logger
|
2022-10-17 21:25:03 +02:00
|
|
|
*/
|
|
|
|
class SamplingHandler extends AbstractHandler implements ProcessableHandlerInterface, FormattableHandlerInterface
|
|
|
|
{
|
|
|
|
use ProcessableHandlerTrait;
|
|
|
|
|
|
|
|
/**
|
2023-07-02 23:57:24 +02:00
|
|
|
* @var HandlerInterface|callable
|
|
|
|
* @phpstan-var HandlerInterface|callable(Record|array{level: Level}|null, HandlerInterface): HandlerInterface
|
2022-10-17 21:25:03 +02:00
|
|
|
*/
|
2023-07-02 23:57:24 +02:00
|
|
|
protected $handler;
|
2022-10-17 21:25:03 +02:00
|
|
|
|
2023-07-02 23:57:24 +02:00
|
|
|
/**
|
|
|
|
* @var int $factor
|
|
|
|
*/
|
|
|
|
protected $factor;
|
2022-10-17 21:25:03 +02:00
|
|
|
|
|
|
|
/**
|
2023-07-02 23:57:24 +02:00
|
|
|
* @psalm-param HandlerInterface|callable(Record|array{level: Level}|null, HandlerInterface): HandlerInterface $handler
|
2022-10-17 21:25:03 +02:00
|
|
|
*
|
2023-07-02 23:57:24 +02:00
|
|
|
* @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)
|
2022-10-17 21:25:03 +02:00
|
|
|
*/
|
2023-07-02 23:57:24 +02:00
|
|
|
public function __construct($handler, int $factor)
|
2022-10-17 21:25:03 +02:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
$this->handler = $handler;
|
|
|
|
$this->factor = $factor;
|
2023-07-02 23:57:24 +02:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
2022-10-17 21:25:03 +02:00
|
|
|
}
|
|
|
|
|
2023-07-02 23:57:24 +02:00
|
|
|
public function isHandling(array $record): bool
|
2022-10-17 21:25:03 +02:00
|
|
|
{
|
|
|
|
return $this->getHandler($record)->isHandling($record);
|
|
|
|
}
|
|
|
|
|
2023-07-02 23:57:24 +02:00
|
|
|
public function handle(array $record): bool
|
2022-10-17 21:25:03 +02:00
|
|
|
{
|
|
|
|
if ($this->isHandling($record) && mt_rand(1, $this->factor) === 1) {
|
2023-07-02 23:57:24 +02:00
|
|
|
if ($this->processors) {
|
|
|
|
/** @var Record $record */
|
2022-10-17 21:25:03 +02:00
|
|
|
$record = $this->processRecord($record);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->getHandler($record)->handle($record);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false === $this->bubble;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the nested handler
|
|
|
|
*
|
2023-07-02 23:57:24 +02:00
|
|
|
* 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
|
2022-10-17 21:25:03 +02:00
|
|
|
*/
|
2023-07-02 23:57:24 +02:00
|
|
|
public function getHandler(array $record = null)
|
2022-10-17 21:25:03 +02:00
|
|
|
{
|
|
|
|
if (!$this->handler instanceof HandlerInterface) {
|
2023-07-02 23:57:24 +02:00
|
|
|
$this->handler = ($this->handler)($record, $this);
|
|
|
|
if (!$this->handler instanceof HandlerInterface) {
|
|
|
|
throw new \RuntimeException("The factory callable should return a HandlerInterface");
|
2022-10-17 21:25:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-07-02 23:57:24 +02:00
|
|
|
* {@inheritDoc}
|
2022-10-17 21:25:03 +02:00
|
|
|
*/
|
|
|
|
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.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-07-02 23:57:24 +02:00
|
|
|
* {@inheritDoc}
|
2022-10-17 21:25:03 +02:00
|
|
|
*/
|
|
|
|
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.');
|
|
|
|
}
|
|
|
|
}
|