PHP Classes

PHP Logger Library Depot: Retrieve logger objects using named keys

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 68 All time: 10,325 This week: 571Up
Version License PHP version Categories
loggerdepot 1.0.1GNU Lesser Genera...5PHP 5, Logging
Description 

Author

This package can retrieve logger objects using named keys.

It can register one or more logger objects and associate them with a named key.

The package can also return the logger that is associated to a given name so applications can use different logger classes for different purposes.

Innovation Award
PHP Programming Innovation award nominee
November 2019
Number 3
Logging is an important function of applications. It allows to track the activity of the applications in a way that it helps discovering eventual problems that need to be fixed.

An application may have different parts, which may require different types of logging methods.

This package allows using different logger objects to record activity of different parts of an application.

Each logger may be referenced by a name, allowing developers to use a more meaningful name for each type of logger depending on the type of the activity of the application that it is used to register the activity.

Manuel Lemos
Picture of Kjell-Inge Gustafsson
  Performance   Level  
Name: Kjell-Inge Gustafsson <contact>
Classes: 15 packages by
Country: Sweden Sweden
Age: ???
All time rank: 4912 in Sweden Sweden
Week rank: 46 Up1 in Sweden Sweden Up
Innovation award
Innovation award
Nominee: 6x

Documentation

LoggerDepot

is a depot for PHP application/software loggers >making loggers available on demand.

Each logger is identified by a unique and fixed (type case-sensitive _string_) key and set and retrieved using the key.

You can use namespace as key (ex __NAMESPACE__) setting up a logger and invoke the logger using (qualified namespaced) class names (ex get_class() or __CLASS__) in the namespace tree.

It is possible to combine fixed key and 'namespaced' loggers in the depot.

You may also use different keys for the same logger as well as set a logger as a fallback logger.

Invoking of a logger is as easy as LoggerDepot::getLogger( <key> ). If no logger is set, a Psr\Log\NullLogger is returned.

The construction makes it possible to supervise loggers for separate parts (functions, modules, components etc) of your software.

Usage

With \<logger\> (below) means any PHP logger and \<logConfig\> ) corresponding config.

  • logger set up
<?php
namespace Kigkonsult\Example;
use Kigkonsult\LoggerDepot\LoggerDepot;

LoggerDepot::registerLogger(
    __NAMESPACE__, // key
    new <logger>( <logConfig> )
);

  • usage in class
    <?php
    namespace Kigkonsult\Example;
    use Kigkonsult\LoggerDepot\LoggerDepot;
    
    

class LoggerUserClass1 {

private $logger = null;

public function __construct() {
    $this->logger = LoggerDepot::getLogger( __CLASS__ );
    $this->logger->debug( 'Start ' . __CLASS__ );
}

public function aMethod( $argument ) {
    $this->logger->debug( 'Start ' . __METHOD__ );
    $this->logger->info( 'aMethod argument IN : ' . $argument );
}

}

class LoggerUserClass2 {

public static function bMethod( $argument ) {
    LoggerDepot::getLogger( __CLASS__ )->debug( 'Start ' . __METHOD__ );
}

}


###### Register loggers

- Set up a (string) keyed logger:

<?php use Kigkonsult\LoggerDepot\LoggerDepot;

LoggerDepot::registerLogger( <key>, new <logger>( <logConfig> ));


Note, register using the same key again will replace existing logger.

- Or check first if logger is set:

<?php use Kigkonsult\LoggerDepot\LoggerDepot;

if( ! LoggerDepot::isLoggerSet( <key> )) {

LoggerDepot::registerLogger( <key>, new <logger>( <logConfig> ));

}


###### getLogger usage 1

- Get a (string) keyed logger, on demand:

<?php use Kigkonsult\LoggerDepot\LoggerDepot;

$logger = LoggerDepot::getLogger( <key> );


- or a one-liner (ex. for a Psr\Log logger):

<?php use Kigkonsult\LoggerDepot\LoggerDepot;

LoggerDepot::getLogger( <key> )->error( 'Error message' );


