alexeevdv / yii2-psr-log-adapter
1.0.1
2019-03-19 17:51 UTC
Requires
- psr/log: ^1.0
- yiisoft/yii2: ^2.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-20 07:33:18 UTC
README
由于Yii2日志记录器不兼容PSR3,因此当您需要在第三方库(使用PSR3日志接口)中实现日志记录功能时,此包可能可以节省您的时间。
安装
安装此扩展的首选方式是通过 Composer。
运行以下命令之一
$ php composer.phar require alexeevdv/yii2-psr-log-adapter
或者
"alexeevdv/yii2-psr-log-adapter": "^1.0"
将以下内容添加到您的 composer.json
文件的 require
部分。
使用方法
假设一些第三方代码
use Psr\Log\LoggerInterface; class ThirdParty { private $logger; function __construct(LoggerInterface $logger) { $this->logger = $logger; } }
隐式创建适配器
use alexeevdv\yii\PsrLoggerAdapter; $logger = new PsrLoggerAdapter(['category' => 'my-category']); $thirdParty = new ThirdParty($logger);
通过依赖注入容器透明使用
// 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);
配置
默认情况下,从DI容器中获取yii日志记录器,但您可以根据需要指定自己的。
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', ]);