Merge "SpecialTrackingCategories: Read from the extension registry"
[lhc/web/wiklou.git] / includes / debug / logger / monolog / SamplingHandler.php
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21 use Monolog\Handler\HandlerInterface;
22 use Monolog\Formatter\FormatterInterface;
23
24 /**
25 * Wrapper for another HandlerInterface that will only handle a percentage of
26 * records offered to it.
27 *
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.
31 *
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
52 *
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.
60 *
61 * @since 1.25
62 * @author Bryan Davis <bd808@wikimedia.org>
63 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
64 */
65 class MWLoggerMonologSamplingHandler implements HandlerInterface {
66
67 /**
68 * @var HandlerInterface $delegate
69 */
70 protected $delegate;
71
72 /**
73 * @var int $factor
74 */
75 protected $factor;
76
77 /**
78 * @param HandlerInterface $handler Wrapped handler
79 * @param int $factor Sample factor
80 */
81 public function __construct( HandlerInterface $handler, $factor ) {
82 $this->delegate = $handler;
83 $this->factor = $factor;
84 }
85
86 public function isHandling( array $record ) {
87 return $this->delegate->isHandling( $record );
88 }
89
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 );
95 }
96 return false;
97 }
98
99 public function handleBatch( array $records ) {
100 foreach ( $records as $record ) {
101 $this->handle( $record );
102 }
103 }
104
105 public function pushProcessor( $callback ) {
106 $this->delegate->pushProcessor( $callback );
107 return $this;
108 }
109
110 public function popProcessor() {
111 return $this->delegate->popProcessor();
112 }
113
114 public function setFormatter( FormatterInterface $formatter ) {
115 $this->delegate->setFormatter( $formatter );
116 return $this;
117 }
118
119 public function getFormatter() {
120 return $this->delegate->getFormatter();
121 }
122
123 }