nwservices/laravel-telegram-logger
最新稳定版本:2.0.0
Composer 安装命令:
composer require nwservices/laravel-telegram-logger
包简介
A Laravel package that monitors laravel.log and sends Telegram notifications when errors occur
README 文档
README
A Laravel package that monitors your laravel.log file and sends Telegram notifications when errors occur. Get instant alerts in your Telegram chat when critical errors happen in your Laravel application.
Features
- Monitors
storage/logs/laravel.logfor new errors - Scheduled task runs via Laravel scheduler (e.g., every minute)
- Only alerts on NEW errors (tracks file position to avoid duplicates)
- Configurable log levels (ERROR, CRITICAL, ALERT, EMERGENCY)
- Environment-based notifications (only send in production/staging)
- Rate limiting to prevent spam from duplicate errors
- Formatted messages with emojis for quick visual identification
- Handles multi-line log entries (stack traces)
- Detects log rotation automatically
Requirements
- PHP 8.1 or higher
- Laravel 10.x, 11.x, or 12.x
Installation
Install the package via Composer:
composer require nwservices/laravel-telegram-logger
The service provider will be automatically registered through Laravel's package auto-discovery.
Publish the configuration file (optional):
php artisan vendor:publish --tag=telegram-logger-config
Getting a Telegram Bot Token
- Open Telegram and search for @BotFather
- Start a conversation and send
/newbot - Follow the prompts to name your bot
- BotFather will provide you with a token like
123456789:ABCdefGHIjklMNOpqrsTUVwxyz - Save this token for your environment configuration
Getting Your Chat ID
For Personal Notifications
- Search for @userinfobot on Telegram
- Start a conversation with it
- It will reply with your user ID (a number like
123456789)
For Group Notifications
- Add your bot to the group
- Send a message in the group
- Visit
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates - Look for the
chatobject and find theidfield (it will be negative for groups, like-123456789)
For Channel Notifications
- Add your bot as an administrator to the channel
- Use the channel's username with an
@prefix (e.g.,@mychannel) - Or use the channel ID (find it using the getUpdates method after posting)
Configuration
Add these environment variables to your .env file:
TELEGRAM_LOGGER_BOT_TOKEN=your-bot-token-here TELEGRAM_LOGGER_CHAT_ID=your-chat-id-here # Optional settings TELEGRAM_LOGGER_PROJECT_NAME="${APP_NAME}" TELEGRAM_LOGGER_ENVIRONMENT="${APP_ENV}" TELEGRAM_LOGGER_NOTIFY_ENVIRONMENTS=production,staging TELEGRAM_LOGGER_THROTTLE=60 # Log monitoring settings TELEGRAM_LOGGER_LOG_PATH=storage/logs/laravel.log TELEGRAM_LOGGER_MONITOR_LEVELS=ERROR,CRITICAL,ALERT,EMERGENCY TELEGRAM_LOGGER_POSITION_STORAGE=cache
Setting Up the Scheduler
The package provides an artisan command that monitors your log file. You need to schedule it to run periodically.
Laravel 11+ (routes/console.php)
use Illuminate\Support\Facades\Schedule; Schedule::command('telegram:monitor-log') ->everyMinute() ->withoutOverlapping() ->runInBackground();
Laravel 10 (app/Console/Kernel.php)
protected function schedule(Schedule $schedule): void { $schedule->command('telegram:monitor-log') ->everyMinute() ->withoutOverlapping() ->runInBackground(); }
Make sure your server's cron is set up to run Laravel's scheduler:
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Artisan Command
Basic Usage
# Run the log monitor (typically via scheduler) php artisan telegram:monitor-log # Test without sending (dry run) php artisan telegram:monitor-log --dry-run # Reset the position tracker (start fresh) php artisan telegram:monitor-log --reset # Monitor a custom log file php artisan telegram:monitor-log --log-path=/var/log/myapp/app.log # Monitor specific levels only php artisan telegram:monitor-log --levels=ERROR,CRITICAL
Command Options
| Option | Description |
|---|---|
--log-path |
Path to log file (default: storage/logs/laravel.log) |
--levels |
Comma-separated log levels to monitor (default: from config) |
--dry-run |
Parse and display errors without sending to Telegram |
--reset |
Reset the file position tracker |
Configuration Options
| Option | Default | Description |
|---|---|---|
bot_token |
'' |
Your Telegram bot token from BotFather |
chat_id |
'' |
The Telegram chat/group/channel ID to send messages to |
project_name |
APP_NAME |
Project name displayed in notifications |
environment |
APP_ENV |
Environment name displayed in notifications |
notify_environments |
['production', 'staging'] |
Only send notifications in these environments |
throttle |
60 |
Seconds to wait before sending duplicate messages |
log_path |
storage/logs/laravel.log |
Path to the log file to monitor |
monitor_levels |
ERROR,CRITICAL,ALERT,EMERGENCY |
Log levels to monitor |
position_storage |
cache |
Where to store file position (cache or file) |
How It Works
- The
telegram:monitor-logcommand runs on a schedule (e.g., every minute) - It reads
laravel.logfrom the last saved position - Parses new content for log entries matching configured levels (e.g.,
.ERROR) - Sends Telegram notifications for each matching entry
- Saves the new file position to avoid re-processing
- Automatically detects log rotation and resets position
Log Level Emojis
Messages are prefixed with emojis for quick visual identification:
| Level | Emoji |
|---|---|
| Emergency | 🆘 |
| Alert | 🔔 |
| Critical | 🔴 |
| Error | 🚨 |
| Warning | ⚠️ |
| Notice | 📝 |
| Info | ℹ️ |
| Debug | 🔍 |
Example Message
🚨 ERROR
Project: My Laravel App
Environment: production
Time: 2025-01-15 14:30:45
Message:
SQLSTATE[HY000] [2002] Connection refused
#0 /var/www/app/Database/Connection.php(42): PDO->__construct()
#1 /var/www/app/Database/Manager.php(123): Connection->connect()
...
Context:
{
"environment": "production"
}
Testing
To test if your configuration is working:
# Add a test error to your log file echo "[$(date '+%Y-%m-%d %H:%M:%S')] production.ERROR: Test error from Laravel Telegram Logger" >> storage/logs/laravel.log # Run the monitor in dry-run mode php artisan telegram:monitor-log --dry-run # Or run it for real php artisan telegram:monitor-log
Security
- Never commit your bot token to version control
- Use environment variables for all sensitive configuration
- Consider using a dedicated bot for each project
- Be mindful of what information appears in your logs
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 36
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-15