Просмотр файла vendor/monolog/monolog/src/Monolog/Handler/SamplingHandler.php

Размер файла: 3.89Kb
  1. <?php declare(strict_types=1);
  2.  
  3. /*
  4. * This file is part of the Monolog package.
  5. *
  6. * (c) Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11.  
  12. namespace Monolog\Handler;
  13.  
  14. use Monolog\Formatter\FormatterInterface;
  15.  
  16. /**
  17. * Sampling handler
  18. *
  19. * A sampled event stream can be useful for logging high frequency events in
  20. * a production environment where you only need an idea of what is happening
  21. * and are not concerned with capturing every occurrence. Since the decision to
  22. * handle or not handle a particular event is determined randomly, the
  23. * resulting sampled log is not guaranteed to contain 1/N of the events that
  24. * occurred in the application, but based on the Law of large numbers, it will
  25. * tend to be close to this ratio with a large number of attempts.
  26. *
  27. * @author Bryan Davis <bd808@wikimedia.org>
  28. * @author Kunal Mehta <legoktm@gmail.com>
  29. */
  30. class SamplingHandler extends AbstractHandler implements ProcessableHandlerInterface, FormattableHandlerInterface
  31. {
  32. use ProcessableHandlerTrait;
  33.  
  34. /**
  35. * @var callable|HandlerInterface $handler
  36. */
  37. protected $handler;
  38.  
  39. /**
  40. * @var int $factor
  41. */
  42. protected $factor;
  43.  
  44. /**
  45. * @psalm-param HandlerInterface|callable(array, HandlerInterface): HandlerInterface $handler
  46. *
  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)
  49. */
  50. public function __construct($handler, int $factor)
  51. {
  52. parent::__construct();
  53. $this->handler = $handler;
  54. $this->factor = $factor;
  55.  
  56. if (!$this->handler instanceof HandlerInterface && !is_callable($this->handler)) {
  57. throw new \RuntimeException("The given handler (".json_encode($this->handler).") is not a callable nor a Monolog\Handler\HandlerInterface object");
  58. }
  59. }
  60.  
  61. public function isHandling(array $record): bool
  62. {
  63. return $this->getHandler($record)->isHandling($record);
  64. }
  65.  
  66. public function handle(array $record): bool
  67. {
  68. if ($this->isHandling($record) && mt_rand(1, $this->factor) === 1) {
  69. if ($this->processors) {
  70. $record = $this->processRecord($record);
  71. }
  72.  
  73. $this->getHandler($record)->handle($record);
  74. }
  75.  
  76. return false === $this->bubble;
  77. }
  78.  
  79. /**
  80. * Return the nested handler
  81. *
  82. * If the handler was provided as a factory callable, this will trigger the handler's instantiation.
  83. *
  84. * @return HandlerInterface
  85. */
  86. public function getHandler(array $record = null)
  87. {
  88. if (!$this->handler instanceof HandlerInterface) {
  89. $this->handler = ($this->handler)($record, $this);
  90. if (!$this->handler instanceof HandlerInterface) {
  91. throw new \RuntimeException("The factory callable should return a HandlerInterface");
  92. }
  93. }
  94.  
  95. return $this->handler;
  96. }
  97.  
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function setFormatter(FormatterInterface $formatter): HandlerInterface
  102. {
  103. $handler = $this->getHandler();
  104. if ($handler instanceof FormattableHandlerInterface) {
  105. $handler->setFormatter($formatter);
  106.  
  107. return $this;
  108. }
  109.  
  110. throw new \UnexpectedValueException('The nested handler of type '.get_class($handler).' does not support formatters.');
  111. }
  112.  
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function getFormatter(): FormatterInterface
  117. {
  118. $handler = $this->getHandler();
  119. if ($handler instanceof FormattableHandlerInterface) {
  120. return $handler->getFormatter();
  121. }
  122.  
  123. throw new \UnexpectedValueException('The nested handler of type '.get_class($handler).' does not support formatters.');
  124. }
  125. }