salikhov/yii2-telegram-log-target

Yii2 log target that sends error notifications to Telegram

Maintainers

Package info

github.com/SalikhovID/yii2-telegram-log-target

Type:yii2-extension

pkg:composer/salikhov/yii2-telegram-log-target

Statistics

Installs: 34

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-01-05 20:58 UTC

This package is not auto-updated.

Last update: 2026-04-14 20:44:28 UTC


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