Log

Inject a logger with a named channel

Description: Injects a logger instance with an optional named channel for better log organization.

Namespace: Illuminate\Container\Attributes\Log

Added in: Laravel 11.20

Usage

use Illuminate\Support\Facades\Log;
use Psr\Log\LoggerInterface;

// Before (service provider):
$this->app->when(PaymentService::class)
    ->needs(LoggerInterface::class)
    ->give(fn () => Log::channel('payments'));

use Illuminate\Container\Attributes\Log;
use Psr\Log\LoggerInterface;

// After:
class PaymentService
{
    public function __construct(
        #[Log(channel: 'payments', name: 'custom_logger')] private readonly LoggerInterface $logger
    ) {}

    public function charge(int $amount): void
    {
        $this->logger->info('Charging customer', ['amount' => $amount]);
        // Logged under the 'payments' channel
    }
}

Without a channel name, it injects the default logger:

#[Log] private readonly LoggerInterface $logger