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 Error Logger Laravel Package

justbetter/laravel-error-logger

Manually log application errors to a database with a fluent API (message, details, group, throwable, model). Optionally schedule a daily notification job to send summaries via a chosen channel (e.g. Slack). Includes pruning command; Nova UI available separately.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require justbetter/laravel-error-logger
    php artisan vendor:publish --provider="JustBetter\ErrorLogger\ServiceProvider"
    php artisan migrate
    
  2. First Log Entry: Manually log an exception in a controller or service:

    use JustBetter\ErrorLogger\Facades\ErrorLogger;
    
    try {
        // Risky code here
    } catch (\Exception $e) {
        ErrorLogger::log($e);
    }
    
  3. Verify Storage: Check the error_logs table in your database for the recorded entry.

First Use Case

Log critical exceptions (e.g., payment failures, API timeouts) and review them via:

  • Database (error_logs table)
  • Nova dashboard (if using justbetter/nova-error-logger)

Implementation Patterns

Core Workflows

  1. Manual Logging:

    • Use ErrorLogger::log($exception) in catch blocks for critical exceptions.
    • Example: Logging failed API calls:
      try {
          $response = Http::post('https://api.example.com/fail');
      } catch (\Exception $e) {
          ErrorLogger::log($e, ['request_data' => $request->all()]);
      }
      
  2. Contextual Data: Attach metadata to logs for debugging:

    ErrorLogger::log($e, [
        'user_id' => auth()->id(),
        'ip' => request()->ip(),
        'route' => route()->getName(),
    ]);
    
  3. Scheduler Integration:

    • Add the daily notification job to App\Console\Kernel.php:
      $schedule->job(new \JustBetter\ErrorLogger\Jobs\ErrorNotificationJob())
               ->dailyAt('09:00');
      
    • Configure notification channel in .env:
      LARAVEL_ERROR_NOTIFICATION_CHANNEL=slack
      
  4. Nova Integration (if installed):

    • Access logs via Nova’s Error Logs resource.
    • Filter by user, severity, or date range.

Integration Tips

  • Middleware: Create middleware to auto-log HTTP exceptions:
    public function handle($request, Closure $next) {
        try {
            return $next($request);
        } catch (\Exception $e) {
            ErrorLogger::log($e);
            throw $e; // Re-throw to maintain flow
        }
    }
    
  • Testing: Mock ErrorLogger in tests:
    $this->partialMock(ErrorLogger::class, function ($mock) {
        $mock->shouldReceive('log')->once();
    });
    

Gotchas and Tips

Pitfalls

  1. Manual Logging Required:

    • The package does not auto-capture exceptions. Always wrap risky code in try-catch and log manually.
    • Fix: Use middleware or decorators for global exception handling.
  2. Database Bloat:

    • Unpruned logs can fill the error_logs table.
    • Fix: Run php artisan laravel-error-logger:prune regularly (e.g., weekly via scheduler).
  3. Notification Delays:

    • Daily notifications (ErrorNotificationJob) may lag if the server is offline.
    • Fix: Monitor job status with php artisan schedule:work.
  4. Nova Dependency:

    • Nova integration requires justbetter/nova-error-logger. Without it, logs are database-only.

Debugging

  • Missing Logs:

    • Verify the error_logs table exists and is writable.
    • Check Laravel logs (storage/logs/laravel.log) for migration errors.
  • Notification Failures:

    • Ensure the LARAVEL_ERROR_NOTIFICATION_CHANNEL (e.g., slack) is configured in .env.
    • Test notifications manually:
      php artisan laravel-error-logger:notify
      

Extension Points

  1. Custom Channels: Extend notification support by implementing JustBetter\ErrorLogger\Contracts\NotificationChannel:

    class EmailChannel implements NotificationChannel {
        public function send(array $errors) { /* ... */ }
    }
    
  2. Log Filters: Override the ErrorLogger facade to filter logs before storage:

    ErrorLogger::extend(function ($logger) {
        $logger->setFilter(function ($exception, $context) {
            return !str_contains($exception->getMessage(), 'expected');
        });
    });
    
  3. Prune Logic: Customize the prune command by publishing and modifying the migration:

    php artisan vendor:publish --tag="error-logger-migrations"
    
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.
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
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