PHP code example of leinonen / yii2-monolog

1. Go to this page and download the library: Download leinonen/yii2-monolog library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

leinonen / yii2-monolog example snippets


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,
                    ],
                ],
            ],
        ],
    ],
    ...
]


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

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

'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"
            ]
        ]
    ]
]

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

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

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

            return $record;
        }
    ]
]

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

            return $handler;
        }
    ],
]

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']
                ],
            ]
        ]
    ]
]

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

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

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