← All changes
|
includes/sdk/google/monolog/monolog/src/Monolog/Handler/SamplingHandler.php
+29
-39
1.2.7
→
1.4.2
View file →
| @@ -8,11 +8,13 @@ | ||
| 8 | 8 | * |
| 9 | 9 | * For the full copyright and license information, please view the LICENSE |
| 10 | 10 | * file that was distributed with this source code. |
| 11 | 11 | */ |
| 12 | -namespace Dudlewebs\WPMCS\Monolog\Handler; | |
| 12 | +namespace Dudlewebs\WPMCS\GCP\Monolog\Handler; | |
| 13 | 13 | |
| 14 | -use Dudlewebs\WPMCS\Monolog\Formatter\FormatterInterface; | |
| 14 | +use Closure; | |
| 15 | +use Dudlewebs\WPMCS\GCP\Monolog\Formatter\FormatterInterface; | |
| 16 | +use Dudlewebs\WPMCS\GCP\Monolog\LogRecord; | |
| 15 | 17 | /** |
| 16 | 18 | * Sampling handler |
| 17 | 19 | * |
| 18 | 20 | * A sampled event stream can be useful for logging high frequency events in |
| @@ -24,48 +26,39 @@ | ||
| 24 | 26 | * tend to be close to this ratio with a large number of attempts. |
| 25 | 27 | * |
| 26 | 28 | * @author Bryan Davis <[email protected]> |
| 27 | 29 | * @author Kunal Mehta <[email protected]> |
| 28 | - * | |
| 29 | - * @phpstan-import-type Record from \Monolog\Logger | |
| 30 | - * @phpstan-import-type Level from \Monolog\Logger | |
| 31 | 30 | */ |
| 32 | 31 | class SamplingHandler extends AbstractHandler implements ProcessableHandlerInterface, FormattableHandlerInterface |
| 33 | 32 | { |
| 34 | 33 | use ProcessableHandlerTrait; |
| 35 | 34 | /** |
| 36 | - * @var HandlerInterface|callable | |
| 37 | - * @phpstan-var HandlerInterface|callable(Record|array{level: Level}|null, HandlerInterface): HandlerInterface | |
| 35 | + * Handler or factory Closure($record, $this) | |
| 36 | + * | |
| 37 | + * @phpstan-var (Closure(LogRecord|null, HandlerInterface): HandlerInterface)|HandlerInterface | |
| 38 | 38 | */ |
| 39 | - protected $handler; | |
| 39 | + protected Closure|HandlerInterface $handler; | |
| 40 | + protected int $factor; | |
| 40 | 41 | /** |
| 41 | - * @var int $factor | |
| 42 | - */ | |
| 43 | - protected $factor; | |
| 44 | - /** | |
| 45 | - * @psalm-param HandlerInterface|callable(Record|array{level: Level}|null, HandlerInterface): HandlerInterface $handler | |
| 42 | + * @phpstan-param (Closure(LogRecord|null, HandlerInterface): HandlerInterface)|HandlerInterface $handler | |
| 46 | 43 | * |
| 47 | - * @param callable|HandlerInterface $handler Handler or factory callable($record|null, $samplingHandler). | |
| 48 | - * @param int $factor Sample factor (e.g. 10 means every ~10th record is sampled) | |
| 44 | + * @param Closure|HandlerInterface $handler Handler or factory Closure($record|null, $samplingHandler). | |
| 45 | + * @param int $factor Sample factor (e.g. 10 means every ~10th record is sampled) | |
| 49 | 46 | */ |
| 50 | - public function __construct($handler, int $factor) | |
| 47 | + public function __construct(Closure|HandlerInterface $handler, int $factor) | |
| 51 | 48 | { |
| 52 | 49 | parent::__construct(); |
| 53 | 50 | $this->handler = $handler; |
| 54 | 51 | $this->factor = $factor; |
| 55 | - if (!$this->handler instanceof HandlerInterface && !is_callable($this->handler)) { | |
| 56 | - throw new \RuntimeException("The given handler (" . json_encode($this->handler) . ") is not a callable nor a Monolog\\Handler\\HandlerInterface object"); | |
| 57 | - } | |
| 58 | 52 | } |
| 59 | - public function isHandling(array $record): bool | |
| 53 | + public function isHandling(LogRecord $record) : bool | |
| 60 | 54 | { |
| 61 | 55 | return $this->getHandler($record)->isHandling($record); |
| 62 | 56 | } |
| 63 | - public function handle(array $record): bool | |
| 57 | + public function handle(LogRecord $record) : bool | |
| 64 | 58 | { |
| 65 | - if ($this->isHandling($record) && mt_rand(1, $this->factor) === 1) { | |
| 66 | - if ($this->processors) { | |
| 67 | - /** @var Record $record */ | |
| 59 | + if ($this->isHandling($record) && \mt_rand(1, $this->factor) === 1) { | |
| 60 | + if (\count($this->processors) > 0) { | |
| 68 | 61 | $record = $this->processRecord($record); |
| 69 | 62 | } |
| 70 | 63 | $this->getHandler($record)->handle($record); |
| 71 | 64 | } |
| @@ -73,28 +66,25 @@ | ||
| 73 | 66 | } |
| 74 | 67 | /** |
| 75 | 68 | * Return the nested handler |
| 76 | 69 | * |
| 77 | - * If the handler was provided as a factory callable, this will trigger the handler's instantiation. | |
| 78 | - * | |
| 79 | - * @phpstan-param Record|array{level: Level}|null $record | |
| 80 | - * | |
| 81 | - * @return HandlerInterface | |
| 70 | + * If the handler was provided as a factory, this will trigger the handler's instantiation. | |
| 82 | 71 | */ |
| 83 | - public function getHandler(?array $record = null) | |
| 72 | + public function getHandler(LogRecord|null $record = null) : HandlerInterface | |
| 84 | 73 | { |
| 85 | 74 | if (!$this->handler instanceof HandlerInterface) { |
| 86 | - $this->handler = ($this->handler)($record, $this); | |
| 87 | - if (!$this->handler instanceof HandlerInterface) { | |
| 88 | - throw new \RuntimeException("The factory callable should return a HandlerInterface"); | |
| 75 | + $handler = ($this->handler)($record, $this); | |
| 76 | + if (!$handler instanceof HandlerInterface) { | |
| 77 | + throw new \RuntimeException("The factory Closure should return a HandlerInterface"); | |
| 89 | 78 | } |
| 79 | + $this->handler = $handler; | |
| 90 | 80 | } |
| 91 | 81 | return $this->handler; |
| 92 | 82 | } |
| 93 | 83 | /** |
| 94 | - * {@inheritDoc} | |
| 84 | + * @inheritDoc | |
| 95 | 85 | */ |
| 96 | - public function setFormatter(FormatterInterface $formatter): HandlerInterface | |
| 86 | + public function setFormatter(FormatterInterface $formatter) : HandlerInterface | |
| 97 | 87 | { |
| 98 | 88 | $handler = $this->getHandler(); |
| 99 | 89 | if ($handler instanceof FormattableHandlerInterface) { |
| 100 | 90 | $handler->setFormatter($formatter); |
| @@ -99,18 +89,18 @@ | ||
| 99 | 89 | if ($handler instanceof FormattableHandlerInterface) { |
| 100 | 90 | $handler->setFormatter($formatter); |
| 101 | 91 | return $this; |
| 102 | 92 | } |
| 103 | - throw new \UnexpectedValueException('The nested handler of type ' . get_class($handler) . ' does not support formatters.'); | |
| 93 | + throw new \UnexpectedValueException('The nested handler of type ' . \get_class($handler) . ' does not support formatters.'); | |
| 104 | 94 | } |
| 105 | 95 | /** |
| 106 | - * {@inheritDoc} | |
| 96 | + * @inheritDoc | |
| 107 | 97 | */ |
| 108 | - public function getFormatter(): FormatterInterface | |
| 98 | + public function getFormatter() : FormatterInterface | |
| 109 | 99 | { |
| 110 | 100 | $handler = $this->getHandler(); |
| 111 | 101 | if ($handler instanceof FormattableHandlerInterface) { |
| 112 | 102 | return $handler->getFormatter(); |
| 113 | 103 | } |
| 114 | - throw new \UnexpectedValueException('The nested handler of type ' . get_class($handler) . ' does not support formatters.'); | |
| 104 | + throw new \UnexpectedValueException('The nested handler of type ' . \get_class($handler) . ' does not support formatters.'); | |
| 115 | 105 | } |
| 116 | 106 | } |