定制 marekmiklusek/telegram-logger 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

marekmiklusek/telegram-logger

最新稳定版本:v1.1.2

Composer 安装命令:

composer require marekmiklusek/telegram-logger

包简介

A Laravel package to send logs to Telegram.

README 文档

README

blog-featured-how-to-delete-telegram

📢 Laravel Telegram Logger

🚀 Laravel Telegram Logger is a package that sends Laravel log messages and exceptions to Telegram for real-time monitoring.

📌 Features

Real-time logging to Telegram
Supports all log levels (debug, info, warning, error, etc.)
Automatic exception handling (captures error file, line, and message)
Configurable log level filtering
Silent notifications support (to avoid sound/vibration in Telegram)
Minimal setup, easy to integrate
Can be enabled/disabled via configuration

🛠 Installation

Require the package via Composer:

composer require marekmiklusek/telegram-logger

🔧 Configuration

Publish the package configuration:

php artisan vendor:publish --tag=telegram-logger-config

This will create a config file at config/telegram-logger.php.

.env Configuration

Add your Telegram Bot API token and Chat ID to your .env file:

TELEGRAM_BOT_TOKEN=your_bot_token
TELEGRAM_CHAT_ID=your_chat_id

Config File (config/telegram-logger.php)

return [
    'bot_token' => env('TELEGRAM_BOT_TOKEN'),
    'chat_id' => env('TELEGRAM_CHAT_ID'),
    'level' => 'error',
    'silent_notification' => false,
    'is_enabled' => true,
];

🏗 Usage

Basic Logging

Use Laravel's Log facade as usual, and errors will be sent to Telegram automatically:

use Illuminate\Support\Facades\Log;

Log::debug('Debug message');
Log::info('Info message');
Log::warning('Warning message');
Log::error('Error message');

Logging with Context

You can pass additional context to logs:

Log::error('User not found', ['user_id' => 42, 'action' => 'login']);

Logging Exceptions

Exceptions are automatically detected and logged:

try {
    throw new \Exception('Database connection failed!');
} catch (\Exception $exception) {
    Log::error('Unhandled exception occurred', ['exception' => $exception]);
}

⚙ How It Works

The package listens to Laravel's logging events and sends structured messages to Telegram.

Example Telegram Log Output

🛠️ Application: MyLaravelApp
🌍 Environment: production

❌ Level: ERROR
📝 Message: "User not found"

📌 File:
/var/www/html/app/Http/Controllers/UserController.php:45

📂 Context:
{
  "user_id": 42,
  "action": "login"
}

⏳ Time: 2025-02-19 10:15:30

Example Telegram Log Output (Exception)

🛠️ Application: MyLaravelApp
🌍 Environment: production

❌ Level: ERROR
🔥 Exception Occurred !
💥 Message: "Database connection failed!"

📌 File:
/var/www/html/app/Services/DatabaseService.php:30

⏳ Time: 2025-02-19 10:18:45

🎯 Advanced Configuration

1️⃣ Logger Enablement

You can enable or disable the logger in config/telegram-logger.php:

return [
    'is_enabled' => false,
];

✅ If true, logs will be sent to Telegram as configured
✅ If false, the logger will be completely disabled (no logs sent)

2️⃣ Log Level Filtering

You can define the minimum log level in config/telegram-logger.php:

return [
    'level' => 'warning',
];
  • debug: Logs everything
  • info: Logs info, notice, warning, error, critical, alert, emergency
  • error: Logs error, critical, alert, emergency
  • critical: Logs only critical, alert, emergency

3️⃣ Silent Notifications

Enable silent notifications (no sound/vibration) in config/telegram-logger.php

return [
    'silent_notification' => true,
];

✅ If true, messages will be sent silently.
✅ If false, Telegram will send notifications normally.

💡 Troubleshooting

Logs not appearing in Telegram?

  1. Check that your .env values are correctly set:
php artisan config:clear
php artisan config:cache
  1. Ensure your bot has permission to send messages to your chat.

Getting "Chat not found" error?

  • Make sure you have sent a message to your bot first.
  • Use this tool to get your Chat ID.

📜 License

This package is open-source and licensed under the MIT License.

统计信息

  • 总下载量: 107
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-19