onamfc/laravel-devlogger
Laravel database logger with automatic exception catching and rich metadata. Tag and filter logs by level, date, queue, status, request context, and user. Includes configurable retention/cleanup, fallback channels, and flexible env-based setup.
Installation
composer require onamfc/laravel-devlogger
php artisan devlogger:install # New unified command (replaces manual publish/migrate)
config/devlogger.php) and creates the required logs table.Basic Configuration
config/devlogger.php to define:
log_level (e.g., debug, error, critical)enabled (set to false in production)table_name (default: logs)purge_old_logs (auto-cleanup interval in days)log_level now set to debug (previously error in some setups).First Use Case: Automatic Error Logging
php artisan devlogger:list
php artisan devlogger:purge
Replace Log::error() with DevLogger’s fluent interface:
use Onamfc\DevLogger\Facades\DevLogger;
// Log with context (auto-attaches user, IP, etc.)
DevLogger::error("Failed to process order", [
'order_id' => 123,
'user_id' => auth()->id(),
]);
// Log with custom metadata
DevLogger::debug("API request received", [
'endpoint' => 'users/create',
'payload' => $request->all(),
]);
Log HTTP requests/responses in middleware:
use Onamfc\DevLogger\Facades\DevLogger;
public function handle($request, Closure $next)
{
$start = microtime(true);
$response = $next($request);
$duration = microtime(true) - $start;
DevLogger::info("Request processed", [
'method' => $request->method(),
'url' => $request->fullUrl(),
'duration' => $duration,
'status' => $response->getStatusCode(),
]);
return $response;
}
Use the DevLogger facade to fetch logs programmatically:
// Get last 10 errors for a user
$logs = DevLogger::query()
->where('level', 'error')
->where('context->user_id', auth()->id())
->orderBy('created_at', 'desc')
->limit(10)
->get();
// Filter by custom metadata
$logs = DevLogger::query()
->where('context->order_id', 123)
->get();
Add to app/Console/Kernel.php:
protected function schedule(Schedule $schedule)
{
$schedule->command('devlogger:purge')->dailyAt('03:00');
}
Auto-attach metadata via service provider binding:
// In AppServiceProvider@boot()
DevLogger::extend(function ($logger) {
$logger->setContext('custom_key', 'custom_value');
});
Configure retention policies directly in config/devlogger.php:
'retention' => [
'max_days' => 30, // Default: 30 days
'soft_delete' => true, // Enable soft deletes (default: false)
],
Deprecated Commands
php artisan vendor:publish --provider="Onamfc\DevLogger\DevLoggerServiceProvider" is no longer needed.php artisan devlogger:install instead (handles config + migrations).Performance Overhead
DevLogger::disable() in performance-critical paths:
DevLogger::disable();
// ... bulk operations ...
DevLogger::enable();
Context Data Size Limits
context column uses JSON storage. Large payloads may bloat the DB.DevLogger::logRaw() for large payloads.Log Level Defaults
log_level is now debug (previously error in some setups).log_level in config/devlogger.php if stricter filtering is needed.Soft Delete Conflicts
soft_delete is enabled but the logs table lacks a deleted_at column, migrations will fail.php artisan devlogger:install --force to update the schema.DB::enableQueryLog();
DevLogger::error("Test");
dd(DB::getQueryLog());
DevLogger::getLastLog() to debug context attachment:
$lastLog = DevLogger::getLastLog();
dd($lastLog->context);
devlogger:purge fails:
purge_old_logs config (must be > 0).deleted_at column exists.Custom Log Levels
Extend the level column by modifying the migration or using a trait:
// app/Models/Log.php
use Onamfc\DevLogger\Traits\LogLevelTrait;
class Log extends Model
{
use LogLevelTrait;
protected $casts = [
'level' => 'string',
];
}
Web Interface
Build a custom admin panel using the logs table. Example query for a dashboard:
$stats = DevLogger::query()
->selectRaw('level, count(*) as count')
->groupBy('level')
->get();
Slack/Email Alerts
Hook into log events via the DevLogger event system:
// In EventServiceProvider@boot()
DevLogger::listen(function ($log) {
if ($log->level === 'critical') {
Notification::route('slack', config('services.slack.webhook'))
->notify(new CriticalLogAlert($log));
}
});
Rate Limiting Prevent log spam by throttling in middleware:
use Illuminate\Cache\RateLimiter;
public function handle($request, Closure $next)
{
$limiter = app(RateLimiter::class);
if ($limiter->tooManyAttempts($this->throttleKey($request), 10)) {
DevLogger::warning("Rate limit exceeded for IP: {$request->ip()}");
}
return $next($request);
}
New: Retention Policies
devlogger:prune to manually enforce retention:
php artisan devlogger:prune --days=7 # Delete logs older than 7 days
app/Console/Kernel.php:
$schedule->command('devlogger:prune --days=' . config('devlogger.retention.max_days'))
->dailyAt('02:00');
How can I help you explore Laravel packages today?