Configuring (and viewing!) logs in Laravel
November 21, 2024
Laravel is a flexible, modern, powerful PHP framework, and its logging system is no different.
From simple file-based logging to more advanced setups with Laravel Telescope, Pail, or Sentry, we're going to walk through all the ways you can configure and optimize logging in Laravel.
Prefer to watch instead of read? Check out the video version of this article on YouTube: Configuring (and viewing!) logs in Laravel
Understanding the basics of logging in Laravel
At its core, Laravel uses a system of "channels" to give you
flexibility. These channels provide a unified interface to send messages to different places—local files, Slack, Sentry,
and more. If you've ever seen the storage/logs/laravel.log
file, you're already familiar with Laravel's default file
logging.
Dumping logs into a file is a good start, but it isn't always enough. That's where Laravel's more advanced logging tools come into play.
What's in the logging.php configuration file
Laravel's logging configuration is stored in config/logging.php
. Each channel is configurable and can handle different
levels of log (e.g. a debug error, an info message, or a critical error) and can send the logs to different drivers (
e.g., file, Slack, or Sentry.)
For Slack you may only send the most critical errors, while you may write all logs to a local file.
In newer releases of Laravel, it now uses environment variables to make it a little easier to manage your log channels
without having to mess around with the config/logging.php
file.
You can see here the default log channel is set to an environment variable called LOG_CHANNEL
which defaults
to stack
:
'default' => env('LOG_CHANNEL', 'stack'),
Down a bit further in the config/logging.php
file, we can see a channels
array that defines the different log
channels available. One channel is called stack
:
// config/logging.php 'channels' => [ 'stack' => [ 'driver' => 'stack', 'channels' => explode(',', env('LOG_STACK', 'single')), 'ignore_exceptions' => false, ],]
We're going to come back to this stack
channel in a moment, but first, let's look at some of the other channels.
In this example, we have a single channel called daily
that writes logs to a file. The stack
channel is a special
type that groups multiple channels together. This is useful for sending logs to different destinations based on
severity.
Basic file logging setup
The simplest logging setup involves writing logs to a file. By default, Laravel logs all messages (usually at the debug
level) to storage/logs/laravel.log
. You can take a look at these logs in real time with this terminal command:
tail -f storage/logs/laravel.log
It's a simple, no-frills solution, but it works. That said, file logging can get cumbersome when you're dealing with lots of messages. If you're looking for a bit more clarity or functionality, Laravel has plenty of tools to improve your logging game, starting with Laravel Pail.
Laravel Pail
If you're tired of staring at tail
output and want something with a little more polish, Laravel Pail is a great
alternative. Pail adds helpful context to your logs, like the request route and the user who triggered the log.
Installing it is a breeze:
composer require laravel/pailphp artisan pail
Not only does it present logs in a cleaner format, but it also highlights the severity of messages (e.g., info, error) and shows relevant route details, making it easier to debug your app.
Here's an example of what the Pail output looks like:
INFO Tailing application logs. Press Ctrl+C to exit Use -v|-vv to show more details ┌ 18:06:26 INFO ─────────────────────────────────────────────────────────────────────────┐│ Hello world │└──────────────────────────────────────────────────────────────────────── artisan tinker ┘
Laravel Telescope
If you prefer a graphical interface, Laravel Telescope is your go-to. It's like having a full-blown control panel for your local development environment, showing you everything from requests and exceptions to database queries and scheduled jobs.
To get started with Telescope, simply install it with:
composer require laravel/telescopephp artisan telescope:install
Once installed, Telescope opens a web interface that provides detailed insights into your app's performance. While it's fantastic for local development, it's important to remember that Telescope isn't recommended for production due to security and performance concerns.
External logging services
When it's time to push your application to production, you'll need more robust logging solutions. Enter: Sentry and Slack, two great options for catching critical errors in real time.
Sentry
Sentry is an error-tracking service that captures exceptions, stack traces, and more. Integrating it with Laravel is simple:
composer require sentry/sentry-laravel
After that, just set the log channel to sentry in your .env file:
// .envLOG_CHANNEL=sentry
Now, every critical error will be sent to Sentry's dashboard for easy tracking and analysis.
Slack
If your team lives on Slack, you can configure Laravel to send logs directly to a Slack channel. Set up a Slack webhook and specify which log levels should be sent (hint: stick to critical errors to avoid overwhelming your team).
Configuring log channels and stacks
Here's where Laravel's logging system really shines—log channels. A channel represents where your logs are sent, and you can set up different channels for different purposes. For instance, you might log everything to a file but only send critical errors to Sentry or Slack.
Laravel also allows you to group multiple channels into a stack. This is handy for sending messages to different destinations based on severity. For example, you could log all debug messages to a file while sending critical errors to Sentry and Slack.
Here's how you might configure a stack channel in config/logging.php
:
'channels' => [ 'stack' => [ 'driver' => 'stack', 'channels' => ['daily', 'slack', 'sentry'], ], 'daily' => [ 'driver' => 'daily', 'path' => storage_path('logs/laravel.log'), 'level' => 'debug', 'days' => 14, ], 'slack' => [ 'driver' => 'slack', 'url' => env('SLACK_WEBHOOK_URL'), 'level' => 'critical', ], 'sentry' => [ 'driver' => 'sentry', 'level' => 'error', ],],
In this example, debug messages are written to a daily log file, critical messages go to Slack, and errors are forwarded to Sentry. It's a great way to keep logs organized and ensure that your team is notified when something goes wrong.
Best practices for production logging
When logging in production, a little planning goes a long way. Too many logs can slow down your app or overwhelm your team. Here are a few best practices:
- Use different log levels: Only send critical or error-level logs to services like Sentry or Slack. This prevents unnecessary noise.
- Rotate logs: Use the daily driver to rotate log files and avoid massive log dumps that are hard to navigate.
- Log retention: Adjust how long logs are kept based on your environment—shorter for staging, longer for production.
Final thoughts
Laravel offers a powerful, flexible logging system that can be tailored to fit your exact needs, whether you're in development or production. From basic file logging to advanced solutions like Sentry and Telescope, Laravel makes it easy to monitor and debug your application effectively.
By leveraging these tools and best practices, you'll be well-equipped to diagnose issues, optimize performance, and keep your Laravel app running smoothly.
YouTube video
With these strategies in hand, you'll be the master of logging in no time! (And don't forget to check out the video version of this guide on YouTube!)