2017 © Pedro Peláez
 

yii2-extension yii2-psr-log-target

Yii 2 log target which uses PSR-3 compatible logger

image

samdark/yii2-psr-log-target

Yii 2 log target which uses PSR-3 compatible logger

  • Monday, December 18, 2017
  • by samdark
  • Repository
  • 7 Watchers
  • 36 Stars
  • 4,389 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 7 Versions
  • 47 % Grown

The README.md

Yii 2 PSR Log Target

Allows you to process logs using any PSR-3 compatible logger such as Monolog., (*1)

Latest Stable Version Total Downloads Build Status Code Coverage Scrutinizer Quality Score, (*2)

Installation

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

Usage

In order to use PsrTarget you should configure your log application component like the following:, (*3)

// $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],
                    // It is optional parameter. Default value is false. If you use Yii log buffering, you see buffer write time, and not real timestamp.
                    // If you want write real time to logs, you can set addTimestampToContext as true and use timestamp from log event context.
                    'addTimestampToContext' => true,
                ],
                // ...
            ],
        ],
    ],
];

Standard usage:, (*4)

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

Usage with PSR logger levels:, (*5)

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

Usage with original timestamp from context in the log:, (*6)

// $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->pushProcessor(function($record) {
    if (isset($record['context']['timestamp'])) {
        $dateTime = DateTime::createFromFormat('U.u', $record['context']['timestamp']);
        $timeZone = $record['datetime']->getTimezone();
        $dateTime->setTimezone($timeZone);
        $record['datetime'] = $dateTime;

        unset($record['context']['timestamp']);
    }

    return $record;
});

You can use PsrMessage instead of regular string messages to add custom context., (*7)

Standard usage:, (*8)

Yii::error(new \samdark\log\PsrMessage("Critical message", [
    'custom' => 'context',
    'key' => 'value',
]));

Usage with PSR logger Levels:, (*9)

Yii::getLogger()->log(new \samdark\log\PsrMessage("Critical message", [
    'important' => 'context'
]), Psr\Log\LogLevel::CRITICAL);

Usage with PSR-3 log message processing:, (*10)

$psrLogger = new \Monolog\Logger('my_logger');
$psrLogger->pushProcessor(new \Monolog\Processor\PsrLogMessageProcessor());
Yii::debug(new \samdark\log\PsrMessage("Greetings from {fruit}", [
    'fruit' => 'banana'
]));

Running tests

In order to run tests perform the following commands:, (*11)

composer install
./vendor/bin/phpunit

The Versions

18/12 2017

dev-master

9999999-dev https://github.com/samdark/yii2-psr-log-target

Yii 2 log target which uses PSR-3 compatible logger

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

log psr-3 extension yii

18/12 2017

1.0.5

1.0.5.0 https://github.com/samdark/yii2-psr-log-target

Yii 2 log target which uses PSR-3 compatible logger

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

log psr-3 extension yii

13/12 2017

1.0.4

1.0.4.0 https://github.com/samdark/yii2-psr-log-target

Yii 2 log target which uses PSR-3 compatible logger

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

log psr-3 extension yii

14/05 2017

1.0.3

1.0.3.0 https://github.com/samdark/yii2-psr-log-target

Yii 2 log target which uses PSR-3 compatible logger

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

log psr-3 extension yii

16/03 2017

1.0.2

1.0.2.0 https://github.com/samdark/yii2-psr-log-target

Yii 2 log target which uses PSR-3 compatible logger

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

log psr-3 extension yii

09/03 2017

1.0.1

1.0.1.0 https://github.com/samdark/yii2-psr-log-target

Yii 2 log target which uses PSR-3 compatible logger

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

log psr-3 extension yii

09/03 2017

1.0.0

1.0.0.0 https://github.com/samdark/yii2-psr-log-target

Yii 2 log target which uses PSR-3 compatible logger

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

log psr-3 extension yii