haythem-bekir / laravel-discord-logger
Laravel package for sending log notifications to Discord via webhooks with real-time alerts and daily reports
Package info
github.com/haythembekir20/laravel-discord-logger
pkg:composer/haythem-bekir/laravel-discord-logger
Requires
- php: ^8.1
- illuminate/http: ^10.0|^11.0|^12.0
- illuminate/support: ^10.0|^11.0|^12.0
- spatie/laravel-package-tools: ^1.11
Requires (Dev)
- larastan/larastan: ^2.0|^3.0
- laravel/pint: ^1.0
- orchestra/testbench: ^8.0|^9.0|^10.0
- pestphp/pest: ^2.0|^3.0
- pestphp/pest-plugin-laravel: ^2.0|^3.0
- phpunit/phpunit: ^10.0|^11.0
README
A Laravel package that sends daily log statistics reports to Discord. Get comprehensive insights about your application's logs delivered to your Discord channel every day.
Why Daily Reports?
Instead of alert fatigue from real-time notifications, this package provides:
- đ Comprehensive daily statistics - Total logs, breakdown by level and channel
- â ī¸ Top recurring errors - Identify patterns and prioritize fixes
- đ¯ Better signal-to-noise - Review at your chosen time, not during incidents
- đ Zero performance impact - Runs as a scheduled task, not on every request
- đ Historical insights - See trends and patterns over time
Features
- â Daily log statistics report sent to Discord
- â Breakdown by log level (emergency, alert, critical, error, warning, etc.)
- â Breakdown by channel
- â Top 5 recurring errors with count
- â Customizable bot appearance (username, avatar)
- â Environment labels (production, staging, etc.)
- â Manual or scheduled execution
- â Dry-run mode for previewing reports
- â Clean SOLID architecture
- â Fully testable
Requirements
- PHP 8.1+
- Laravel 10.x, 11.x, or 12.x
Installation
Install via Composer:
composer require haythem-bekir/laravel-discord-logger
Publish the configuration file:
php artisan vendor:publish --tag=discord-logger-config
Quick Start
1. Get Your Discord Webhook URL
- Go to your Discord server settings
- Navigate to Integrations â Webhooks
- Click New Webhook
- Choose the channel for reports
- Copy the webhook URL
2. Configure Your Environment
Add to your .env file:
DISCORD_LOGGER_DAILY_REPORT_ENABLED=true DISCORD_LOGGER_DAILY_REPORT_WEBHOOK_URL=https://discord.com/api/webhooks/YOUR_WEBHOOK_URL DISCORD_LOGGER_DAILY_REPORT_TIME=08:00 DISCORD_LOGGER_DAILY_REPORT_TIMEZONE=UTC
3. Schedule the Daily Report
Laravel 11+ (routes/console.php):
use Illuminate\Support\Facades\Schedule; Schedule::command('discord-logger:daily-report')->dailyAt('08:00');
Laravel 10 (app/Console/Kernel.php):
protected function schedule(Schedule $schedule): void { $schedule->command('discord-logger:daily-report')->dailyAt('08:00'); }
Make sure your scheduler is running:
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
That's it! You'll receive daily reports at 8:00 AM (UTC).
Usage
Manual Execution
Run reports manually anytime:
# Send yesterday's report php artisan discord-logger:daily-report # Send report for a specific date php artisan discord-logger:daily-report --date=2024-01-15 # Preview without sending (dry run) php artisan discord-logger:daily-report --dry-run
Programmatic Usage
Use the actions directly in your code:
use HaythemBekir\DiscordLogger\Application\DailyReport\GatherLogStatisticsAction; use HaythemBekir\DiscordLogger\Application\DailyReport\BuildDailyReportAction; use HaythemBekir\DiscordLogger\Application\DailyReport\SendDailyReportAction; use Carbon\Carbon; class CustomReportService { public function __construct( private GatherLogStatisticsAction $gather, private BuildDailyReportAction $build, private SendDailyReportAction $send, ) {} public function sendWeeklyDigest(): void { $dates = collect(range(0, 6))->map(fn($days) => Carbon::now()->subDays($days)); foreach ($dates as $date) { $stats = $this->gather->execute($date); $report = $this->build->execute($stats); $this->send->execute($report); } } }
Configuration
The package provides several customization options in config/discord-logger.php:
return [ 'daily_report' => [ 'enabled' => env('DISCORD_LOGGER_DAILY_REPORT_ENABLED', false), 'webhook_url' => env('DISCORD_LOGGER_DAILY_REPORT_WEBHOOK_URL'), 'time' => env('DISCORD_LOGGER_DAILY_REPORT_TIME', '08:00'), 'timezone' => env('DISCORD_LOGGER_DAILY_REPORT_TIMEZONE', 'UTC'), ], 'appearance' => [ 'username' => env('DISCORD_LOGGER_BOT_USERNAME', 'Laravel Logger'), 'avatar_url' => env('DISCORD_LOGGER_BOT_AVATAR_URL'), ], 'colors' => [ 'emergency' => 0xFF0000, // Red 'alert' => 0xFF3300, // Orange-Red 'critical' => 0xFF6600, // Orange 'error' => 0xFF9900, // Dark Orange 'warning' => 0xFFCC00, // Yellow 'notice' => 0x00CCFF, // Cyan 'info' => 0x0099FF, // Blue 'debug' => 0x999999, // Gray ], 'environment_label' => env('DISCORD_LOGGER_ENVIRONMENT_LABEL', env('APP_ENV')), 'log_viewer' => [ 'enabled' => env('DISCORD_LOGGER_LOG_VIEWER_ENABLED', true), 'app_url' => env('DISCORD_LOGGER_APP_URL', env('APP_URL')), ], ];
Log Viewer Integration
If you have a Laravel Log Viewer package installed, error messages in Discord reports will automatically become clickable links that take you directly to the log viewer!
Supported Log Viewers
- opcodesio/log-viewer - Modern, feature-rich log viewer (Recommended)
- rap2hpoutre/laravel-log-viewer - Simple, lightweight log viewer
How It Works
-
Install any supported log viewer package:
composer require opcodesio/log-viewer
-
Ensure your
APP_URLis configured in.env:APP_URL=https://your-app.com
-
Error messages in Discord will automatically become clickable:
Top Recurring Errors: âĸ 8x: [foreach() argument must be of type array|object, null given](https://your-app.com/log-viewer?query=foreach) âĸ 6x: [syntax error, unexpected token "return"](https://your-app.com/log-viewer?query=syntax+error)
Clicking the error takes you directly to the log viewer with a search query pre-filled, making it easy to investigate issues!
Configuration
You can customize the log viewer integration:
# Enable/disable log viewer links DISCORD_LOGGER_LOG_VIEWER_ENABLED=true # Override app URL if needed DISCORD_LOGGER_APP_URL=https://your-app.com
What the Report Includes
Each daily report contains:
- đ Date - Which day the report covers
- đ Total Logs - Total number of log entries
- đ Breakdown by Level - Count for each level with emoji indicators:
- đ¨ Emergency
- đ Alert
- đĨ Critical
- â Error
- â ī¸ Warning
- đ Notice
- âšī¸ Info
- đ Debug
- đ Breakdown by Channel - Count per logging channel
- â ī¸ Top Recurring Errors - Top 5 most frequent error messages
Example Discord Message
đ
Daily Log Report - 2024-01-15
Issues detected in the last 24 hours.
Total Logs: 1,234
â Error: 15
â ī¸ Warning: 47
đ Notice: 122
âšī¸ Info: 1,050
By Channel:
production: 1,234
Top Recurring Errors:
âĸ 8x: foreach() argument must be of type array|object, null given [View in Log Viewer]
âĸ 6x: syntax error, unexpected token "return" [View in Log Viewer]
âĸ 2x: Call to undefined method [View in Log Viewer]
Generated by Laravel Discord Logger
đĄ Note: If you have a Log Viewer package installed, the error messages become clickable links!
Testing
# Run tests composer test # Run with coverage composer test-coverage # Static analysis composer analyse # Code style check composer format-check # Auto-fix code style composer format
Architecture
This package follows SOLID principles and Domain-Driven Design:
src/
âââ Application/DailyReport/ # Use cases & DTOs
â âââ GatherLogStatisticsAction.php
â âââ BuildDailyReportAction.php
â âââ SendDailyReportAction.php
âââ Domain/ # Business logic & value objects
â âââ Config/
â âââ ValueObjects/
âââ Infrastructure/ # External integrations
â âââ Http/DiscordWebhookClient.php
âââ Console/Commands/ # Artisan commands
This makes the package:
- â Testable - Mock dependencies easily
- â Extensible - Implement custom transports
- â Maintainable - Clear separation of concerns
Changelog
Please see CHANGELOG for more information on what has changed recently.
Upgrading
Please see UPGRADE for upgrade instructions.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Credits
- Haythem Bekir
- Built with Spatie Laravel Package Tools
License
The MIT License (MIT). Please see License File for more information.