'MWLoggerMonologSpi', * 'args' => array( array( * 'handlers' => array( * 'some-handler' => array( ... ), * 'sampled-some-handler' => array( * 'class' => 'MWLoggerMonologSamplingHandler', * 'args' => array( * function() { * return MWLoggerFactory::getProvider()->getHandler( 'some-handler'); * }, * 2, // emit logs with a 1:2 chance * ), * ), * ), * ) ), * ); * @endcode * * 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 occurence. 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 [[Law of large numbers]] it will * tend to be close to this ratio with a large number of attempts. * * @since 1.25 * @author Bryan Davis * @copyright © 2014 Bryan Davis and Wikimedia Foundation. */ class MWLoggerMonologSamplingHandler implements HandlerInterface { /** * @var HandlerInterface $delegate */ protected $delegate; /** * @var int $factor */ protected $factor; /** * @param HandlerInterface $handler Wrapped handler * @param int $factor Sample factor */ public function __construct( HandlerInterface $handler, $factor ) { $this->delegate = $handler; $this->factor = $factor; } public function isHandling( array $record ) { return $this->delegate->isHandling( $record ); } public function handle( array $record ) { if ( $this->isHandling( $record ) && mt_rand( 1, $this->factor ) === 1 ) { return $this->delegate->handle( $record ); } return false; } public function handleBatch( array $records ) { foreach ( $records as $record ) { $this->handle( $record ); } } public function pushProcessor( $callback ) { $this->delegate->pushProcessor( $callback ); return $this; } public function popProcessor() { return $this->delegate->popProcessor(); } public function setFormatter( FormatterInterface $formatter ) { $this->delegate->setFormatter( $formatter ); return $this; } public function getFormatter() { return $this->delegate->getFormatter(); } }