Skip to content

Laravel - Errors & Logging

When you start a new Laravel project, error and exception handling is already configured for you. The App\Exceptions\Handler class is where all exceptions thrown by your application are logged and then rendered back to the user. Laravel aims to provide detailed error information during development and generic, user-friendly error pages in production.

The APP_DEBUG environment variable in your .env file controls how much information about an error is actually displayed to the user.

  • For local development, APP_DEBUG should be set to true. This will cause Laravel to display detailed error pages with stack traces, helping you debug issues.
  • For production environments, APP_DEBUG should always be set to false. If set to true in production, you risk exposing sensitive configuration values or application internals to end users.

When APP_DEBUG is false, Laravel will display a generic error page. You can customize these error pages by creating Blade templates in the resources/views/errors directory (e.g., 404.blade.php for ‘Not Found’ errors, 500.blade.php for ‘Server Errors’).

Note: After changing the APP_DEBUG variable in your .env file, if you have cached your configuration (php artisan config:cache), you should clear the cache (php artisan config:clear) and re-cache it for the change to take effect, or simply restart your development server if not using config caching.

Laravel makes it easy to display custom error pages for various HTTP status codes. For example, to create a custom page for 404 Not Found errors, create a file at resources/views/errors/404.blade.php. Laravel will automatically render this view when a 404 error occurs.

The abort helper can be used to trigger these error pages from anywhere in your application:

if (!Auth::user()->isAdmin()) {
abort(403, 'Unauthorized action.'); // Triggers resources/views/errors/403.blade.php
}

Logging is an essential mechanism for understanding and debugging application behavior, especially in production. Laravel provides a robust logging system built on top of the popular Monolog PHP logging library. By default, Laravel is configured to create a daily log file for your application stored in the storage/logs directory.

Logging behavior is configured in the config/logging.php file. This file allows you to configure your application’s log channels. A channel represents a specific way of writing log information. For example, you might log all debug messages to a local file but send critical errors to a service like Slack or Sentry.

Laravel supports several built-in log drivers/channels:

  • single: A single log file (storage/logs/laravel.log).
  • daily: Rotates log files daily.
  • slack: Sends log messages to a Slack channel.
  • syslog: Writes logs to the system log.
  • errorlog: Uses PHP’s error_log function.
  • stack: Allows you to combine multiple channels into one (e.g., log to both a file and Slack).

You can set the default log channel using the LOG_CHANNEL environment variable in your .env file. For example:

LOG_CHANNEL=stack

You can write information to the logs using the Log facade. The logger provides eight logging levels defined in RFC 5424: emergency, alert, critical, error, warning, notice, info, and debug.

use Illuminate\Support\Facades\Log;
Log::emergency($message);
Log::alert($message);
Log::critical($message);
Log::error($message);
Log::warning($message);
Log::notice($message);
Log::info('User logged in: ' . $user->id);
Log::debug('Debugging an array:', ['key' => 'value']);

Contextual information can also be passed to the log methods as an array:

Log::info('User {id} failed to login.', ['id' => $userId, 'attempt_ip' => $request->ip()]);

The generated log entries can typically be found in storage/logs/laravel.log (or laravel-YYYY-MM-DD.log if using the daily driver). Regularly reviewing logs is crucial for monitoring application health and troubleshooting issues.

For more advanced logging configurations, such as customizing Monolog formatters or using specific channels, refer to the official Laravel logging documentation.