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