Plop
A simple logging library for PHP
Plop\Plop Class Reference

Main class for Plop. More...

+ Inheritance diagram for Plop\Plop:

Public Member Functions

 __clone ()
 This class is not clone-safe.
 
 addLevelName ($levelName, $levelValue)
 
 addLogger (\Plop\LoggerInterface $logger)
 
 count ()
 
 getCreationDate ()
 
 getLevelName ($level)
 
 getLevelValue ($levelName)
 
 getLogger ($namespace= '', $class= '', $method= '')
 
 offsetExists ($offset)
 
 offsetGet ($offset)
 
 offsetSet ($offset, $logger)
 
 offsetUnset ($offset)
 
- Public Member Functions inherited from Plop\IndirectLoggerAbstract
 getClass ()
 
 getFilters ()
 
 getHandlers ()
 
 getLevel ()
 
 getMethod ()
 
 getNamespace ()
 
 getRecordFactory ()
 
 isEnabledFor ($level)
 
 log ($level, $msg, array $args=array(),\Exception $exception=null)
 
 setFilters (\Plop\FiltersCollectionAbstract $filters)
 
 setHandlers (\Plop\HandlersCollectionAbstract $handlers)
 
 setLevel ($level)
 
 setRecordFactory (\Plop\RecordFactoryInterface $factory)
 
- Public Member Functions inherited from Plop\LoggerAbstract
 alert ($msg, array $args=array(),\Exception $exception=null)
 
 critical ($msg, array $args=array(),\Exception $exception=null)
 
 debug ($msg, array $args=array(),\Exception $exception=null)
 
 emergency ($msg, array $args=array(),\Exception $exception=null)
 
 error ($msg, array $args=array(),\Exception $exception=null)
 
 exception ($msg,\Exception $exception, array $args=array())
 
 info ($msg, array $args=array(),\Exception $exception=null)
 
 notice ($msg, array $args=array(),\Exception $exception=null)
 
 warn ($msg, array $args=array(),\Exception $exception=null)
 
 warning ($msg, array $args=array(),\Exception $exception=null)
 

Static Public Member Functions

static findCaller ()
 
static & getInstance ()
 

Public Attributes

const BASIC_FORMAT = '[%(levelname)s] %(message)s'
 Default format used by the root logger.
 

Protected Member Functions

 __construct ()
 
 getIndirectLogger ()
 
- Protected Member Functions inherited from Plop\IndirectLoggerAbstract
 getIndirectLogger ()
 

Static Protected Member Functions

static getLoggerId (\Plop\LoggerInterface $logger)
 

Protected Attributes

 $created
 Date and time when the logging service was initialized.
 
 $levelNames
 Mapping between level names and their value.
 
 $loggers
 Associative array of loggers, indexed by their ID.
 

Static Protected Attributes

static $instance = null
 Shared instance of the logging service.
 

Detailed Description

Main class for Plop.

For the most basic use cases, the Plop class acts as an instance of Plop::LoggerInterface, which means you only need code such as the following to start logging messages:

// Grab an instance of the logging service.
// Log a message with the INFO level.
$logging->info('The cat is both dead and alive!');

This is equivalent to the following slightly less concise piece of code:

// Grab an instance of the logging service.
// Retrieve the logger for the current namespace,
// class and function explicitly.
$logger = $logging->getLogger(__NAMESPACE__, __CLASS__, __FUNCTION__);
// Log a message with the INFO level.
$logger->info('The cat is both dead and alive!');

For more complex use cases, you will need to configure logging (either manually or by means of tools such as a Dependency Injection Container). The following piece of code shows how to configure bits of Plop using PHP code:

