nikolaposa / monolog-factory
Configuration-based Monolog factory
Installs: 160 571
Dependents: 4
Suggesters: 0
Security: 0
Stars: 24
Watchers: 3
Forks: 4
Open Issues: 0
Requires
- php: ^7.2 || ^8.0
- beberlei/assert: ^3.2
- monolog/monolog: ^2.0
- nikolaposa/cascader: ^1.3
- psr/container: ^1.0 || ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.7
- phpunit/phpunit: ^8.0 || ^9.4
- rollbar/rollbar: ^2.0
README
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.
Installation
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
Usage
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' => 'test@example.com', 'subject' => 'Test', 'from' => 'noreply@example.com', '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' => 'test@example.com', 'subject' => 'Test', 'from' => 'noreply@example.com', 'level' => Logger::ALERT, ], 'formatter' => [ 'name' => HtmlFormatter::class, ], ], ], 'processors' => [ [ 'name' => PsrLogMessageProcessor::class, ], ], ], 'logger2' => [ 'name' => 'logger2', 'handlers' => [ [ 'name' => BufferHandler::class, 'params' => [ 'handler' => [ '__class__' => NativeMailerHandler::class, 'to' => 'test@example.com', 'subject' => 'Test', 'from' => 'noreply@example.com', ], '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.
Credits
License
Released under MIT License - see the License File for details.