salikhov/yii2-telegram-log-target
最新稳定版本:v1.0.0
Composer 安装命令:
composer require salikhov/yii2-telegram-log-target
包简介
Yii2 log target that sends error notifications to Telegram
README 文档
README
A Yii2 log target that sends error notifications to Telegram. Perfect for real-time error monitoring of your Yii2 applications.
Features
- Sends only 5xx server errors and exceptions (no spam from 4xx client errors)
- HTML-formatted messages with error details, file location, and stack trace
- Automatic message chunking for long error messages (Telegram's 4096 char limit)
- Easy configuration via environment variables
Installation
composer require salikhov/yii2-telegram-log-target
Configuration
1. Create a Telegram Bot
- Open Telegram and search for @BotFather
- Send
/newbotand follow the instructions - Copy the bot token (looks like
123456789:ABCdefGHIjklMNOpqrsTUVwxyz)
2. Get Your Chat ID
- Start a chat with your bot
- Send any message to the bot
- Visit
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates - Find your
chat_idin the response
For group chats, add the bot to the group and use the negative group ID.
3. Configure Yii2
Add to your application config (e.g., config/main.php or config/web.php):
'components' => [ 'log' => [ 'targets' => [ [ 'class' => \Salikhov\TelegramLogTarget\TelegramTarget::class, 'levels' => ['error'], 'chat_id' => getenv('TELEGRAM_ERROR_CHAT_ID'), 'bot_token' => getenv('TELEGRAM_BOT_TOKEN'), 'title' => 'My App Error', // Optional, defaults to "Error" ], ], ], ],
4. Set Environment Variables
Add to your .env file:
TELEGRAM_ERROR_CHAT_ID=123456789 TELEGRAM_BOT_TOKEN=123456789:ABCdefGHIjklMNOpqrsTUVwxyz
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
chat_id |
string | null | Telegram chat ID (user, group, or channel) |
bot_token |
string | null | Telegram bot token from @BotFather |
title |
string | "Error" | Title prefix shown in bold at the top |
levels |
array | - | Standard Yii2 log levels to capture |
Message Format
Error messages are formatted with HTML:
**Error**
2024-01-15 14:30:00
Error: Division by zero
File: /var/www/app/controllers/SiteController.php:42
[Stack trace...]
Filtering Errors
By default, only 5xx server errors are sent. This includes:
- All exceptions (except
HttpExceptionwith 4xx status codes) HttpExceptionwith status codes 500-599
4xx client errors are intentionally ignored to reduce noise.
Advanced Usage
Multiple Environments
'targets' => [ [ 'class' => \Salikhov\TelegramLogTarget\TelegramTarget::class, 'levels' => ['error'], 'chat_id' => getenv('TELEGRAM_ERROR_CHAT_ID'), 'bot_token' => getenv('TELEGRAM_BOT_TOKEN'), 'title' => YII_ENV_PROD ? 'PROD Error' : 'DEV Error', 'enabled' => YII_ENV_PROD, // Only enable in production ], ],
Combine with File Logging
'targets' => [ // File logging for all errors [ 'class' => 'yii\log\FileTarget', 'levels' => ['error', 'warning'], ], // Telegram only for critical errors [ 'class' => \Salikhov\TelegramLogTarget\TelegramTarget::class, 'levels' => ['error'], 'chat_id' => getenv('TELEGRAM_ERROR_CHAT_ID'), 'bot_token' => getenv('TELEGRAM_BOT_TOKEN'), ], ],
Requirements
- PHP 8.0+
- Yii2 2.0+
License
MIT License. See LICENSE for details.
yii2-telegram-log-target
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-05