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
21 use Monolog\Handler\HandlerInterface
;
24 * Wrapper for another HandlerInterface that will only handle a percentage of
25 * records offered to it.
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
31 * Usage with MWLoggerMonologSpi:
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',
42 * return MWLogger::getProvider()->getHandler( 'some-handler');
44 * 2, // emit logs with a 1:2 chance
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.
61 * @author Bryan Davis <bd808@wikimedia.org>
62 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
64 class MWLoggerMonologSamplingHandler
implements HandlerInterface
{
67 * @var HandlerInterface $delegate
77 * @param HandlerInterface $handler Wrapped handler
78 * @param int $factor Sample factor
80 public function __construct( HandlerInterface
$handler, $factor ) {
81 $this->delegate
= $handler;
82 $this->factor
= $factor;
85 public function isHandling( array $record ) {
86 return $this->delegate
->isHandling( $record );
89 public function handle( array $record ) {
90 if ( $this->isHandling( $record )
91 && mt_rand( 1, $this->factor
) === 1
93 return $this->delegate
->handle( $record );
98 public function handleBatch( array $records ) {
99 foreach ( $records as $record ) {
100 $this->handle( $record );
104 public function pushProcessor( $callback ) {
105 $this->delegate
->pushProcessor( $callback );
109 public function popProcessor() {
110 return $this->delegate
->popProcessor();
113 public function setFormatter( FormatterInterface
$formatter ) {
114 $this->delegate
->setFormatter( $formatter );
118 public function getFormatter() {
119 return $this->delegate
->getFormatter();