承接 salikhov/yii2-telegram-log-target 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

  1. Open Telegram and search for @BotFather
  2. Send /newbot and follow the instructions
  3. Copy the bot token (looks like 123456789:ABCdefGHIjklMNOpqrsTUVwxyz)

2. Get Your Chat ID

  1. Start a chat with your bot
  2. Send any message to the bot
  3. Visit https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates
  4. Find your chat_id in 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 HttpException with 4xx status codes)
  • HttpException with 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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-05