alexeevdv / yii2-psr-log-adapter
Installs: 9 056
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- psr/log: ^1.0
- yiisoft/yii2: ^2.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-10-20 07:37:10 UTC
README
Yii2 logger is not PSR3 compatible, therefore when you need logger functionality in third party library (which uses PSR3 logger interface), this package may save your time.
Installation
The preferred way to install this extension is through composer.
Either run
$ php composer.phar require alexeevdv/yii2-psr-log-adapter
or add
"alexeevdv/yii2-psr-log-adapter": "^1.0"
to the require
section of your composer.json
file.
Usage
Lets assume some third party code
use Psr\Log\LoggerInterface; class ThirdParty { private $logger; function __construct(LoggerInterface $logger) { $this->logger = $logger; } }
Create adapter implicitly
use alexeevdv\yii\PsrLoggerAdapter; $logger = new PsrLoggerAdapter(['category' => 'my-category']); $thirdParty = new ThirdParty($logger);
Transparent usage via DI container
// Yii application config [ //... 'container' => [ 'definitions' => [ \Psr\Log\LoggerInterface::class => [ 'class' => \alexeevdv\yii\PsrLoggerAdapter::class, 'category' => 'my-category', ], ], ], //... ] // Lest create third party object now // Logger adapter will be injected automagically $thirdParty = Yii::createObject(ThirdParty::class);
Configuration
By default yii logger is taken from DI container but you can specify your own if you wish.
use alexeevdv\yii\PsrLoggerAdapter; $logger = new PsrLoggerAdapter([ 'logger' => 'mylogger', // logger configuration here. Anything that can be passed to \yii\di\Instance::ensure 'category' => 'my-category', ]);