承接 haythem-bekir/laravel-discord-logger 相关项目开发

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

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

haythem-bekir/laravel-discord-logger

Composer 安装命令:

composer require haythem-bekir/laravel-discord-logger

包简介

Laravel package for sending log notifications to Discord via webhooks with real-time alerts and daily reports

README 文档

README

A Laravel package for sending log notifications to Discord via webhooks. Supports real-time error alerts and daily log reports.

Features

  • Real-time Notifications: Send log messages to Discord instantly as they happen
  • Daily Reports: Scheduled summary of log statistics with top recurring errors
  • Async Support: Queue-based notifications for zero performance impact
  • Rate Limiting: Prevent webhook flooding during error storms
  • Customizable: Configure log levels, colors, bot appearance, and more
  • Environment Labels: Identify which environment logs are coming from

Requirements

  • PHP 8.1+
  • Laravel 10.x or 11.x

Installation

Install via Composer:

composer require haythem-bekir/laravel-discord-logger

Publish the configuration file:

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

Configuration

Environment Variables

Add these to your .env file:

# Real-time Notifications
DISCORD_LOGGER_REALTIME_ENABLED=true
DISCORD_LOGGER_REALTIME_WEBHOOK_URL=https://discord.com/api/webhooks/your-webhook-url
DISCORD_LOGGER_REALTIME_ASYNC=true
DISCORD_LOGGER_REALTIME_LEVELS=emergency,alert,critical,error

# Rate Limiting
DISCORD_LOGGER_RATE_LIMIT_ENABLED=true
DISCORD_LOGGER_RATE_LIMIT_MAX=10

# Daily Reports
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

# Appearance
DISCORD_LOGGER_BOT_USERNAME="Laravel Logger"
DISCORD_LOGGER_BOT_AVATAR_URL=

# Environment Label
DISCORD_LOGGER_ENVIRONMENT_LABEL=production

Setting Up the Log Channel

Add the Discord channel to your config/logging.php:

'channels' => [
    // ... other channels

    'discord' => [
        'driver' => 'custom',
        'via' => \HaythemBekir\DiscordLogger\Logging\CreateDiscordLogger::class,
        'level' => 'error',
    ],
],

Using with Stack Channel

To send logs to both file and Discord:

'channels' => [
    'stack' => [
        'driver' => 'stack',
        'channels' => ['daily', 'discord'],
        'ignore_exceptions' => false,
    ],

    // ... other channels
],

Usage

Real-time Notifications

Once configured, any log message at or above the configured level will be sent to Discord:

// These will trigger Discord notifications (if level is 'error' or above)
Log::error('Something went wrong!');
Log::critical('Database connection failed', ['host' => 'localhost']);

// This won't trigger (below error level by default)
Log::warning('This is just a warning');

Daily Reports

Run the daily report command manually:

# 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

Scheduling the Daily Report

To automatically send daily reports, add this to your routes/console.php (Laravel 11+) or app/Console/Kernel.php:

Laravel 11+ (routes/console.php):

use Illuminate\Support\Facades\Schedule;

Schedule::command('discord-logger:daily-report')
    ->dailyAt('08:00')
    ->timezone('UTC');

Laravel 10 (app/Console/Kernel.php):

protected function schedule(Schedule $schedule): void
{
    $schedule->command('discord-logger:daily-report')
        ->dailyAt('08:00')
        ->timezone('UTC');
}

Make sure your scheduler is running:

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

Direct Service Usage

You can also use the service directly:

use HaythemBekir\DiscordLogger\Services\DiscordNotificationService;

$discord = app(DiscordNotificationService::class);

// Send a custom notification
$discord->sendLogNotification('error', 'Custom error message', [
    'user_id' => 123,
    'action' => 'login_failed',
]);

// Send a custom report
$discord->sendDailyReport([
    'date' => '2024-01-15',
    'total' => 150,
    'by_level' => [
        'error' => 10,
        'warning' => 40,
        'info' => 100,
    ],
]);

Discord Webhook Setup

  1. Go to your Discord server settings
  2. Navigate to Integrations > Webhooks
  3. Click New Webhook
  4. Choose the channel for notifications
  5. Copy the webhook URL
  6. Paste it in your .env file

Embed Colors

Default colors for each log level (customizable in config):

Level Color
Emergency Red
Alert Orange-Red
Critical Orange
Error Dark Orange
Warning Yellow
Notice Cyan
Info Blue
Debug Gray

License

MIT License. See LICENSE for more information.

统计信息

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

GitHub 信息

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

其他信息

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