monolog: MWLoggerMonologSamplingHandler
[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
23 /**
24 * Wrapper for another HandlerInterface that will only handle a percentage of
25 * records offered to it.
26 *
27 * When HandlerInterface::handle() is called for a given record, it will be
28 * handled or ignored with a one in N chance based on the sample factor given
29 * for the handler.
30 *
31 * Usage with MWLoggerMonologSpi:
32 * @code
33 * $wgMWLoggerDefaultSpi = array(
34 * 'class' => 'MWLoggerMonologSpi',
35 * 'args' => array( array(
36 * 'handlers' => array(
37 * 'some-handler' => array( ... ),
38 * 'sampled-some-handler' => array(
39 * 'class' => 'MWLoggerMonologSamplingHandler',
40 * 'args' => array(
41 * function() {
42 * return MWLogger::getProvider()->getHandler( 'some-handler');
43 * },
44 * 2, // emit logs with a 1:2 chance
45 * ),
46 * ),
47 * ),
48 * ) ),
49 * );
50 * @endcode
51 *
52 * A sampled event stream can be useful for logging high frequency events in
53 * a production environment where you only need an idea of what is happening
54 * and are not concerned with capturing every occurence. Since the decision to
55 * handle or not handle a particular event is determined randomly, the
56 * resulting sampled log is not guaranteed to contain 1/N of the events that
57 * occurred in the application but based on [[Law of large numbers]] it will
58 * tend to be close to this ratio with a large number of attempts.
59 *
60 * @since 1.25
61 * @author Bryan Davis <bd808@wikimedia.org>
62 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
63 */
64 class MWLoggerMonologSamplingHandler implements HandlerInterface {
65
66 /**
67 * @var HandlerInterface $delegate
68 */
69 protected $delegate;
70
71 /**
72 * @var int $factor
73 */
74 protected $factor;
75
76 /**
77 * @param HandlerInterface $handler Wrapped handler
78 * @param int $factor Sample factor
79 */
80 public function __construct( HandlerInterface $handler, $factor ) {
81 $this->delegate = $handler;
82 $this->factor = $factor;
83 }
84
85 public function isHandling( array $record ) {
86 return $this->delegate->isHandling( $record );
87 }
88
89 public function handle( array $record ) {
90 if ( $this->isHandling( $record )
91 && mt_rand( 1, $this->factor ) === 1
92 ) {
93 return $this->delegate->handle( $record );
94 }
95 return false;
96 }
97
98 public function handleBatch( array $records ) {
99 foreach ( $records as $record ) {
100 $this->handle( $record );
101 }
102 }
103
104 public function pushProcessor( $callback ) {
105 $this->delegate->pushProcessor( $callback );
106 return $this;
107 }
108
109 public function popProcessor() {
110 return $this->delegate->popProcessor();
111 }
112
113 public function setFormatter( FormatterInterface $formatter ) {
114 $this->delegate->setFormatter( $formatter );
115 return $this;
116 }
117
118 public function getFormatter() {
119 return $this->delegate->getFormatter();
120 }
121
122 }