SpecialLinkSearch: clean up munged query variable handling
[mediawiki.git] / includes / debug / logger / monolog / SamplingHandler.php
bloba9f83b055fcf1907fd76948e8b25d2087d399f63
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 use Monolog\Handler\HandlerInterface;
22 use Monolog\Formatter\FormatterInterface;
24 /**
25 * Wrapper for another HandlerInterface that will only handle a percentage of
26 * records offered to it.
28 * When HandlerInterface::handle() is called for a given record, it will be
29 * handled or ignored with a one in N chance based on the sample factor given
30 * for the handler.
32 * Usage with MWLoggerMonologSpi:
33 * @code
34 * $wgMWLoggerDefaultSpi = array(
35 * 'class' => 'MWLoggerMonologSpi',
36 * 'args' => array( array(
37 * 'handlers' => array(
38 * 'some-handler' => array( ... ),
39 * 'sampled-some-handler' => array(
40 * 'class' => 'MWLoggerMonologSamplingHandler',
41 * 'args' => array(
42 * function() {
43 * return MWLoggerFactory::getProvider()->getHandler( 'some-handler');
44 * },
45 * 2, // emit logs with a 1:2 chance
46 * ),
47 * ),
48 * ),
49 * ) ),
50 * );
51 * @endcode
53 * A sampled event stream can be useful for logging high frequency events in
54 * a production environment where you only need an idea of what is happening
55 * and are not concerned with capturing every occurence. Since the decision to
56 * handle or not handle a particular event is determined randomly, the
57 * resulting sampled log is not guaranteed to contain 1/N of the events that
58 * occurred in the application but based on [[Law of large numbers]] it will
59 * tend to be close to this ratio with a large number of attempts.
61 * @since 1.25
62 * @author Bryan Davis <bd808@wikimedia.org>
63 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
65 class MWLoggerMonologSamplingHandler implements HandlerInterface {
67 /**
68 * @var HandlerInterface $delegate
70 protected $delegate;
72 /**
73 * @var int $factor
75 protected $factor;
77 /**
78 * @param HandlerInterface $handler Wrapped handler
79 * @param int $factor Sample factor
81 public function __construct( HandlerInterface $handler, $factor ) {
82 $this->delegate = $handler;
83 $this->factor = $factor;
86 public function isHandling( array $record ) {
87 return $this->delegate->isHandling( $record );
90 public function handle( array $record ) {
91 if ( $this->isHandling( $record )
92 && mt_rand( 1, $this->factor ) === 1
93 ) {
94 return $this->delegate->handle( $record );
96 return false;
99 public function handleBatch( array $records ) {
100 foreach ( $records as $record ) {
101 $this->handle( $record );
105 public function pushProcessor( $callback ) {
106 $this->delegate->pushProcessor( $callback );
107 return $this;
110 public function popProcessor() {
111 return $this->delegate->popProcessor();
114 public function setFormatter( FormatterInterface $formatter ) {
115 $this->delegate->setFormatter( $formatter );
116 return $this;
119 public function getFormatter() {
120 return $this->delegate->getFormatter();