prowebcraft/yii2-log-helper

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

Maintainers

Package info

github.com/prowebcraft/yii2-log-helper

Type:yii2-extension

pkg:composer/prowebcraft/yii2-log-helper

Statistics

Installs: 1 402

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

dev-main 2025-01-11 11:01 UTC

This package is auto-updated.

Last update: 2026-03-11 13:53:07 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,
                ]
            ],
        ],
    ],
];