prowebcraft/yii2-log-helper

Base helper class for better logging experience. Send messages to telegram

dev-main 2023-11-24 15:17 UTC

This package is auto-updated.

Last update: 2024-10-24 17:12:41 UTC


README

Yii2 Log Helper

General usage

Attach \prowebcraft\yii2log\trait\Log trait to your class or extend it from \prowebcraft\yii2log\Log

Call static debug, info, warning or error method to log some info, for example:

<?php

use \prowebcraft\yii2log\trait\Log;

class MarsExpedition {

    use Log;
    
    public function doHeavyJob(){
        self::info('Prepare to launch');
        $missionInfo = [
            'title' => 'Mission to Mars',
            'distance' => '54.6m km',
            'pilot' => 'Elon Musk',
        ];
        self::debug('Flight data: %s', $missionInfo);
        try {
            // mission inpossible            
        } catch($e) {
            self::error('Error flying to Mars: %s', $e);
        }
    }
}

Configuration

Send messages to telegram

To send error/warning messages to telegram add component and log target to your config (ex. common/main-local.php):

    'components' => [
        // ...
        'telegram' => [
            'class' => \prowebcraft\yii2log\TelegramBot::class,
            'token' => '123:xyz' // telegram bot token,
            'defaultChatId' => -1000000000, // group or channel id,
            'targetPerCategory' => [
                'mission_control' => -20000000
            ],
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => \prowebcraft\yii2log\TelegramTarget::class,
                    'levels' => ['error', 'warning'], // log levels
                    'logVars' => []
                ]
            ],
        ],
        // ...
    ],
];

Output messages to console

To output messages to console in Console applications log target to your config (ex. console/main-local.php):

    'components' => [,
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'flushInterval' => 1,
            'targets' => [
                [
                    'class' => \prowebcraft\yii2log\ConsoleTarget::class,
                    'levels' => ['error', 'warning', 'info', 'trace'],
                    'logVars' => [],
                    'displayCategory' => true,
                    'except' => [
                        'yii\*'
                    ],
                    'exportInterval' => 1,
                ]
            ],
        ],
    ],
];