prowebcraft / yii2-log-helper
更好的日志体验的基础辅助类。发送消息到telegram
dev-main
2023-11-24 15:17 UTC
Requires
- php: >=7.4.0
- yiisoft/yii2-httpclient: ~2.0
This package is auto-updated.
Last update: 2024-08-24 16:45:23 UTC
README
Yii2 Log Helper
通用用法
将 \prowebcraft\yii2log\trait\Log 特性附加到您的类或从 \prowebcraft\yii2log\Log 扩展它
调用静态 debug
、info
、warning
或 error
方法来记录一些信息,例如
<?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); } } }
配置
发送消息到telegram
要将错误/警告消息发送到telegram,请在配置中添加组件和日志目标(例如 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' => [] ] ], ], // ... ], ];
输出消息到控制台
要将消息输出到控制台,请在控制台应用程序的配置中添加日志目标(例如 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, ] ], ], ], ];