Laravel Logging Best Practices
Laravel's logging system is built on Monolog and supports sophisticated multi-channel configurations. Here's how to set it up for production.
Configure Log Channels
// config/logging.php
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['daily', 'stderr'],
],
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => 'info',
'days' => 14,
],
'stderr' => [
'driver' => 'monolog',
'handler' => StreamHandler::class,
'with' => ['stream' => 'php://stderr'],
'formatter' => JsonFormatter::class,
],
'audit' => [
'driver' => 'daily',
'path' => storage_path('logs/audit.log'),
'level' => 'info',
'days' => 90,
],
],Add Contextual Information
// Middleware to add request context
class LogContext
{
public function handle(Request $request, Closure $next)
{
Log::shareContext([
'request_id' => $request->header('X-Request-ID', Str::uuid()),
'user_id' => $request->user()?->id,
'ip' => $request->ip(),
]);
return $next($request);
}
}Structured Logging
// Good: structured data
Log::info('Order created', [
'order_id' => $order->id,
'user_id' => $order->user_id,
'total' => $order->total,
'items_count' => $order->items->count(),
]);
// Bad: string interpolation
Log::info("Order {$order->id} created by user {$order->user_id}");Log Levels Guide
- emergency/alert/critical — system is unusable, immediate action needed
- error — runtime errors, exception caught
- warning — unusual but handled (retry succeeded, deprecation)
- info — significant events (user login, order placed)
- debug — disabled in production
Sensitive Data
// config/logging.php — replace sensitive fields
'tap' => [RedactSensitiveData::class],
class RedactSensitiveData
{
public function __invoke($logger)
{
foreach ($logger->getHandlers() as $handler) {
$handler->pushProcessor(function ($record) {
// Redact sensitive fields from context
$sensitive = ['password', 'token', 'credit_card'];
foreach ($sensitive as $key) {
if (isset($record['context'][$key])) {
$record['context'][$key] = '[REDACTED]';
}
}
return $record;
});
}
}
}Pair your logging setup with Bugsly's error tracking. Logs provide the narrative context; Bugsly captures the exact exception with stack traces and request data.
Try Bugsly free, no card
Free plan, no card: 1 project, 100 new issues and 50,000 events a month, 7-day retention, 10 AI explanations a month. Paid plans from $19/month, 7-day money-back, cancel anytime.
Start free, no cardRelated Articles
Best Logging Tools for Laravel in 2026
Discover the top logging tools for Laravel developers in 2026. Compare features, pricing, and integrations to find the best fit for your projects.
Read moreSetting Up Session Replay in Laravel
Set up session replay for Laravel using Bugsly. Quick installation, configuration, and verification steps included.
Read moreBest Monitoring Tools for Laravel in 2026
Discover the top monitoring tools for Laravel developers in 2026. Compare features, pricing, and integrations to find the best fit for your projects.
Read moreSetting Up Uptime Monitoring in Laravel
Complete guide to integrating Bugsly uptime monitoring in your Laravel project. Get started in minutes with this tutorial.
Read moreFrom the same team
SEOBolt
Technical SEO and AI search audits with ready-to-apply fix plans.
Try SEOBolt →GrowFast
Domain in, qualified B2B leads out, with cold outreach and deliverability handled.
Try GrowFast →AutoShort
Turn a topic into a faceless short: AI script, voiceover and captions.
Try AutoShort →RemixViral
Paste a long video and get back the moments worth posting, cut vertical and captioned.
Try RemixViral →