Plop
A simple logging library for PHP
Logger.php
1 <?php
2 /*
3  This file is part of Plop, a simple logging library for PHP.
4 
5  Copyright © 2010-2014 François Poirotte
6 
7  Plop is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Plop is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Plop. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 namespace Plop;
22 
28 {
30  protected $ns;
31 
33  protected $cls;
34 
36  protected $method;
37 
39  protected $level;
40 
42  protected $handlers;
43 
45  protected $emittedWarning;
46 
48  protected $filters;
49 
79  public function __construct(
80  $ns = null,
81  $cls = null,
82  $method = null,
83  \Plop\HandlersCollectionAbstract $handlers = null,
84  \Plop\FiltersCollectionAbstract $filters = null
85  ) {
86  if ($handlers === null) {
87  $handlers = new \Plop\HandlersCollection();
88  }
89  if ($filters === null) {
90  $filters = new \Plop\FiltersCollection();
91  }
92  if ($ns !== null) {
93  while (substr($ns, -strlen('\\')) == '\\') {
94  $ns = (string) substr($ns, 0, -strlen('\\'));
95  }
96  }
97 
98  // Strip potential namespace from class name.
99  $cls = (string) substr($cls, strrpos('\\' . $cls, '\\'));
100  if ($cls === '') {
101  $cls = null;
102  }
103 
104  // Strip potential namespace from method/function name.
105  $method = (string) substr($method, strrpos('\\' . $method, '\\'));
106  if ($method === '') {
107  $method = null;
108  }
109 
110  $this->ns = $ns;
111  $this->cls = $cls;
112  $this->method = $method;
113  $this->level = \Plop\NOTSET;
114  $this->handlers = $handlers;
115  $this->emittedWarning = false;
116  $this->filters = $filters;
117  $this->setRecordFactory(new \Plop\RecordFactory());
118  }
119 
121  public function getNamespace()
122  {
123  return $this->ns;
124  }
125 
127  public function getClass()
128  {
129  return $this->cls;
130  }
131 
133  public function getMethod()
134  {
135  return $this->method;
136  }
137 
139  public function getRecordFactory()
140  {
141  return $this->recordFactory;
142  }
143 
145  public function setRecordFactory(\Plop\RecordFactoryInterface $factory)
146  {
147  $this->recordFactory = $factory;
148  return $this;
149  }
150 
152  public function getFilters()
153  {
154  return $this->filters;
155  }
156 
158  public function setFilters(\Plop\FiltersCollectionAbstract $filters)
159  {
160  $this->filters = $filters;
161  return $this;
162  }
163 
165  public function getHandlers()
166  {
167  return $this->handlers;
168  }
169 
171  public function setHandlers(\Plop\HandlersCollectionAbstract $handlers)
172  {
173  $this->handlers = $handlers;
174  return $this;
175  }
176 
178  public function log(
179  $level,
180  $msg,
181  array $args = array(),
182  \Exception $exception = null
183  ) {
184  if ($this->isEnabledFor($level)) {
185  $caller = \Plop\Plop::findCaller();
186  $record = $this->recordFactory->createRecord(
187  $this->ns,
188  $this->cls,
189  $this->method,
190  $caller['ns'],
191  $caller['cls'],
192  $caller['func'],
193  $level,
194  $caller['file'] ? $caller['file'] : '???',
195  $caller['line'],
196  $msg,
197  $args,
198  $exception
199  );
200  $this->handle($record);
201  }
202  return $this;
203  }
204 
214  protected function handle(\Plop\RecordInterface $record)
215  {
216  if ($this->filters->filter($record)) {
217  $this->callHandlers($record);
218  }
219  return $this;
220  }
221 
232  protected function callHandlers(\Plop\RecordInterface $record)
233  {
234  if (!count($this->handlers) && !$this->emittedWarning) {
235  $stderr = $this->getStderr();
236  fprintf(
237  $stderr,
238  'No handlers could be found for logger ("%s" in "%s")' . "\n",
239  $this->cls .
240  ($this->cls === null || $this->cls === '' ? '' : '::') .
241  $this->method,
242  $this->ns
243  );
244  fclose($stderr);
245  $this->emittedWarning = true;
246  return $this;
247  }
248 
249  $this->handlers->handle($record);
250  return $this;
251  }
252 
263  protected function getStderr()
264  {
265  return fopen('php://stderr', 'at');
266  }
267 
269  public function getLevel()
270  {
271  return $this->level;
272  }
273 
275  public function setLevel($level)
276  {
277  if (!is_int($level)) {
278  $plop = \Plop\Plop::getInstance();
279  $level = $plop->getLevelValue($level);
280  }
281  $this->level = $level;
282  return $this;
283  }
284 
286  public function isEnabledFor($level)
287  {
288  if (!is_int($level)) {
289  $plop = \Plop\Plop::getInstance();
290  $level = $plop->getLevelValue($level);
291  }
292  return ($level >= $this->level);
293  }
294 }
setFilters(\Plop\FiltersCollectionAbstract $filters)
Definition: Logger.php:158
setHandlers(\Plop\HandlersCollectionAbstract $handlers)
Definition: Logger.php:171
An interface for a factory that produces instances that implement the Plop::RecordInterface interface...
__construct($ns=null, $cls=null, $method=null,\Plop\HandlersCollectionAbstract $handlers=null,\Plop\FiltersCollectionAbstract $filters=null)
Definition: Logger.php:79
An abstract class that can be used as a base to create new loggers.
Interface for a log record.
setLevel($level)
Definition: Logger.php:275
static & getInstance()
Definition: Plop.php:195
$filters
An object handling a collection of filters.
Definition: Logger.php:48
static findCaller()
Definition: Plop.php:624
A factory that creates log records as new instances of the Plop::Record class.
handle(\Plop\RecordInterface $record)
Definition: Logger.php:214
log($level, $msg, array $args=array(),\Exception $exception=null)
Definition: Logger.php:178
callHandlers(\Plop\RecordInterface $record)
Definition: Logger.php:232
getHandlers()
Definition: Logger.php:165
getFilters()
Definition: Logger.php:152
Abstract class for a collection of filters.
$handlers
A collection of handlers currently associated with this logger.
Definition: Logger.php:42
$cls
Name of the class this logger relates to.
Definition: Logger.php:33
$ns
Name of the namespace this logger relates to.
Definition: Logger.php:30
isEnabledFor($level)
Definition: Logger.php:286
This exception is thrown by Plop whenever a problem is detected.
Definition: Exception.php:30
getNamespace()
Definition: Logger.php:121
setRecordFactory(\Plop\RecordFactoryInterface $factory)
Definition: Logger.php:145
$level
Minimum level at which this logger will accept log entries.
Definition: Logger.php:39
$emittedWarning
Whether a warning has been emitted about this logger having no handlers.
Definition: Logger.php:45
$method
Name of the method or function this logger relates to.
Definition: Logger.php:36
Interface for a collection of handlers.
getRecordFactory()
Definition: Logger.php:139
A class that provides logging capabilities.
Definition: Logger.php:27