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 use Monolog\Logger;
15  
16 /**
17 * Simple handler wrapper that filters records based on a list of levels
18 *
19 * It can be configured with an exact list of levels to allow, or a min/max level.
20 *
21 * @author Hennadiy Verkh
22 * @author Jordi Boggiano <j.boggiano@seld.be>
23 */
24 class FilterHandler extends AbstractHandler
25 {
26 /**
27 * Handler or factory callable($record, $this)
28 *
29 * @var callable|\Monolog\Handler\HandlerInterface
30 */
31 protected $handler;
32  
33 /**
34 * Minimum level for logs that are passed to handler
35 *
36 * @var int[]
37 */
38 protected $acceptedLevels;
39  
40 /**
41 * Whether the messages that are handled can bubble up the stack or not
42 *
43 * @var Boolean
44 */
45 protected $bubble;
46  
47 /**
48 * @param callable|HandlerInterface $handler Handler or factory callable($record, $this).
49 * @param int|array $minLevelOrList A list of levels to accept or a minimum level if maxLevel is provided
50 * @param int $maxLevel Maximum level to accept, only used if $minLevelOrList is not an array
51 * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
52 */
53 public function __construct($handler, $minLevelOrList = Logger::DEBUG, $maxLevel = Logger::EMERGENCY, $bubble = true)
54 {
55 $this->handler = $handler;
56 $this->bubble = $bubble;
57 $this->setAcceptedLevels($minLevelOrList, $maxLevel);
58  
59 if (!$this->handler instanceof HandlerInterface && !is_callable($this->handler)) {
60 throw new \RuntimeException("The given handler (".json_encode($this->handler).") is not a callable nor a Monolog\Handler\HandlerInterface object");
61 }
62 }
63  
64 /**
65 * @return array
66 */
67 public function getAcceptedLevels()
68 {
69 return array_flip($this->acceptedLevels);
70 }
71  
72 /**
73 * @param int|string|array $minLevelOrList A list of levels to accept or a minimum level or level name if maxLevel is provided
74 * @param int|string $maxLevel Maximum level or level name to accept, only used if $minLevelOrList is not an array
75 */
76 public function setAcceptedLevels($minLevelOrList = Logger::DEBUG, $maxLevel = Logger::EMERGENCY)
77 {
78 if (is_array($minLevelOrList)) {
79 $acceptedLevels = array_map('Monolog\Logger::toMonologLevel', $minLevelOrList);
80 } else {
81 $minLevelOrList = Logger::toMonologLevel($minLevelOrList);
82 $maxLevel = Logger::toMonologLevel($maxLevel);
83 $acceptedLevels = array_values(array_filter(Logger::getLevels(), function ($level) use ($minLevelOrList, $maxLevel) {
84 return $level >= $minLevelOrList && $level <= $maxLevel;
85 }));
86 }
87 $this->acceptedLevels = array_flip($acceptedLevels);
88 }
89  
90 /**
91 * {@inheritdoc}
92 */
93 public function isHandling(array $record)
94 {
95 return isset($this->acceptedLevels[$record['level']]);
96 }
97  
98 /**
99 * {@inheritdoc}
100 */
101 public function handle(array $record)
102 {
103 if (!$this->isHandling($record)) {
104 return false;
105 }
106  
107 // The same logic as in FingersCrossedHandler
108 if (!$this->handler instanceof HandlerInterface) {
109 $this->handler = call_user_func($this->handler, $record, $this);
110 if (!$this->handler instanceof HandlerInterface) {
111 throw new \RuntimeException("The factory callable should return a HandlerInterface");
112 }
113 }
114  
115 if ($this->processors) {
116 foreach ($this->processors as $processor) {
117 $record = call_user_func($processor, $record);
118 }
119 }
120  
121 $this->handler->handle($record);
122  
123 return false === $this->bubble;
124 }
125  
126 /**
127 * {@inheritdoc}
128 */
129 public function handleBatch(array $records)
130 {
131 $filtered = array();
132 foreach ($records as $record) {
133 if ($this->isHandling($record)) {
134 $filtered[] = $record;
135 }
136 }
137  
138 $this->handler->handleBatch($filtered);
139 }
140 }