opxcore/log-manager

The OpxCore log manager.

1.3.1 2021-02-24 13:23 UTC

This package is auto-updated.

Last update: 2024-12-24 21:50:51 UTC


README

Build Status Coverage Status Latest Stable Version Total Downloads License

Log manager is package to handle multiple loggers. All write (send or something else) logic and message formatting must be implemented in these loggers. Log manager is only resolves which logger will be used and fires log action for corresponding logger.

Each logger must implement Psr\Log\LoggerInterface .

Installing

composer require opxcore/log-manager

Basic creating:

use OpxCore\Log\LogManager;

$manager = new LogManager($default, $loggers, $groups);

Creating with container

use OpxCore\Interfaces\LoggerInterface;
use OpxCore\Log\LogManager;

$container->bind(
    LoggerInterface::class,
    LogManager::class,
    [
        'default' => $default,
        'loggers' => $loggers,
        'groups' => $groups,
    ]
);

$manager = $container->make(LoggerInterface::class);

or

$container->bind(LoggerInterface::class, LogManager::class);

$manager = $container->make(LoggerInterface::class, [
        'default' => $default,
        'loggers' => $loggers,
        'groups' => $groups,
    ]);

Configuring and using

Configuration array consists of three keys. Value of 'default' must contain a name (or array of names) of logger to be used as default logger. 'loggers' is a set of loggers to be used keyed by name. A required parameter of each logger is a 'driver' containing class name of logger to be used with corresponding name (see examples below). Third additional key 'groups' contain array of logger groups keyed by name of group.

Log manager extends container, so loggers will be resolved by it with all dependency injections. All loggers will be resolved on demand and instanced for future use. All parameters except 'driver' will be passed to logger constructor as parameters.

Additionally, you can bind custom created logger:

$manager->bind('custom_logger', function() {
    return new Logger(...);
});

To get a certain logger call method $manager->logger($name) where $name is name of logger to be returned. If null given the default logger (or several loggers) will be used.

To get multiple log loggers use same method with an array of names $manager->logger([$name1, $name2])

In both cases LoggerProxy class with chosen loggers bindings will be returned, so $manager->logger([$name1, $name2])->log($message) will call log action on each of logger.

To use group logging use $manager->group($group) or $manager->group([$group1, $group2]). Each group will be resolved to set of loggers and then merged together, removing duplicates.

PSR-3

Log manager implements PSR-3. So available methods are:

$manager->emergency($message, $context);
$manager->alert($message, $context);
$manager->critical($message, $context);
$manager->error($message, $context);
$manager->warning($message, $context);
$manager->notice($message, $context);
$manager->info($message, $context);
$manager->debug($message, $context);
$manager->log($level, $message, $context);

These methods will call corresponding method of default logger(s).

Examples

Log manager configuration:

    $default = 'file'; // Also you can use 'default' => ['file', 'null'],    
    $loggers = [
        'file' => [
            'driver' => \OpxCore\Log\LogFile::class,
            'filename' => '/www/project/logs',
        ],
        'null' => [
            'driver' => \OpxCore\Log\LogNull::class,
        ]
    ];
    $groups = [
        'local' => ['file', 'null'],
        'network' => ['email'],
    ];

Logger class:

namespace \OpxCore\Log;

class LogFile implements \Psr\Log\LoggerInterface
{
    protected string $filename;
    
    public function __construct(string $filename)
    {
        $this->filename = $filename;
    }
    
    ...
}

Calling $manager->logger('file') at first time will create and return LogFile class instance (for this example is equal to new \OpxCore\Log\LogFile('/www/project/logs')) and store it's an instance for future use. So calling $manager->logger('file') for second time will return same instance of logger.

For this example using

$manager->logger('file')->debug('Some message');

is equal to (as 'file' set as default logger)

$manager->logger()->debug('Some message');

and equal to

$manager->debug('Some message');