scratch – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 1 <?php
2  
3 /*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11  
12 namespace Monolog\Handler;
13  
14 /**
15 * Sampling handler
16 *
17 * A sampled event stream can be useful for logging high frequency events in
18 * a production environment where you only need an idea of what is happening
19 * and are not concerned with capturing every occurrence. Since the decision to
20 * handle or not handle a particular event is determined randomly, the
21 * resulting sampled log is not guaranteed to contain 1/N of the events that
22 * occurred in the application, but based on the Law of large numbers, it will
23 * tend to be close to this ratio with a large number of attempts.
24 *
25 * @author Bryan Davis <bd808@wikimedia.org>
26 * @author Kunal Mehta <legoktm@gmail.com>
27 */
28 class SamplingHandler extends AbstractHandler
29 {
30 /**
31 * @var callable|HandlerInterface $handler
32 */
33 protected $handler;
34  
35 /**
36 * @var int $factor
37 */
38 protected $factor;
39  
40 /**
41 * @param callable|HandlerInterface $handler Handler or factory callable($record, $fingersCrossedHandler).
42 * @param int $factor Sample factor
43 */
44 public function __construct($handler, $factor)
45 {
46 parent::__construct();
47 $this->handler = $handler;
48 $this->factor = $factor;
49  
50 if (!$this->handler instanceof HandlerInterface && !is_callable($this->handler)) {
51 throw new \RuntimeException("The given handler (".json_encode($this->handler).") is not a callable nor a Monolog\Handler\HandlerInterface object");
52 }
53 }
54  
55 public function isHandling(array $record)
56 {
57 return $this->handler->isHandling($record);
58 }
59  
60 public function handle(array $record)
61 {
62 if ($this->isHandling($record) && mt_rand(1, $this->factor) === 1) {
63 // The same logic as in FingersCrossedHandler
64 if (!$this->handler instanceof HandlerInterface) {
65 $this->handler = call_user_func($this->handler, $record, $this);
66 if (!$this->handler instanceof HandlerInterface) {
67 throw new \RuntimeException("The factory callable should return a HandlerInterface");
68 }
69 }
70  
71 if ($this->processors) {
72 foreach ($this->processors as $processor) {
73 $record = call_user_func($processor, $record);
74 }
75 }
76  
77 $this->handler->handle($record);
78 }
79  
80 return false === $this->bubble;
81 }
82 }