// Grab the root logger.
$logger = $logging->getLogger();
// Log only messages with a level of INFO or more.
$logger->setLevel(\Plop::INFO);
// Change the format used to display logs on the console.
// Also, display dates as UNIX timestamps instead of using
// the default format ("2012-09-29 11:16:15,123").
foreach ($logger->getHandlers() as $handler)
$handler->getFormatter()
->setFormat('%(asctime)s - %(levelname)s - %(message)s')
->setDateFormat('U'); // We want UNIX timestamps.
// Send logs to the syslog daemon (using the default format),
// in addition to the console, but only if the record's level
// is at least WARNING.
$handler = new \Plop\Handler\SysLog(
\Plop\Handler\SysLog::DEFAULT_ADDRESS,
LOG_DAEMON
);
$handler->setLevel(\Plop::WARNING);
// Attach the new handler to the logger.
$handlers = $logger->getHandlers();
$handlers[] = $handler;

Definition at line 142 of file Plop.php.

Constructor & Destructor Documentation

Plop\Plop::__construct ( )
protected

Create a new instance of the logging service.

Definition at line 160 of file Plop.php.

Member Function Documentation

Plop\Plop::addLevelName (   $levelName,
  $levelValue 
)

Set a (new) name for a given level.

Parameters
string$levelNameName of the new level to register.
int$levelValueValue for the new level.
Return values
PlopThe current logging service (ie. $this).

Definition at line 229 of file Plop.php.

Plop\Plop::addLogger ( \Plop\LoggerInterface  $logger)

Register a logger.

This is effectively a shortcut for the following piece of code:

$logging[] = $logger;

It is kept to help other tools that operate on Plop (such as Dependency Injection Containers) but do not support object subscripting (ie. array notation).

Parameters
Plop::LoggerInterface$loggerNew logger to register.
Return values
PlopThe current logging service (ie. $this).
Note
Since the Plop class acts as a singleton, any logger registered with this method can be retrieved later by calling
(Plop::getInstance())->getLogger();
with the appropriate arguments.
You may register several loggers at the same time with this method. Just pass each logger to register as an argument to this method.

Definition at line 390 of file Plop.php.

Plop\Plop::count ( )

Return the number of loggers currently registered with Plop.

Return values
intNumber of loggers currently registered.

Implements Countable.

Definition at line 429 of file Plop.php.

static Plop\Plop::findCaller ( )
static

Return information about the caller of this method.

Return values
arrayAn associative array with information about the caller. This array always contains the following keys:
  • "ns" – the name of the caller's namespace.
  • "dir" – the path to the caller's file.
  • "file" – the name of the caller's file.
  • "line" – the line number in that file where the call was made.
  • "func" – the name of the function/method where the call happened.
  • "cls" – the name of the class where the call was made.

Each of those values may be null (or 0 in the case of "line") if the information could not be extracted from the call stack.

Note
Here, "caller" means the first context in the call stack that does not refer to one of Plop's methods/files.

Definition at line 624 of file Plop.php.

Referenced by Plop\Logger\log().

Plop\Plop::getCreationDate ( )

Get the date and time (as a UNIX timestamp with microseconds resolution) when the logging service was created.

Return values
floatCreation date of the logging service.

Definition at line 212 of file Plop.php.

Plop\Plop::getIndirectLogger ( )
protected

Return the actual logger that will be used to proxy calls to methods of the Plop::LoggerInterface interface.

Return values
Plop::LoggerInterfaceActual logger to proxy calls to.

Definition at line 594 of file Plop.php.

static& Plop\Plop::getInstance ( )
static

Return an instance of the logging service.

Return values
PlopInstance of the logging service.

Definition at line 195 of file Plop.php.

Referenced by Plop\Filter\Level\__construct(), Plop\Record\__construct(), Plop\Logger\isEnabledFor(), Plop\Psr3Logger\log(), and Plop\Logger\setLevel().

Plop\Plop::getLevelName (   $level)

Return the name of a level given its value.

Parameters
int$levelLevel for which a name must be returned.
Return values
stringName for the given level.
Note
If the level was not given a specific name (ie. Plop::addLevelName() was not called first), "Level $level" is returned.

Definition at line 255 of file Plop.php.

Plop\Plop::getLevelValue (   $levelName)

Return the value of a level given its name.

