开源书籍 / yii2-monolog
Yii框架的Monolog集成。
0.2.2
2017-06-06 09:44 UTC
Requires
- monolog/monolog: ^1.20
- yiisoft/yii2: *
Requires (Dev)
- phpunit/phpunit: ~4.5
- phpunit/phpunit-mock-objects: 2.3.0
- satooshi/php-coveralls: ~1.0
- yiisoft/yii2-mongodb: ^2.1
Suggests
- yiisoft/yii2-mongodb: Allow sending log messages to a MongoDB server using Yii2 connection
README
Yii框架的Monolog集成。
要求
- PHP 5.4或更高版本
- Yii 2.0.0或更高版本
使用composer安装
- 打开您的项目目录;
- 运行
composer require sbooker/yii2-monolog
以将Monolog添加到项目的vendor目录。
配置
在Yii2中配置Monolog组件,请参考以下示例结构。组件中必须包含"main"通道,并且在没有设置通道时使用日志记录器。
return [ //.... 'components' => [ 'monolog' => [ 'class' => '\Mero\Monolog\MonologComponent', 'handlers' => [ 'main' => [ 'type' => 'stream', 'path' => '@app/runtime/logs/main.log', 'level' => 'debug', 'channels' => [ 'main', ] ], ], 'processor' = [ 'logProcessor', // id in ServiceLocator or Container ] ], ], //.... ];
您可以配置多个通道以及为每个通道配置不同的处理器和处理器。
处理器
您可以使用数组结构或对象结构在通道中添加处理器。
数组结构
使用数组结构,您可以更好地阅读其处理器的配置。
示例
return [ //... 'handler' => [ [ 'type' => 'stream', 'path' => '@app/runtime/logs/log_' . date('Y-m-d') . '.log', 'level' => 'debug' ] ], //... ];
警告:此选项在Monolog中没有现有处理器。有关更多详细信息,请参阅处理器页面。
对象结构
使用对象结构,您将通知已声明并已实例化其相应处理器的对象。
示例
return [ //... 'handler' => [ new \Monolog\Handler\StreamHandler( __DIR__.'/../runtime/logs/system.log', \Monolog\Logger::DEBUG ) ], //... ];
使用Yii2 Monolog
要使用Yii 2 Monolog,只需调用组件的getLogger
方法,并告知使用哪个通道。
示例
namespace app\controllers; use Yii; use \yii\web\Controller; class ExampleController extends Controller { /** * This action register "Hello world" in channel * "main"(default when no value is setted on "getLogger" method). */ public function actionFirstExample() { $monologComponent = Yii::$app->monolog; $logger = $monologComponent->getLogger(); $logger->log('info', 'Hello world'); } /** * This action register "Hello world" in channel "channel1". */ public function actionSecondExample() { $monologComponent = Yii::$app->monolog; $logger = $monologComponent->getLogger("channel1"); $logger->log('info', 'Hello world'); } }