Monolog Factory that allows configuration-based creation of Logger objects.
In addition to the generic factory, this package features one to be used with PSR-11 dependency injection containers.
The preferred method of installation is via Composer. Run the following command to install
the latest version of a package and add it to your project's composer.json
:
composer require nikolaposa/monolog-factory
Generic factory
use Monolog\Formatter\HtmlFormatter;
use Monolog\Handler\NativeMailerHandler;
use Monolog\Logger;
use Monolog\Processor\PsrLogMessageProcessor;
use MonologFactory\LoggerFactory;
$loggerFactory = new LoggerFactory();
$logger = $loggerFactory->create('my_logger', [
'handlers' => [
[
'name' => NativeMailerHandler::class,
'params' => [
'to' => '[email protected]',
'subject' => 'Test',
'from' => '[email protected]',
'level' => Logger::ALERT,
],
'formatter' => [
'name' => HtmlFormatter::class,
],
],
],
'processors' => [
[
'name' => PsrLogMessageProcessor::class,
],
],
]);
DI container factory configuration
use Monolog\Formatter\HtmlFormatter;
use Monolog\Handler\BufferHandler;
use Monolog\Handler\NativeMailerHandler;
use Monolog\Logger;
use Monolog\Processor\PsrLogMessageProcessor;
use MonologFactory\DiContainerLoggerFactory;
return [
'logger' => [
'logger1' => [
'name' => 'logger1',
'handlers' => [
[
'name' => NativeMailerHandler::class,
'params' => [
'to' => '[email protected]',
'subject' => 'Test',
'from' => '[email protected]',
'level' => Logger::ALERT,
],
'formatter' => [
'name' => HtmlFormatter::class,
],
],
],
'processors' => [
[
'name' => PsrLogMessageProcessor::class,
],
],
],
'logger2' => [
'name' => 'logger2',
'handlers' => [
[
'name' => BufferHandler::class,
'params' => [
'handler' => [
'__class__' => NativeMailerHandler::class,
'to' => '[email protected]',
'subject' => 'Test',
'from' => '[email protected]',
],
'buffer_limit' => 5,
],
'processors' => [
[
'name' => MemoryUsageProcessor::class,
],
],
],
],
'processors' => [
[
'name' => PsrLogMessageProcessor::class,
],
],
],
],
'di' => [
'factories' => [
'Logger1' => new DiContainerLoggerFactory('logger1'),
//... or more preferred/optimal way:
'Logger2' => [DiContainerLoggerFactory::class, 'logger2'],
],
],
];
See more examples.
Released under MIT License - see the License File for details.