2017 © Pedro Peláez
 

yii2-extension yii2-monolog

Monolog extension for Yii2

image

leinonen/yii2-monolog

Monolog extension for Yii2

  • Friday, April 27, 2018
  • by lordthorzonus
  • Repository
  • 2 Watchers
  • 6 Stars
  • 305 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 41 % Grown

The README.md

Latest Stable Version Total Downloads Latest Unstable Version License Scrutinizer Code Quality Code Coverage Build Status SensioLabsInsight, (*1)

Yii2 Monolog

Yii2 Monolog allows one to use Monolog easily with Yii2. For instructions regarding Monolog itself please refer to the documentation., (*2)

Table of contents

Installation

Require this package, with Composer, in the root directory of your project., (*3)

composer require leinonen/yii2-monolog

Configuration

Configure the leinonen\Yii2Monolog\Yii2Monolog as a bootstrapped component in your application config., (*4)

An example configuration of one log channel called myLoggerChannel with a basic StreamHandler and a UidProcessor:, (*5)

use leinonen\Yii2Monolog\MonologTarget;
use leinonen\Yii2Monolog\Yii2Monolog;
use Monolog\Handler\StreamHandler;
use Monolog\Processor\UidProcessor;

...
[
    'bootstrap' => ['monolog'],
    'components' => [
        ...
        'monolog' => [
            'class' => Yii2Monolog::class,
            'channels' => [
                'myLoggerChannel' => [
                    'handlers' => [
                        StreamHandler::class => [
                            'path' => '@app/runtime/logs/someLog.log',
                        ],
                    ],
                    'processors' => [
                        UidProcessor::class,
                    ],
                ],
            ],
        ],
    ],
    ...
]

Channels

To see the core concepts about Monolog channels check the Offical Documentation for Monolog., (*6)

This component allows registering multiple channels with channel name => configuration array key value pairs with the channels configuration key., (*7)

Main channel

The component automatically registers a main channel which is used when requesting Psr\Log\LoggerInterface from the DI container or when fetching a Logger from the component without specifying a channel name., (*8)

The main channel is configurable with the configuration key mainChannel, (*9)


[ 'components' => [ ... 'monolog' => [ 'class' => Yii2Monolog::class, 'channels' => [ 'myFirstChannel' => [ ... ], 'someOtherAwesomeChannel' => [ ... ], ], 'mainChannel' => 'someOtherAwesomeChannel' ] ] ]

If the main channel is null or not specified at all, the first channel from the channels list will be used as the main channel. With the example config above it would be myFirstChannel., (*10)

Handlers

The package supports all official and 3rd party handlers for Monolog. It uses leinonen\Yii2Monolog\CreationStrategies\ReflectionStrategy by default in background to figure out the config values which the handler is to be constructed with. The handlers are defined with a config key handlers in the Monolog configuration. All the handlers are resolved through Yii's DI container making it easier to implement your own custom handlers., (*11)

Example handler configuration with a stack of two handlers:, (*12)

[
    ...
        'monolog' => [
            'channels' => [
                'myLoggerChannel' => [
                    'handlers' => [
                        SlackbotHandler::class => [
                            'slackTeam' => 'myTeam',
                            'token' => 'mySecretSlackToken',
                            'channel' => 'myChannel',
                        ],
                        RotatingFileHandler::class => [
                            'path' => '@app/runtime/logs/myRotatinglog.log',
                            'maxFiles' => 10,
                        ],
                    ],
                ],
            ],
        ],
    ...
]

You can find the available handlers from the Monolog\Handler namespace, (*13)

Yii2 specific handlers

The package also provides a specific creation strategies for couple of handlers to help integrating with Yii2., (*14)

StreamHandler

The path config value is resolved through Yii's getAlias() method making it possible to use aliases such as @app in the config. Use this instead of stream, (*15)

RotatingFileHandler

The path config value is resolved through Yii's getAlias() method making it possible to use aliases such as @app in the config. Use this instead of filename, (*16)

Formatters

The package supports all official and 3rd party formatters for Monolog. It uses leinonen\Yii2Monolog\CreationStrategies\ReflectionStrategy by default in background to figure out the config values which the formatter is to be constructed with. All the formatters are resolved through Yii's DI container making it easier to implement your own custom formatters., (*17)

It's possible to configure a custom formatter for each handler. The formatter is configured with a formatter key in the handler's config array:, (*18)

