A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/merorafael/yii2-monolog below:

merorafael/yii2-monolog: Yii 2 Monolog extension

The Monolog integration for the Yii framework.

Instalation with composer
  1. Open your project directory;
  2. Run composer require mero/yii2-monolog to add Monolog in your project vendor.

To configure Monolog component in Yii2, use the structure exemplified below. The channel "main" is required in component and used when no channel is setted to use a logger.

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

You can configure multiple channels and different handlers and processors for each channel.

Example

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' => [],
            ],
        ],
    //...
];

You can add handlers in their channels using the array structure or object structure.

Using the array structure, you have a better readability of the configuration of their handlers.

Example

return [
    //...
    'handler' => [
        [
            'type' => 'stream',
            'path' => '@app/runtime/logs/log_' . date('Y-m-d') . '.log',
            'level' => 'debug'
        ]
    ],
    //...
];

Warning: this option does not have any existing handlers in monolog. See the handlers page more details.

Using the object structure, you will be informing instantiated objects already declared their respective handlers.

Example

return [
    //...
    'handler' => [
        new \Monolog\Handler\StreamHandler(
            __DIR__.'/../runtime/logs/system.log',
            \Monolog\Logger::DEBUG
        )
    ],
    //...
];

To use Yii 2 Monolog simply call the method getLogger component informing which channel is used.

Example

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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4