carcara/system-log
Pacote Laravel simples e configurável para registrar logs de acesso (requests). Publica config para ativar/desativar e ignorar campos, usa canal dedicado (ex.: accesslog) e aplica via middleware access.log em rotas ou grupos (ideal após autenticação).
Installation
composer require carcara/system-log
Publish the migration and config:
php artisan vendor:publish --provider="Carcara\SystemLog\SystemLogServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="Carcara\SystemLog\SystemLogServiceProvider" --tag="config"
Run the migration:
php artisan migrate
Basic Configuration
Edit config/system-log.php to define:
['/admin/*']).['/api/health']).App\Models\User).ip_address).First Use Case: Log All Admin Access
Add middleware to app/Http/Kernel.php:
'web' => [
\Carcara\SystemLog\Middleware\LogAccess::class,
// ... other middleware
],
Now, every request to /admin/* will auto-log user IP, timestamp, and route.
Logging User Actions Manually log specific events (e.g., sensitive operations):
use Carcara\SystemLog\Facades\SystemLog;
SystemLog::log('user.id', 'action', ['metadata' => 'value']);
Customizing Log Fields
Extend the Log model (app/Models/Log.php):
namespace App\Models;
use Carcara\SystemLog\Models\Log as BaseLog;
class Log extends BaseLog {
protected $casts = [
'metadata' => 'array',
'user_agent' => 'string',
];
}
Filtering Logs Query logs via Eloquent:
$logs = \App\Models\Log::where('user_id', auth()->id())
->where('action', 'delete')
->latest()
->get();
Integration with Events
Listen for model events (e.g., Creating):
use Carcara\SystemLog\Events\LogCreated;
LogCreated::dispatch($log);
'logged_routes' => [
'^/admin/(users|products)/\d+$',
],
throttle middleware to prevent log spam:
Route::middleware(['throttle:60,1', 'log'])->group(...);
Middleware Order Matters
Place LogAccess after auth middleware to avoid logging guest routes incorrectly.
// Wrong: Logs guests.
'web' => [LogAccess::class, Authenticate::class],
// Correct: Skips guests.
'web' => [Authenticate::class, LogAccess::class],
IP Address Issues
127.0.0.1 or ::1 in logs. Avoid null by ensuring Request::ip() works in tests.trusted_proxies in AppServiceProvider if behind a load balancer:
$this->app['request']->setTrustedProxies(['192.168.1.1']);
Performance
SystemLog::logBatch([
['user_id' => 1, 'action' => 'bulk_update'],
['user_id' => 2, 'action' => 'bulk_delete'],
]);
LogAccess::dispatch($request)->onQueue('logs');
app/Providers/AppServiceProvider.php:
public function boot() {
\Carcara\SystemLog\Models\Log::created(fn ($log) => tap($log)->toArray());
}
php artisan route:list to confirm LogAccess is registered.Custom Log Models
Override the Log model to add fields (e.g., device_type):
class Log extends BaseLog {
protected $fillable = ['device_type'];
}
Update the migration accordingly.
Export Logs Add a scheduled command to export logs to CSV/Excel:
use Carcara\SystemLog\Models\Log;
use Illuminate\Support\Facades\Storage;
class ExportLogsCommand extends Command {
public function handle() {
$logs = Log::all()->toArray();
Storage::put('logs.csv', array_to_csv($logs));
}
}
Webhook Notifications
Extend the LogCreated event to send alerts:
LogCreated::listen(function ($log) {
if ($log->action === 'password_reset') {
Http::post('https://alerts.example.com', ['log' => $log]);
}
});
How can I help you explore Laravel packages today?