Quantcast
Channel: Live News for Yii Framework
Viewing all articles
Browse latest Browse all 3361

[extension] samdark/yii2-psr-log-target

$
0
0

Yii 2 PSR Log Target

  1. Installation
  2. Usage
  3. Running tests

Allows you to process logs using any PSR-3 compatible logger such as Monolog.

<img src="https://travis-ci.org/samdark/yii2-psr-log-target.svg" />

Installation

composer require "samdark/yii2-psr-log-target"

Usage

In order to use PsrTarget you should configure your log application component like the following:

// $psrLogger should be an instance of PSR-3 compatible logger.
// As an example, we'll use Monolog to send log to Slack.
$psrLogger = new \Monolog\Logger('my_logger');
$psrLogger->pushHandler(new \Monolog\Handler\SlackHandler('slack_token', 'logs', null, true, null, \Monolog\Logger::DEBUG));

return [
    // ...
    'bootstrap' => ['log'],    
    // ...    
    'components' => [
        // ...        
        'log' => [
            'targets' => [
                [
                    'class' => 'samdark\log\PsrTarget',
                    'logger' => $psrLogger,
                    
                    // It is optional parameter. The message levels that this target is interested in.
                    // The parameter can be an array.
                    'levels' => ['info', yii\log\Logger::LEVEL_WARNING, Psr\Log\LogLevel::CRITICAL],
                ],
                // ...
            ],
        ],
    ],
];

Standard usage:

Yii::info('Info message');
Yii::error('Error message');

Usage with PSR logger levels:

Yii::getLogger()->log('Critical message', Psr\Log\LogLevel::CRITICAL);
Yii::getLogger()->log('Alert message', Psr\Log\LogLevel::ALERT);

Running tests

In order to run tests perform the following commands:

composer install
./vendor/bin/phpunit

Viewing all articles
Browse latest Browse all 3361

Trending Articles