48 const WARN = \Plop\WARNING;
145 const BASIC_FORMAT =
'[%(levelname)s] %(message)s';
148 protected static $instance = null;
162 $this->loggers = array();
163 $rootLogger = new \Plop\Logger(null, null);
164 $basicHandler = new \Plop\Handler\Stream(fopen(
'php://stderr',
'w'));
165 $this[] = $rootLogger;
166 $handlers = $rootLogger->getHandlers();
167 $formatter = new \Plop\Formatter(self::BASIC_FORMAT);
168 $handlers[] = $basicHandler->setFormatter($formatter);
169 $this->created = microtime(
true);
170 $this->levelNames = array(
171 \
Plop\NOTSET =>
'NOTSET',
172 \
Plop\DEBUG =>
'DEBUG',
173 \
Plop\INFO =>
'INFO',
174 \
Plop\NOTICE =>
'NOTICE',
175 \
Plop\WARNING =>
'WARNING',
176 \
Plop\ERROR =>
'ERROR',
177 \
Plop\CRITICAL =>
'CRITICAL',
178 \
Plop\ALERT =>
'ALERT',
179 \
Plop\EMERGENCY =>
'EMERGENCY',
186 throw new \Plop\Exception(
'Cloning this class is forbidden');
197 if (static::$instance === null) {
199 static::$instance =
new $c();
201 return static::$instance;
214 return $this->created;
231 if (!is_int($levelValue)) {
232 throw new \Plop\Exception(
'Invalid level value');
234 if (!is_string($levelName)) {
235 throw new \Plop\Exception(
'Invalid level name');
237 $this->levelNames[$levelValue] = $levelName;
257 if (!is_int($level)) {
258 throw new \Plop\Exception(
'Invalid level value');
260 if (!isset($this->levelNames[$level])) {
261 return "Level $level";
263 return $this->levelNames[$level];
285 if (!is_string($levelName)) {
286 throw new \Plop\Exception(
'Invalid level name');
288 $key = array_search($levelName, $this->levelNames,
true);
345 public function getLogger($namespace =
'', $class =
'', $method =
'')
348 $class = substr($class, strrpos(
'\\' . $class,
'\\'));
349 $method = substr($method, strrpos(
'\\' . $method,
'\\'));
353 $method = substr((
string) $method, strrpos(
':' . $method,
':'));
355 return $this[
"$method:$class:$namespace"];
392 $loggers = func_get_args();
393 foreach ($loggers as $logger) {
395 throw new \Plop\Exception(
'Not a logger');
399 foreach ($loggers as $logger) {
416 $func = $logger->getMethod();
417 $cls = $logger->getClass();
418 $ns = $logger->getNamespace();
419 return "$func:$cls:$ns";
431 return count($this->loggers);
464 throw new \Plop\Exception(
'Invalid logger');
467 $id = static::getLoggerId($logger);
468 if (is_string($offset)) {
469 if ($offset != $id) {
470 throw new \Plop\Exception(
'Identifier mismatch');
474 $this->loggers[$id] = $logger;
495 if (!is_string($offset)) {
496 throw new \Plop\Exception(
'Invalid identifier');
499 $parts = explode(
':', $offset, 3);
500 if (count($parts) != 3) {
501 throw new \Plop\Exception(
'Invalid identifier');
503 list($method, $class, $ns) = $parts;
505 $len = -strlen(
'\\');
506 while (substr($ns, $len) ==
'\\') {
507 $ns = (string) substr($ns, 0, $len);
511 if (isset($this->loggers[
"$method:$class:$ns"])) {
512 return $this->loggers[
"$method:$class:$ns"];
516 if ($class !=
"" && isset($this->loggers[
":$class:$ns"])) {
517 return $this->loggers[
":$class:$ns"];
521 $parts = explode(
'\\', $ns);
522 while (count($parts)) {
523 $offset = implode(
'\\', $parts);
528 if (isset($this->loggers[
"::$offset"])) {
529 return $this->loggers[
"::$offset"];
535 return $this->loggers[
'::'];
560 $offset = static::getLoggerId($offset);
562 if (!is_string($offset)) {
563 throw new \Plop\Exception(
'Invalid identifier');
565 return isset($this->loggers[$offset]);
585 $offset = static::getLoggerId($offset);
587 if ($offset ==
"::") {
588 throw new \Plop\Exception(
'The root logger cannot be unset');
590 unset($this->loggers[$offset]);
596 $caller = static::findCaller();
597 return $this[
"{$caller['func']}:{$caller['cls']}:{$caller['ns']}"];
626 if (version_compare(PHP_VERSION,
'5.3.6',
'>=')) {
627 $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
629 $bt = debug_backtrace(
false);
633 $ns = __NAMESPACE__ .
'\\';
635 $ns2 = $ns .
'Tests' .
'\\';
636 for ($i = 0; $i < $max; $i++) {
637 if (isset($bt[$i][
'function']) &&
638 !strncmp($ns, $bt[$i][
'function'], $len) &&
639 strncmp($ns2, $bt[$i][
'function'], $len + 6)) {
644 if (isset($bt[$i][
'class']) &&
645 !strncmp($ns, $bt[$i][
'class'], $len) &&
646 strncmp($ns2, $bt[$i][
'class'], $len + 6)) {
665 $func = isset($bt[$i][
'function']) ? $bt[$i][
'function'] : null;
666 $cls = isset($bt[$i][
'class']) ? $bt[$i][
'class'] : null;
667 $file = isset($bt[$i - 1][
'file']) ? $bt[$i - 1][
'file'] : null;
668 $line = isset($bt[$i - 1][
'line']) ? $bt[$i - 1][
'line'] : 0;
670 if (($pos = strrpos($func,
'\\')) !==
false) {
671 $ns = substr($func, 0, $pos);
672 $func = substr($func, $pos + 1);
673 } elseif (($pos = strrpos($cls,
'\\')) !==
false) {
674 $ns = substr($cls, 0, $pos);
675 $cls = substr($cls, $pos + 1);
Interface to provide accessing objects as arrays.
$levelNames
Mapping between level names and their value.
addLevelName($levelName, $levelValue)
offsetSet($offset, $logger)
$created
Date and time when the logging service was initialized.
static getLoggerId(\Plop\LoggerInterface $logger)
getLogger($namespace= '', $class= '', $method= '')
An abstract class that can be used to proxy logging calls to an actual logger.
getLevelValue($levelName)
$loggers
Associative array of loggers, indexed by their ID.
addLogger(\Plop\LoggerInterface $logger)
__clone()
This class is not clone-safe.
Classes implementing Countable can be used with the count() function.