Logging is the heartbeat of every Laravel application. Whether you are debugging errors, monitoring performance, or auditing user activity, logs provide the trail of events that tell the story of what’s happening inside your app.
Laravel comes with Monolog integration by default, but its real power lies in how you configure, process, and utilize these logs. In this guide, we’ll explore the top 10 log processing methods in Laravel that every developer should know.
1. Using Laravel’s Built-in Monolog Integration
Laravel uses Monolog under the hood. This gives you access to multiple log handlers and channels.
You can configure log drivers in config/logging.php
:
'default' => env('LOG_CHANNEL', 'stack'),
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['daily', 'slack'],
],
]
With this setup, logs are sent both to daily log files and Slack notifications.
2. Daily Log Files
Using the daily
driver, Laravel creates a new log file each day.
This prevents massive single log files and makes troubleshooting easier.
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'days' => 14,
],
Here, logs are kept for 14 days before being rotated out.
3. Custom Log Channels
You can create dedicated channels for specific log types such as payments, authentication, or API requests. This keeps your logs organized.
'payment' => [
'driver' => 'single',
'path' => storage_path('logs/payment.log'),
'level' => 'info',
],
Usage:
Log::channel('payment')->info('Payment processed', ['user_id' => 101]);
4. Log Levels
Laravel supports all standard Monolog levels. Using the correct level helps filter logs more effectively:
debug()
– Detailed information for debugging.info()
– General application information.notice()
– Normal but significant events.warning()
– Something unexpected happened.error()
– Errors that require attention.critical()
– Critical conditions, app may not continue running.alert()
– Immediate action required.emergency()
– System unusable.
5. Contextual Logging
You can add extra context to your logs, making them easier to trace:
Log::error('Payment failed', [
'user_id' => 123,
'order_id' => 456,
'gateway' => 'Stripe'
]);
This way, logs are more meaningful and searchable.
6. Storing Logs in Database
Sometimes you need logs available for queries and analytics. You can create a custom Monolog handler or use packages like spatie/laravel-activitylog.
Example migration for a logs table:
Schema::create('logs', function (Blueprint $table) {
$table->id();
$table->string('level');
$table->text('message');
$table->json('context')->nullable();
$table->timestamps();
});
7. Centralized Logging with ELK Stack
For large applications, storing logs on servers isn’t enough. With the ELK stack (Elasticsearch, Logstash, Kibana), you can centralize logs, perform queries, and visualize data in real time.
Laravel can be configured to send logs to Logstash using
syslog
or JSON log drivers.
8. Using Laravel Telescope
Laravel Telescope is a debugging assistant that captures logs, queries, requests, exceptions, and more inside a user-friendly dashboard.
Install it with:
composer require laravel/telescope
Once enabled, you get a powerful UI to inspect logs and app activity in real time.
9. Cloud Logging (Papertrail, Loggly, Sentry)
Services like Papertrail, Loggly, and Sentry allow Laravel applications to send logs directly to the cloud.
Benefits include real-time alerts, advanced search, and integrations with Slack or email.
10. Structured JSON Logging
Instead of plain-text logs, JSON logs are machine-readable and work well with log processors and monitoring tools.
Configure in logging.php
:
'json' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.json'),
'tap' => [App\Logging\CustomizeFormatter::class],
],
Bonus: Best Practices for Laravel Logging
- Never log sensitive data like passwords or credit card details.
- Use
daily
logs for easier maintenance. - Set up log rotation to avoid huge files.
- Combine local logs with a centralized solution for production apps.
- Use structured logging for scalability.
Conclusion
Log processing in Laravel is more than just writing errors to a file. By combining Laravel’s native logging capabilities with tools like Telescope, ELK, or cloud platforms, you can build a powerful monitoring and debugging ecosystem.
Master these methods and your Laravel applications will be easier to debug, monitor, and scale.
Comments
Post a Comment