'handlers' => [
    StreamHandler::class => [
        'path' => '@app/runtime/logs/myLog.log',
        'formatter' => [
            LineFormatter::class => [
                'format' => "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
                'dateFormat' => "Y-m-d\TH:i:sP"
            ]
        ]
    ]
]

You can find available formatters from the Monolog/Formatter namespace., (*19)

Processors

The package supports all official and 3rd party processors for Monolog. It uses leinonen\Yii2Monolog\CreationStrategies\ReflectionStrategy by default in background to figure out the config values which the processor is to be constructed with. All the processors are resolved through Yii's DI container making it easier to implement your own custom processors., (*20)

The processors can be defined globally for one target or specifically for a handler. Processors are configured with a processors key in the config array with an array of callables:, (*21)

[
    ...
        'monolog' => [
            'channels' => [
                'myLoggerChannel' => [
                    'processors' => [
                        GitProcessor::class,
                        function ($record) {
                            $record['myCustomData'] = 'test';

                            return $record;
                        },
                    ],
                ],
            ],
        ],
    ...
]

Or config to a specific handler:, (*22)

...
'handlers' => [
    StreamHandler::class => [
    'path' => '@app/runtime/logs/myLog.log',
    'processors' => [
        GitProcessor::class,
        function ($record) {
            $record['myCustomData'] = 'test';

            return $record;
        }
    ]
]

You can find available processors from the Monolog/Processor namespace., (*23)

Configuring Handlers/Formatters/Processors after creation

For further customisation it's possible to specify a configure key for all classes that are created with leinonen\Yii2Monolog\CreationStrategies\ReflectionStrategy . The configure key must be a callable which receives the created class instance and config. It also has to return the instance. It is called just after the class has been resolved from Yii's DI container. For example it's possible to customize Monolog\Handler\RotatingFileHandler's filename format:, (*24)

'handlers' => [
    RotatingFileHandler::class => [
        'path' => 'something',
        'maxFiles' => 2,
        'configure' => function (RotatingFileHandler $handler, $config) {
            $handler->setFilenameFormat('myprefix-{filename}-{date}', 'Y-m-d');

            return $handler;
        }
    ],
]

Usage

Using the component as a Yii's log target

If you want to integrate this component into an existing project which utilizes Yii's own logger, you can configure channels as log targets easily. See Yii's documentation about log targets here., (*25)

Example configuration:, (*26)

use leinonen\Yii2Monolog\MonologTarget;
use leinonen\Yii2Monolog\Yii2Monolog;

[
    'bootstrap' => ['monolog', 'log'],
    'components' => [
        ...
        'monolog' => [
            'class' => Yii2Monolog::class,
            'channels' => [
                'myFirstChannel' => [
                    ...
                ],
                'someOtherAwesomeChannel' => [
                    ...
                ],
            ],
            'mainChannel' => 'someOtherAwesomeChannel'
        ],
        'log' => [
            'targets' => [
                [
                    'class' => MonologTarget::class,
                    'channel' => 'myFirstChannel',
                    'levels' => ['error', 'warning']
                ],
            ]
        ]
    ]
]

In this case all the Yii's loggers messages will go through the handler / processor stack of myFirstChannel logger without touching any of your existing code., (*27)

\Yii::warning('hello');
\Yii::error('world!');

If you leave the channel configuration out the target will use the main channel configured in the component., (*28)

Using the component standalone

If you want to not use Yii's logger at all it's possible to use this component as a completely standalone logger., (*29)

Fetching a specific logger from the component:, (*30)

 $myChannelLogger = Yii::$app->monolog->getLogger('myChannel');
 $myChannelLogger->critical('help me!');

 $mainChannelLogger = Yii::$app->monolog->getLogger();
 $mainChannelLogger->notice('This was a log message through the main channel');

As the main channel is registered as the implementation of the Psr\Log\LoggerInterface you can also use constructor injection in your controllers:, (*31)

class SuperController
{
    private $logger;

    public function __construct($id, $module, LoggerInterface $logger, $config = [])
    {
        $this->logger = $logger;
        parent::__construct($id, $module, $config);
    }

    public function actionExample()
    {
        $this->logger->notice('Action Example was called');
    }
}

The Versions

27/04 2018

dev-master

9999999-dev

Monolog extension for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Juuso Leinonen

27/04 2018

1.0.1

1.0.1.0

Monolog extension for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Juuso Leinonen

27/04 2018

dev-analysis-zRKNQx

dev-analysis-zRKNQx

Monolog extension for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Juuso Leinonen

14/02 2018

1.0.0

1.0.0.0

Monolog extension for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Juuso Leinonen