mero/yii2-monolog

Yii 框架的 Monolog 集成。

安装次数: 159,844

依赖项: 0

建议者: 0

安全性: 0

星标: 41

关注者: 5

分支: 20

公开问题: 1

类型:yii2-extension

0.1.2 2016-10-12 14:39 UTC

This package is auto-updated.

Last update: 2024-09-17 05:37:31 UTC


README

SensioLabsInsight Build Status Coverage Status Latest Stable Version Total Downloads License

这是 Yii 框架的 Monolog 集成。

要求

  • PHP 5.4 或更高版本
  • Yii 2.0.0 或更高版本

使用 composer 安装

  1. 打开您的项目目录;
  2. 运行 composer require mero/yii2-monolog 以将 Monolog 添加到您的项目 vendor 中。

配置

在 Yii2 中配置 Monolog 组件,请使用以下示例结构。组件中需要 "main" 通道,当未设置通道以使用记录器时,将使用该通道。

return [
    //....
    'components' => [
        'monolog' => [
            'class' => '\Mero\Monolog\MonologComponent',
            'channels' => [
                'main' => [
                    'handler' => [
                        //Handler object or array configuration
                    ],
                    'processor' => [],
                ],
            ],
        ],
    ],
    //....
];

您可以配置多个通道,并为每个通道配置不同的处理器和处理程序。

示例

return [
    //...
        'channels' => [
            'main' => [
                'handler' => [
                    [
                        'type' => 'stream',
                        'path' => '@app/runtime/logs/main_' . date('Y-m-d') . '.log',
                        'level' => 'debug'
                    ]
                ],
                'processor' => [],
            ],
            'channel1' => [
                'handler' => [
                    [
                        'type' => 'stream',
                        'path' => '@app/runtime/logs/channel1_' . date('Y-m-d') . '.log',
                        'level' => 'debug'
                    ]
                ],
                'processor' => [],
            ],
            'channel2' => [
                'handler' => [
                    [
                        'type' => 'stream',
                        'path' => '@app/runtime/logs/channel1_' . date('Y-m-d') . '.log',
                        'level' => 'debug'
                    ]
                ],
                'processor' => [],
            ],
        ],
    //...
];

处理器

您可以使用数组结构或对象结构在通道中添加处理器。

数组结构

使用数组结构,您可以更好地阅读其处理器的配置。

示例

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');
    }
    
}