Parameters
string$levelNameLevel for which a value must be returned.
Return values
intValue for the given level.
Note
If the given level name is not known, Plop::NOTSET (0) is returned.
You may use Plop::addLevelName() to register new levels.

Definition at line 283 of file Plop.php.

Referenced by Plop\Filter\Level\__construct().

Plop\Plop::getLogger (   $namespace = '',
  $class = '',
  $method = '' 
)

Return the logger that is most appropriate given a bit of context.

Parameters
string$namespace(optional) Namespace for which a logger must be returned. Most of the time, you will pass the value of __NAMESPACE__ to this parameter.
string$class(optional) Class for which a logger must be returned. Most of the time, you will pass the value of __CLASS__ to this parameter.
string$method(optional) Method inside the given class for which a logger must be returned. Most of the time, you will pass the value of __FUNCTION__ to this parameter, even for methods, where this will have the same value as __FUNCTION__.
Return values
Plop::LoggerInterfaceLogger that is the most appropriate given the context.
Note
For functions, pass null as the value for the $class parameter.
Warning
When the default value is kept for every parameter, this method will return the root logger. It will not try to get the values of __NAMESPACE__, __CLASS__ and __FUNCTION__ automatically. If you need more magic than that, keep in mind that the Plop class also implements the Plop::LoggerInterface interface to provide shortcuts. Therefore,
$logging->info('The quick brown fox jumps over the lazy dog');
is equivalent to
$logging
->getLogger(__NAMESPACE__, __CLASS__, __FUNCTION__)
->info('The quick brown fox jumps over the lazy dog');

Definition at line 345 of file Plop.php.

static Plop\Plop::getLoggerId ( \Plop\LoggerInterface  $logger)
staticprotected

Return a logger's identifier.

Parameters
Plop::LoggerInterface$loggerA logger whose identifier we're interested in.
Return values
stringThe logger's identifier.

Definition at line 414 of file Plop.php.

Plop\Plop::offsetExists (   $offset)

Return a flag indicating whether a logger with the given identifier was registered with Plop.

Parameters
string | Plop::LoggerInterface$offsetA logger identifier. You may also pass a logger, in which case, that logger's identifier will be used for the test.
Return values
boolA flag indicating whether a logger was registered with that identifier (true) or not (false).
Warning
When a logger is passed to this method, it will only look for a registered logger with the same identifier. It will not check whether both loggers are actually the same.

Implements ArrayAccess.

Definition at line 557 of file Plop.php.

Plop\Plop::offsetGet (   $offset)

Return the registered logger with the given identifier, one of its parents, or the root logger if no other logger can be found.

Parameters
string$offsetIdentifier of the logger to return.
Return values
Plop::LoggerInterfaceThe registered logger with that identifier if one was found, or the root logger.
Warning
Do not call this method directly or bad things may happen. Use Plop::getLogger() instead.

Implements ArrayAccess.

Definition at line 493 of file Plop.php.

Plop\Plop::offsetSet (   $offset,
  $logger 
)

Register a new logger with Plop.

Parameters
mixed$offset(deprecated) Identifier for the logger, must match the identifier of the logger given in $logger.
Plop::LoggerInterface$loggerNew logger to register.
Deprecated:
The $offset argument is deprecated as Plop already deduces the value automatically from the $logger argument.
Note
If a logger already exists with the given identifier, it will be replaced by the new one.
The usual pattern for registering new loggers is
$logging[] = $logger;
Also, Plop::addLogger() is an alias for that pattern.

Implements ArrayAccess.

Definition at line 461 of file Plop.php.

Plop\Plop::offsetUnset (   $offset)

Unregister a logger.

Parameters
string | Plop::LoggerInterface$offsetIdentifier of the logger to unregister. You may also pass a logger, in which case, that logger's identifier will be used.
Warning
When a logger is passed to this method, it will only look for a registered logger with the same identifier. It will not check whether both loggers are actually the same.

Implements ArrayAccess.

Definition at line 582 of file Plop.php.


The documentation for this class was generated from the following file: