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 to be used must implements Psr\Log\LoggerInterface.
composer require opxcore/log-manager
use OpxCore\Log\LogManager;
$manager = new LogManager($config);
Creating with container
use OpxCore\Interfaces\LoggerInterface;
use OpxCore\Log\LogManager;
$container->bind(
LoggerInterface::class,
LogManager::class,
['config' => $config],
);
$manager = $container->make(LoggerInterface::class);
or
$container->bind(LoggerInterface::class, LogManager::class);
$manager = $container->make(LoggerInterface::class, ['config' => $config]);
Where $config is configuration for log manager (see below).
Configuration array consists of two keys. Value of 'default'
must contain 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. 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 driver
groups keyed by name.
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 log driver call method
$manager->driver($name)
where $name
is name of logger to be returned. If null
given driver set as default will be used.
To get multiple log drivers use same method with array of names
$manager->driver([$name1, $name2])
In both cases LoggerProxy
class with chosen loggers bindings will be returned,
so $manager->driver([$name1, $name2])->log($message)
will call log action on
each of logger.
To use group logging use $manager->group($name)
or $manager->group([$name1, $name2])
.
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 log driver set as default.
Log manager configuration:
$config = [
'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 $filename;
public function __create($filename)
{
$this->filename = $filename;
}
...
}
Calling $manager->driver('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 instance for future use. So calling $manager->driver('file')
for
second time will return same instance of logger.
For this example using
$manager->driver('file')->debug('Some message');
is equal to (as 'file'
set as default driver)
$manager->driver()->debug('Some message');
and equal to
$manager->debug('Some message');