uzhlaravel / telegramlogs
Monitor your Laravel application logs in real-time through Telegram. This package delivers instant notifications of critical events directly to your Telegram channel, with support for threaded discussions and Markdown formatting.
Fund package maintenance!
uzhlaravel
Requires
- php: ^8.2
- illuminate/contracts: ^10.0||^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.6
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.1
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/phpstan: ^2.1
README
A comprehensive Laravel package that seamlessly integrates Telegram notifications into your application's logging system. Monitor critical events, debug issues, and stay informed about your application's health in real-time through Telegram channels.
Key Features
Core Functionality
- Monolog Integration - Native Laravel logging channel support
- Direct Messaging API - Send custom messages programmatically
- Real-time Notifications - Instant delivery to Telegram channels/groups
- Automatic Installation - One-command setup with guided configuration
- Configuration Testing - Built-in commands to verify your setup
Advanced Capabilities
- Multi-level Logging - Support for all PSR-3 log levels (debug to emergency)
- Forum Groups Support - Send messages to specific topics/threads
- Message Formatting - MarkdownV2 and HTML support with JSON pretty-printing
- Smart Message Handling - Automatic splitting for messages exceeding Telegram's limits
- Error Resilience - Graceful fallback when Telegram is unavailable
- Performance Optimized - Configurable timeouts and connection pooling
Prerequisites
Ensure the following requirements are met before installation:
- Telegram Bot Token: Obtain from BotFather
- Telegram Channel/Group Chat ID: Destination for log notifications
- Laravel Framework: Version 8.0 or higher
Installation
Integrate the package via Composer:
composer require uzhlaravel/telegramlogs
Automated Installation Procedure
Execute the installation command for guided configuration:
php artisan telegramlogs:install
This automated process will:
- Deploy the configuration file
- Guide through environment variable setup
- Optionally configure Telegram as the default logging channel
- Validate configuration integrity
Manual Installation Process
Publish the configuration file manually:
php artisan vendor:publish --tag="telegramlogs-config"
Configuration
Automated Configuration
Execute the installation command for interactive setup:
php artisan telegramlogs:install
Manual Environment Configuration
Update your .env
file with the following parameters:
TELEGRAM_BOT_TOKEN=your_bot_token_here TELEGRAM_CHAT_ID=your_chat_id_here # Optional configuration parameters: TELEGRAM_TOPIC_ID=your_thread_id_here TELEGRAM_TOPIC_MESSAGE_ID=your_message_id_here TELEGRAM_TIMEOUT=10 TELEGRAM_LOGS_LEVEL=error
Environment Variables
Variable | Description | Required | Default | Comments |
---|---|---|---|---|
TELEGRAM_BOT_TOKEN |
Your bot's API token | Yes | - | |
TELEGRAM_CHAT_ID |
Target chat/channel ID | Yes | - | |
TELEGRAM_TOPIC_ID |
Forum logs topic ID (optional) | No | null |
for your logs topic |
TELEGRAM_TOPIC_MESSAGE_ID |
Forum direct topic ID (optional) | No | null |
for your direct msg such as a form |
TELEGRAM_LOG_LEVEL |
Minimum log level | No | critical |
|
TELEGRAM_TIMEOUT |
API request timeout | No | 10 |
Supported Log Levels
Severity Level | Description |
---|---|
Debug | Detailed diagnostic information |
Info | General operational messages |
Notice | Significant normal events |
Warning | Potential issue notifications |
Error | Runtime error conditions |
Critical | Critical condition alerts |
Alert | Immediate action requirements |
Emergency | System instability events |
Log Channel Configuration
Set the default logging channel in your .env
file:
LOG_CHANNEL=telegram
Implementation
Standard Logging Implementation
// Error logging implementation \Log::error('Payment processing failure detected'); // Exception handling with logging try { // Application code execution } catch (\Exception $e) { \Log::critical('API connectivity issue: ' . $e->getMessage()); } // Debug information recording \Log::debug('User authentication successful', ['user_id' => auth()->id()]);
Command Line Utilities
Configuration Validation
php artisan telegramlogs:test
Available options:
--message="Custom notification"
- Dispatch custom test message--level=error
- Specify log severity level--list
- Display available log levels--config
- Show current configuration values
Custom message example:
php artisan telegramlogs:test --message="System health check" --level=warning
Configuration Display
php artisan telegramlogs:test --config
Output Formatting
Notifications delivered to your Telegram channel will feature structured JSON formatting:
{ "message": "Database connection failure", "level": "CRITICAL", "datetime": "2025-08-17T11:55:13.885292+00:00", "context": { "exception": "PDOException: driver not found" } }
Direct Messaging Interface
To initiate direct messaging, import the facade:
use Uzhlaravel\Telegramlogs\Facades\TelegramMessage;
Implementation examples:
// Basic message transmission TelegramMessage::message('System notification from Laravel'); // Targeted chat messaging TelegramMessage::toChat('alternative_chat_id', 'Specific channel message'); // Connection verification TelegramMessage::test();
📱 Getting Telegram Credentials
1. Create a Telegram Bot
- Open Telegram and search for @BotFather
- Send
/newbot
and follow the instructions - Choose a name and username for your bot
- Copy the provided API token
2. Get Chat ID
For private chats:
- Send a message to your bot
- Visit:
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates
- Look for the
chat.id
value
For channels:
- Add your bot as an administrator
- The chat ID is usually in the format:
-100xxxxxxxxx
For groups:
- Add your bot to the group
- Send a message mentioning the bot
- Check the API endpoint above for the group's chat ID
3. Forum Topics (Optional)
For groups with topics enabled:
- Create a topic in your group
- Send a message to that topic
- Use the API endpoint to find the
message_thread_id
Security Protocol
- Maintain bot token confidentiality
- Implement chat-specific bot access restrictions
- Review our security policy for vulnerability disclosure procedures
Version History
Detailed release notes available in CHANGELOG.
Contribution Guidelines
We encourage community contributions. Please review CONTRIBUTING for participation guidelines.
Licensing
MIT License. Complete license details available in LICENSE.