denoby/laravel-telegram-logger
Composer 安装命令:
composer require denoby/laravel-telegram-logger
包简介
Laravel package for sending logs and messages to Telegram
README 文档
README
A Laravel package for sending application logs and messages to Telegram.
Features
- Send Laravel logs to Telegram chat/group/channel
- Support for Telegram topics (threads)
- Fluent message builder with photo/video support
- Configurable exception filtering
- User info in log messages
- Markdown formatting
Requirements
- PHP 8.1+
- Laravel 10.x, 11.x, or 12.x
Installation
composer require denoby/laravel-telegram-logger
The package will auto-register its service provider.
Publish Configuration
php artisan vendor:publish --provider="DenoBY\TelegramLogger\ServiceProvider"
Configuration
Add the following to your .env file:
TELEGRAM_LOG_TOKEN=your-bot-token TELEGRAM_LOG_CHAT_ID=your-chat-id TELEGRAM_LOG_THREAD_ID=optional-thread-id
Getting Your Bot Token
- Open Telegram and search for @BotFather
- Send
/newbotand follow the instructions - Copy the token provided
Getting Your Chat ID
- Add your bot to the group/channel where you want to receive logs
- Send a message to the group
- Visit
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates - Find the
chat.idin the response
Usage
As a Log Channel
Add the Telegram channel to your config/logging.php:
'channels' => [ // ... other channels 'telegram' => [ 'driver' => 'custom', 'via' => \DenoBY\TelegramLogger\Logger::class, 'level' => env('LOG_LEVEL', 'error'), ], ],
You can add it to your stack:
'stack' => [ 'driver' => 'stack', 'channels' => ['daily', 'telegram'], 'ignore_exceptions' => false, ],
Or use it directly:
Log::channel('telegram')->error('Something went wrong!');
As a Tap (Adding to Existing Channels)
You can add Telegram logging to any existing channel using the tap option. This sends logs to both the original channel AND Telegram:
'daily' => [ 'driver' => 'daily', 'path' => storage_path('logs/laravel.log'), 'level' => 'debug', 'tap' => [\DenoBY\TelegramLogger\Tap::class], ],
The Tap class uses the telegram-logger.level config value (default: error) to filter which logs are sent to Telegram. You can set it via environment variable:
TELEGRAM_LOG_LEVEL=error
Per-channel Overrides (thread_id and level)
You can override the global TELEGRAM_LOG_THREAD_ID and log level on a per-channel basis. This is useful when you want different log categories to go to different topics of the same supergroup.
Through Logger (custom channel):
'channels' => [ 'tg_payments' => [ 'driver' => 'custom', 'via' => \DenoBY\TelegramLogger\Logger::class, 'level' => 'info', 'thread_id' => env('TELEGRAM_PAYMENTS_THREAD_ID'), ], 'tg_queues' => [ 'driver' => 'custom', 'via' => \DenoBY\TelegramLogger\Logger::class, 'level' => 'warning', 'thread_id' => env('TELEGRAM_QUEUES_THREAD_ID'), ], ],
Through Tap (positional arguments thread_id,level):
// both overridden 'tap' => [\DenoBY\TelegramLogger\Tap::class.':42,warning'], // only thread_id (level falls back to global) 'tap' => [\DenoBY\TelegramLogger\Tap::class.':42'], // only level (thread_id falls back to global) 'tap' => [\DenoBY\TelegramLogger\Tap::class.':,warning'], // no overrides — uses global config 'tap' => [\DenoBY\TelegramLogger\Tap::class],
If a channel does not set an override, the global TELEGRAM_LOG_THREAD_ID / TELEGRAM_LOG_LEVEL from .env is used.
Sending Messages Directly
Use the Facade to send messages:
use DenoBY\TelegramLogger\Facades\Telegram; use DenoBY\TelegramLogger\Message; // Simple message Telegram::sendMessageToChat( new Message('Hello from Laravel!'), config('telegram-logger.chat_id') ); // With topic (thread) Telegram::sendMessageToChat( new Message('Hello!'), config('telegram-logger.chat_id'), threadId: config('telegram-logger.thread_id') );
Building Messages
use DenoBY\TelegramLogger\Message; // Create message with photos $message = (new Message('Check out these photos!')) ->addPhoto('https://example.com/image1.jpg') ->addPhoto('https://example.com/image2.jpg'); // Append/prepend text $message = (new Message('Main content')) ->prependText('Header:') ->appendText('Footer');
Sending Documents
use DenoBY\TelegramLogger\Facades\Telegram; Telegram::sendDocument( filePath: '/path/to/document.pdf', chatId: config('telegram-logger.chat_id'), caption: 'Here is your document', threadId: config('telegram-logger.thread_id') );
Configuration Options
// config/telegram-logger.php return [ // Bot token from @BotFather 'token' => env('TELEGRAM_LOG_TOKEN'), // Chat/Group/Channel ID 'chat_id' => env('TELEGRAM_LOG_CHAT_ID'), // Topic ID (for supergroups with topics) 'thread_id' => env('TELEGRAM_LOG_THREAD_ID'), // App info for log messages 'app_name' => env('APP_NAME', 'Laravel'), 'app_url' => env('APP_URL'), 'environment' => env('APP_ENV', 'production'), 'deploy_branch' => env('DEPLOY_BRANCH'), // Exceptions to ignore completely 'ignore_exceptions' => [ // \Symfony\Component\Mailer\Exception\TransportException::class, ], // Exceptions to log without stack trace 'ignore_stack_trace_for' => [ // \App\Exceptions\CustomException::class, ], // Maximum message length (Telegram limit is 4096) 'max_message_length' => 3040, // Include authenticated user info 'include_user_info' => true, // HTTP timeout in seconds 'timeout' => 5, // Minimum log level for TelegramTap 'level' => env('TELEGRAM_LOG_LEVEL', 'error'), ];
Log Message Format
Log messages are formatted in Markdown with the following information:
- App URL - Clickable link to your application
- Environment - Current environment (production, staging, etc.)
- Log Level - As a hashtag (#ERROR, #WARNING, etc.)
- Branch - Deploy branch (if configured)
- User ID - Authenticated user info (if enabled)
- File - File and line number where the exception occurred
- Message - The log message
- Stack Trace - Filtered to show only app-related traces
License
The MIT License (MIT). Please see License File for more information.
denoby/laravel-telegram-logger 适用场景与选型建议
denoby/laravel-telegram-logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 818 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「logging」 「monolog」 「logger」 「laravel」 「telegram」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 denoby/laravel-telegram-logger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 denoby/laravel-telegram-logger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 denoby/laravel-telegram-logger 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A GeoIP decorator/processor for monolog
A Zend Framework module that sets up Monolog for logging in applications.
Laravel 5.6 Monolog custom telegram channel
Workflow logger
HTTP request logger middleware for Laravel
A Monolog processor to add X-Request-Id or UNIQUE_ID env to monolog stack if present, provide a default one otherwise. Optionally, add a forwarded request-id list.
统计信息
- 总下载量: 818
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-28