Installation Add the package via Composer:
composer require rigits/laravel-log-lens
Publish the config (optional):
php artisan vendor:publish --provider="Rigits\LogLens\LogLensServiceProvider"
Basic Usage
Inject the LogLens facade into a controller/service:
use Rigits\LogLens\Facades\LogLens;
public function someAction()
{
LogLens::info('User logged in', ['user_id' => 123]);
}
Logs will now appear in your configured channel (default: single channel).
First Use Case
Replace Log:: calls with LogLens:: for structured logging with automatic context (e.g., request IDs, user IDs).
Contextual Logging Attach metadata (e.g., user, request) automatically:
LogLens::withContext(['user_id' => auth()->id()])
->info('Order processed', ['order_id' => $order->id]);
Middleware Integration Use middleware to inject request context:
public function handle($request, Closure $next)
{
LogLens::withContext(['request_id' => $request->header('X-Request-ID')]);
return $next($request);
}
Channel-Specific Logging
Route logs to different channels (e.g., stack for production):
LogLens::channel('stack')->error('Failed payment', ['amount' => 100]);
Exception Handling Log exceptions with stack traces:
try {
// Risky code
} catch (\Exception $e) {
LogLens::error('Payment failed', ['exception' => $e]);
}
LogLens::queue() to defer log processing.LogLens to add pre-logging transformations (e.g., masking PII).Context Leakage
Avoid logging sensitive data (e.g., passwords) in contexts. Use LogLens::mask():
LogLens::mask(['password' => '*****'])->info('Login attempt');
Performance Overhead Excessive context attachment can slow down requests. Limit to essential metadata.
Channel Misconfiguration
Ensure the log-lens channel is properly defined in config/logging.php:
'channels' => [
'log-lens' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
],
],
LogLens::debug(), LogLens::info(), etc., to filter logs in production.LogLens::getContext(); // Returns current context array
php artisan queue:work
Custom Context Providers Bind a service to inject dynamic context:
LogLens::extendContext(function () {
return ['tenant_id' => Tenant::current()->id];
});
Log Formatting
Override the formatter via the LogLensServiceProvider:
$this->app->singleton('log-lens.formatter', function () {
return new CustomFormatter();
});
Event Listeners
Listen for log events (e.g., LogLens\Events\LogEntryCreated):
LogLens::listen(function ($entry) {
// Post-process log entry
});
How can I help you explore Laravel packages today?