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.
Installation:
composer require justbetter/laravel-error-logger
php artisan vendor:publish --provider="JustBetter\ErrorLogger\ServiceProvider"
php artisan migrate
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);
}
Verify Storage:
Check the error_logs table in your database for the recorded entry.
Log critical exceptions (e.g., payment failures, API timeouts) and review them via:
error_logs table)justbetter/nova-error-logger)Manual Logging:
ErrorLogger::log($exception) in catch blocks for critical exceptions.try {
$response = Http::post('https://api.example.com/fail');
} catch (\Exception $e) {
ErrorLogger::log($e, ['request_data' => $request->all()]);
}
Contextual Data: Attach metadata to logs for debugging:
ErrorLogger::log($e, [
'user_id' => auth()->id(),
'ip' => request()->ip(),
'route' => route()->getName(),
]);
Scheduler Integration:
App\Console\Kernel.php:
$schedule->job(new \JustBetter\ErrorLogger\Jobs\ErrorNotificationJob())
->dailyAt('09:00');
.env:
LARAVEL_ERROR_NOTIFICATION_CHANNEL=slack
Nova Integration (if installed):
Error Logs resource.public function handle($request, Closure $next) {
try {
return $next($request);
} catch (\Exception $e) {
ErrorLogger::log($e);
throw $e; // Re-throw to maintain flow
}
}
ErrorLogger in tests:
$this->partialMock(ErrorLogger::class, function ($mock) {
$mock->shouldReceive('log')->once();
});
Manual Logging Required:
try-catch and log manually.Database Bloat:
error_logs table.php artisan laravel-error-logger:prune regularly (e.g., weekly via scheduler).Notification Delays:
ErrorNotificationJob) may lag if the server is offline.php artisan schedule:work.Nova Dependency:
justbetter/nova-error-logger. Without it, logs are database-only.Missing Logs:
error_logs table exists and is writable.storage/logs/laravel.log) for migration errors.Notification Failures:
LARAVEL_ERROR_NOTIFICATION_CHANNEL (e.g., slack) is configured in .env.php artisan laravel-error-logger:notify
Custom Channels:
Extend notification support by implementing JustBetter\ErrorLogger\Contracts\NotificationChannel:
class EmailChannel implements NotificationChannel {
public function send(array $errors) { /* ... */ }
}
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');
});
});
Prune Logic: Customize the prune command by publishing and modifying the migration:
php artisan vendor:publish --tag="error-logger-migrations"
How can I help you explore Laravel packages today?