###### getLogger usage 2

The search of requested logger is performed in logger (set-)order.

- Set up a 'namespace' logger in top of a 'namespace' tree:

<?php namespace Kigkonsult\Example; use Kigkonsult\LoggerDepot\LoggerDepot;

LoggerDepot::registerLogger( __NAMESPACE__, new <logger>( <logConfig> ));


- Get a 'namespace' logger in a class in the same/sub-level 'namespace' tree:

<?php namespace Kigkonsult\Example\Impl; use Kigkonsult\LoggerDepot\LoggerDepot;

class LoggerUserClass3 {

public function aMethod( $argument ) {
    $logger = LoggerDepot::getLogger( get_class());
    ...
    $logger->info( 'aMethod argument IN : ' . $argument );
}

}


- or a one-liner (ex. for a Psr\Log logger):

<?php namespace Kigkonsult\Example\Impl; use Kigkonsult\LoggerDepot\LoggerDepot;

class LoggerUserClass4 {

public function aMethod( $argument ) {
    LoggerDepot::getLogger( get_class())->error( 'Error message' );
}

}


###### fallback

- Set up a fallback logger to use in case requested logger is not found:

<?php use Kigkonsult\LoggerDepot\LoggerDepot;

LoggerDepot::registerLogger( <key>, new <logger>( <logConfig> )); LoggerDepot::setFallbackLoggerKey( <key> );

Note, `LoggerDepot::setFallbackLoggerKey()` return `false` if key (for logger) is not set.

- or shorter

<?php use Kigkonsult\LoggerDepot\LoggerDepot;

LoggerDepot::registerLogger( <key>, new <logger>( <logConfig> ), true );


The first logger is always set as fallback until specific logger is set.
Hence, a single logger will also serve as fallback.

- Fetch key for the fallback logger:

<?php use Kigkonsult\LoggerDepot\LoggerDepot;

$key = LoggerDepot::getFallbackLoggerKey();


###### Misc

- Fetch (array) all keys for all loggers:

<?php use Kigkonsult\LoggerDepot\LoggerDepot;

$keys = LoggerDepot::getLoggerKeys();


- Remove a specific logger:

<?php use Kigkonsult\LoggerDepot\LoggerDepot;

LoggerDepot::unregisterLogger( <key> );


Caveat, removing the fallback logger will force 'the next' (in order) to take over.

- And (in the end?) remove all:

<?php use Kigkonsult\LoggerDepot\LoggerDepot;

foreach( LoggerDepot::getLoggerKeys() as $key ) {

LoggerDepot::unregisterLogger( $key );

}

###### Installation

[Composer], from the Command Line:

composer require kigkonsult/loggerdepot:dev-master


Composer, in your `composer.json`:

{

"require": {
    "kigkonsult/loggerdepot": "dev-master"
}

}


Composer, acquire access

use Kigkonsult\LoggerDepot\LoggerDepot; ... include 'vendor/autoload.php';



Otherwise , download and acquire..

use Kigkonsult\LoggerDepot\LoggerDepot; ... include 'pathToSource/loggerdepot/autoload.php';


###### License

This project is licensed under the LGPLv3 License


[Composer]:https://getcomposer.org/

  Files folder image Files (8)  
File Role Description
Files folder imagedocs (2 files)
Files folder imagesrc (1 file)
Files folder imagetest (1 file)
Accessible without login Plain text file autoload.php Aux. Auxiliary script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files (8)  /  docs  
File Role Description
  Accessible without login Plain text file lgpl.txt Doc. Documentation
  Accessible without login Plain text file README.txt Doc. Documentation

  Files folder image Files (8)  /  src  
File Role Description
  Plain text file LoggerDepot.php Class Class source

  Files folder image Files (8)  /  test  
File Role Description
  Plain text file LoggerDepotTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:68
This week:0
All time:10,325
This week:571Up
User Comments (1)
Thats a very good class ;-)
4 years ago (José Filipe Lopes Santos)
70%StarStarStarStar