congnv / monolog-telegram-handler
Monolog 的 Telegram 处理器。
1.0.3
2024-07-01 10:16 UTC
Requires
- php: >=8.1
- ext-curl: *
- monolog/monolog: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.54
- phpstan/phpstan: ^1.10.67
README
Monolog 的 Telegram 处理器。
安装
composer require congnv/monolog-telegram-handler
声明处理器对象
要声明此处理器,您需要知道要发送日志的机器人令牌和聊天标识符(chat_id)。
$handler = new \TelegramLog\TelegramHandler('<token>', '<chat_id>');
与 Laravel 一起使用
将 Telegram 频道添加到 config/logging.php
'telegram' => [ 'driver' => 'custom', 'via' => \TelegramLog\TelegramLogger::class, 'level' => env('LOG_LEVEL', 'debug'), 'bot_token' => env('TELEGRAM_BOT_TOKEN'), 'chat_id' => env('TELEGRAM_CHAT_ID'), // 'max_stack_line' => 10, ],
自定义消息格式化器
创建新的类实现 \TelegramLog\TelegramMessageFormatterInterface
示例
<?php namespace App\Logging\Formatter; use Monolog\LogRecord; use TelegramLog\TelegramMessageFormatterInterface; class TelegramMessageFormatter implements TelegramMessageFormatterInterface { public function format(LogRecord $record, int $maxStackLine): string { return [{$record->datetime->format('Y-m-d H:i:s')}] {$record->message}"; } }
修改 Telegram 频道
'telegram' => [ 'driver' => 'custom', 'via' => \TelegramLog\TelegramLogger::class, 'formatter' => \App\Logging\Formatter\TelegramMessageFormatter::class, 'level' => env('LOG_LEVEL', 'debug'), 'bot_token' => env('TELEGRAM_BOT_TOKEN'), 'chat_id' => env('TELEGRAM_CHAT_ID'), // 'max_stack_line' => 10, ],