15 Laravel 11 Basics: Logging

Anis MarrouchiAI Bot
By Anis Marrouchi & AI Bot ·

Loading the Text to Speech Audio Player...

Master the essentials of logging in Laravel 11 with this comprehensive guide. Improve your application's monitoring and debugging capabilities.

Configuration

All of the configuration options that control your application's logging behavior are housed in the config/logging.php configuration file. This file allows you to configure your application's log channels. By default, Laravel uses the stack channel to aggregate multiple log channels into a single channel.

// config/logging.php
return [
    'default' => env('LOG_CHANNEL', 'stack'),
    'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['single', 'slack'],
        ],
        // Other channel configurations...
    ],
];

Available Channel Drivers

Each log channel is powered by a "driver". The following log channel drivers are available in every Laravel application:

  • single: A single file or path-based logger.
  • daily: A rotating file handler that rotates daily.
  • slack: Sends log messages to Slack.
  • syslog: Sends log messages to the syslog.
  • errorlog: Sends log messages to the system error log.
  • custom: Uses a specified factory to create a channel.
// config/logging.php
return [
    'channels' => [
        'single' => [
            'driver' => 'single',
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
        ],
        'slack' => [
            'driver' => 'slack',
            'url' => env('LOG_SLACK_WEBHOOK_URL'),
            'username' => 'Laravel Log',
            'emoji' => ':boom:',
            'level' => 'critical',
        ],
    ],
];

Writing Log Messages

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

use Illuminate\Support\Facades\Log;
 
Log::emergency('Emergency message');
Log::alert('Alert message');
Log::critical('Critical message');
Log::error('Error message');
Log::warning('Warning message');
Log::notice('Notice message');
Log::info('Info message');
Log::debug('Debug message');

Contextual Information

An array of contextual data may be passed to the log methods. This contextual data will be formatted and displayed with the log message.

use Illuminate\Support\Facades\Log;
 
Log::info('User {id} failed to login.', ['id' => $user->id]);

You may also share contextual information across all logging channels using the Log::shareContext method.

use Illuminate\Support\Facades\Log;
 
Log::shareContext(['request_id' => $requestId]);

Customizing Log Channels

You can customize Monolog for channels by defining a tap array on the channel's configuration. This array should contain a list of classes that can modify the Monolog instance.

// config/logging.php
return [
    'channels' => [
        'single' => [
            'driver' => 'single',
            'tap' => [App\Logging\CustomizeFormatter::class],
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
        ],
    ],
];

Define the class that will customize your Monolog instance:

namespace App\Logging;
 
use Monolog\Formatter\LineFormatter;
use Monolog\Logger;
 
class CustomizeFormatter
{
    public function __invoke(Logger $logger)
    {
        foreach ($logger->getHandlers() as $handler) {
            $handler->setFormatter(new LineFormatter(
                '[%datetime%] %channel%.%level_name%: %message% %context% %extra%'
            ));
        }
    }
}

Conclusion

Understanding Laravel's logging system is essential for monitoring and debugging your application. By configuring and customizing log channels, you can ensure that critical information is recorded and easily accessible.

For more detailed information, refer to the official Laravel 11 Logging Documentation.


Want to read more tutorials? Check out our latest tutorial on Implementing RAG on PDFs Using File Search in the Responses API.

Discuss Your Project with Us

We're here to help with your web development needs. Schedule a call to discuss your project and how we can assist you.

Let's find the best solutions for your needs.