Master production-grade logging and monitoring in Laravel. Learn to configure custom log channels, track application errors, and integrate external services.
Previously in this course, we explored handling global exceptions in Laravel to ensure our API returns consistent error structures. While catching exceptions is vital, it’s only half the battle; without a robust strategy for logging and monitoring, those errors remain invisible until a user reports them.
In this lesson, we’ll move beyond the default storage/logs/laravel.log file. We’ll configure multi-channel logging and integrate an external service to turn our application’s silent failures into actionable alerts.
Laravel uses the Monolog library, which is the industry standard for PHP. At its core, logging in Laravel is built around "channels." A channel represents a target destination for your logs—whether that’s a local file, the system syslog, an email alert, or a third-party service like Sentry or Bugsnag.
You define these channels in config/logging.php. By default, Laravel uses the stack channel, which allows you to send a single log message to multiple destinations simultaneously.
For our project board, we want to separate critical system failures from standard informational logs. Let's create a custom channel for our "billing" or "integration" events to keep them isolated.
In config/logging.php, add a new channel:
PHP'channels' => [ 'stack' => [ 'driver' => 'stack', 'channels' => ['daily'], ], 'integration' => [ 'driver' => 'single', 'path' => storage_path('logs/integrations.log'), 'level' => 'info', ], ],
Now, you can write logs to this specific channel anywhere in your application:
PHPuse Illuminate\Support\Facades\Log; Log::channel('integration')->info('External API call initiated', ['project_id' => $project->id]);
While local log files are useful during development, they are insufficient for production. In a distributed environment, you need centralized monitoring to aggregate logs, track stack traces, and notify your team via Slack or PagerDuty when things break.
Most production apps use services like Sentry, Honeybadger, or Flare. These services hook into Laravel’s exception handler automatically.
Install the SDK:
composer require sentry/sentry-laravel
Configure the DSN:
Add your project DSN to your .env file:
SENTRY_LAR_DSN=https://your-key@sentry.io/project-id
Automatic Reporting:
Because Sentry integrates with Laravel’s App\Exceptions\Handler, it will automatically capture any unhandled exception. You don’t need to do anything else to start tracking critical errors.
To practice, let's add a "Security" log channel that tracks sensitive actions, such as when a user deletes a project.
security channel to config/logging.php using the daily driver to rotate files automatically.Log into your ProjectController@destroy method.Log::channel('security')->warning('Project deleted', ['user_id' => auth()->id(), 'project_id' => $project->id]);.storage/logs/ after triggering the action.Log::withoutContext() or carefully filter arrays before passing them to logs.single driver, your log file will grow indefinitely. Always use daily or stack with days retention configured in your channel settings.try-catch block but fail to log the exception inside the catch block, the error will never reach your monitoring service. Always log caught exceptions: Log::error($e->getMessage(), ['trace' => $e->getTraceAsString()]);.Effective debugging starts with visibility. By segmenting your logs into channels, you avoid noise and ensure that critical events are easy to find. By integrating external monitoring services, you shift from reactive troubleshooting to proactive maintenance, catching issues before your users do.
We’ve advanced our project board by setting up a dedicated security log, ensuring that every sensitive deletion is tracked and audit-ready.
Up next: We will dive into Job Chaining and Batching to handle complex, multi-step background processes efficiently.
Master non-breaking migrations and safe rollback procedures. Learn the expand-and-contract pattern to evolve your database schema without production downtime.
Read moreMaster production-grade asset management in Laravel. Learn to implement file hashing for versioning and configure Vite to scale your frontend performance via CDN.
Logging and Monitoring