Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Exceptionlog Laravel Package

adrianoalves/laravel-exceptionlog

Minimal Laravel 7+ package to persist exceptions to a database logs table. Install via Composer, migrate, then call ExceptionLog::persist($exception, $level). Includes simple level mapper (app, DB, server, console, jobs) and is customizable.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require adrianoalves/laravel-exceptionlog
    

    Publish the config file:

    php artisan vendor:publish --provider="AdrianoAlves\ExceptionLog\ExceptionLogServiceProvider"
    
  2. Configuration: Edit config/exceptionlog.php to define:

    • driver (e.g., database, log, or slack)
    • Database table name (if using database driver)
    • Slack webhook URL (if using slack driver)
    • Log file path (if using log driver)
  3. First Use Case: Trigger an exception in a route or controller:

    public function testException()
    {
        throw new \Exception("Test exception for logging");
    }
    

    Verify the exception is logged by checking:

    • Database table (exception_logs) for database driver.
    • Laravel logs (storage/logs/laravel.log) for log driver.
    • Slack notifications for slack driver.

Implementation Patterns

Core Workflows

  1. Exception Handling: Override Laravel’s default exception handler to log exceptions:

    // app/Exceptions/Handler.php
    public function report(Throwable $exception)
    {
        ExceptionLog::log($exception);
        parent::report($exception);
    }
    
  2. Manual Logging: Log exceptions manually in custom logic:

    try {
        // Risky operation
    } catch (\Exception $e) {
        ExceptionLog::log($e, ['user_id' => auth()->id()]);
    }
    
  3. Custom Fields: Attach metadata to exceptions:

    ExceptionLog::log($e, [
        'request_id' => $request->header('X-Request-ID'),
        'context' => 'payment_processing'
    ]);
    
  4. Slack Notifications: Configure Slack alerts for critical errors:

    // config/exceptionlog.php
    'slack' => [
        'webhook_url' => env('SLACK_WEBHOOK_URL'),
        'level' => 'critical', // Only log 'critical' errors
    ]
    

Integration Tips

  • Queue Exceptions: Use Laravel queues to defer logging for performance:

    ExceptionLog::log($e)->onQueue('exception-logging');
    
  • Middleware for API Errors: Log API-specific exceptions in middleware:

    public function handle($request, Closure $next)
    {
        try {
            return $next($request);
        } catch (\Exception $e) {
            ExceptionLog::log($e, ['api_endpoint' => $request->path()]);
            throw $e;
        }
    }
    
  • Database Schema Customization: Extend the exception_logs table by creating a migration:

    Schema::table('exception_logs', function (Blueprint $table) {
        $table->string('custom_field')->nullable();
    });
    

Gotchas and Tips

Pitfalls

  1. Missing Config: Forgetting to publish the config file (php artisan vendor:publish) will result in default settings (e.g., no Slack integration).

  2. Database Driver Issues: Ensure the exception_logs table exists and is writable. Run migrations if needed:

    php artisan migrate
    
  3. Slack Rate Limits: Excessive logging to Slack may hit rate limits. Use the level config to filter errors (e.g., critical only).

  4. Performance Overhead: Logging every exception can slow down requests. Use queues or defer logging for non-critical errors.

Debugging

  • Check Logs: Verify exceptions are being logged by inspecting:

    • Database: SELECT * FROM exception_logs ORDER BY created_at DESC LIMIT 10;
    • Laravel logs: tail -f storage/logs/laravel.log
    • Slack: Check for incoming webhook notifications.
  • Enable Debug Mode: Temporarily enable debug mode in config/exceptionlog.php to log additional context:

     'debug' => env('APP_DEBUG', false),
    

Extension Points

  1. Custom Drivers: Extend the package by creating a custom driver. Implement the AdrianoAlves\ExceptionLog\Contracts\Driver interface.

  2. Event Listeners: Listen for exception-logged events to trigger side effects:

    Event::listen('exception-logged', function ($exception) {
        // Send email, trigger webhook, etc.
    });
    
  3. Middleware for Global Logging: Create middleware to log exceptions globally:

    public function handle($request, Closure $next)
    {
        try {
            return $next($request);
        } catch (\Exception $e) {
            ExceptionLog::log($e);
            throw $e;
        }
    }
    

    Register it in app/Http/Kernel.php under $middleware.

  4. Filtering Exceptions: Use the level field to categorize exceptions (e.g., error, warning, critical) and filter logs accordingly:

    ExceptionLog::log($e, ['level' => 'critical']);
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope