|
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 use Monolog\Formatter\FormatterInterface; |
|
16 use Monolog\Formatter\LineFormatter; |
|
17 |
|
18 /** |
|
19 * Base Handler class providing the Handler structure |
|
20 * |
|
21 * @author Jordi Boggiano <j.boggiano@seld.be> |
|
22 */ |
|
23 abstract class AbstractHandler implements HandlerInterface |
|
24 { |
|
25 protected $level = Logger::DEBUG; |
|
26 protected $bubble = false; |
|
27 |
|
28 /** |
|
29 * @var FormatterInterface |
|
30 */ |
|
31 protected $formatter; |
|
32 protected $processors = array(); |
|
33 |
|
34 /** |
|
35 * @param integer $level The minimum logging level at which this handler will be triggered |
|
36 * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not |
|
37 */ |
|
38 public function __construct($level = Logger::DEBUG, $bubble = true) |
|
39 { |
|
40 $this->level = $level; |
|
41 $this->bubble = $bubble; |
|
42 } |
|
43 |
|
44 /** |
|
45 * {@inheritdoc} |
|
46 */ |
|
47 public function isHandling(array $record) |
|
48 { |
|
49 return $record['level'] >= $this->level; |
|
50 } |
|
51 |
|
52 /** |
|
53 * {@inheritdoc} |
|
54 */ |
|
55 public function handleBatch(array $records) |
|
56 { |
|
57 foreach ($records as $record) { |
|
58 $this->handle($record); |
|
59 } |
|
60 } |
|
61 |
|
62 /** |
|
63 * Closes the handler. |
|
64 * |
|
65 * This will be called automatically when the object is destroyed |
|
66 */ |
|
67 public function close() |
|
68 { |
|
69 } |
|
70 |
|
71 /** |
|
72 * {@inheritdoc} |
|
73 */ |
|
74 public function pushProcessor($callback) |
|
75 { |
|
76 if (!is_callable($callback)) { |
|
77 throw new \InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), '.var_export($callback, true).' given'); |
|
78 } |
|
79 array_unshift($this->processors, $callback); |
|
80 } |
|
81 |
|
82 /** |
|
83 * {@inheritdoc} |
|
84 */ |
|
85 public function popProcessor() |
|
86 { |
|
87 if (!$this->processors) { |
|
88 throw new \LogicException('You tried to pop from an empty processor stack.'); |
|
89 } |
|
90 return array_shift($this->processors); |
|
91 } |
|
92 |
|
93 /** |
|
94 * {@inheritdoc} |
|
95 */ |
|
96 public function setFormatter(FormatterInterface $formatter) |
|
97 { |
|
98 $this->formatter = $formatter; |
|
99 } |
|
100 |
|
101 /** |
|
102 * {@inheritdoc} |
|
103 */ |
|
104 public function getFormatter() |
|
105 { |
|
106 if (!$this->formatter) { |
|
107 $this->formatter = $this->getDefaultFormatter(); |
|
108 } |
|
109 |
|
110 return $this->formatter; |
|
111 } |
|
112 |
|
113 /** |
|
114 * Sets minimum logging level at which this handler will be triggered. |
|
115 * |
|
116 * @param integer $level |
|
117 */ |
|
118 public function setLevel($level) |
|
119 { |
|
120 $this->level = $level; |
|
121 } |
|
122 |
|
123 /** |
|
124 * Gets minimum logging level at which this handler will be triggered. |
|
125 * |
|
126 * @return integer |
|
127 */ |
|
128 public function getLevel() |
|
129 { |
|
130 return $this->level; |
|
131 } |
|
132 |
|
133 /** |
|
134 * Sets the bubbling behavior. |
|
135 * |
|
136 * @param Boolean $bubble True means that bubbling is not permitted. |
|
137 * False means that this handler allows bubbling. |
|
138 */ |
|
139 public function setBubble($bubble) |
|
140 { |
|
141 $this->bubble = $bubble; |
|
142 } |
|
143 |
|
144 /** |
|
145 * Gets the bubbling behavior. |
|
146 * |
|
147 * @return Boolean True means that bubbling is not permitted. |
|
148 * False means that this handler allows bubbling. |
|
149 */ |
|
150 public function getBubble() |
|
151 { |
|
152 return $this->bubble; |
|
153 } |
|
154 |
|
155 public function __destruct() |
|
156 { |
|
157 $this->close(); |
|
158 } |
|
159 |
|
160 /** |
|
161 * Gets the default formatter. |
|
162 * |
|
163 * @return FormatterInterface |
|
164 */ |
|
165 protected function getDefaultFormatter() |
|
166 { |
|
167 return new LineFormatter(); |
|
168 } |
|
169 } |