How to Log into a Custom Log File in Laravel

Laravel, by default, writes logs into a single laravel.log file in storage/logs. But what if you want to separate your logs, maybe to track a specific module like payments or API calls?

In this guide, you’ll learn how to log to a separate file, followed by advanced tips for better log management in Laravel.

Step 1: Define a New Log Channel

Open config/logging.php and define your custom log channel inside the channels array:

'channels' => [

    // ... other channels

    'custom_payment' => [
        'driver' => 'single',
        'path' => storage_path('logs/payment.log'),
        'level' => 'debug',
    ],
],

This tells Laravel to create a new log file at:
storage/logs/payment.log.

Step 2: Log into the Custom Channel

Now, in your code, use:

use Illuminate\Support\Facades\Log;

Log::channel('custom_payment')->info('Payment process started for order ID: 12345');

That’s it! This will write logs only to payment.log.

Advanced Tips (2025-Level)

1. Use Daily Logs for Rotation

Prevent massive log files with daily log rotation:

'custom_payment' => [
    'driver' => 'daily',
    'path' => storage_path('logs/payment.log'),
    'level' => 'debug',
    'days' => 14, // keep logs for 14 days
],

2. Format Log Output (Customize Log Format)

To make logs more readable or add contextual information:

'custom_payment' => [
    'driver' => 'monolog',
    'handler' => Monolog\Handler\StreamHandler::class,
    'formatter' => Monolog\Formatter\LineFormatter::class,
    'with' => [
        'stream' => storage_path('logs/payment.log'),
    ],
    'formatter_with' => [
        'format' => "[%datetime%] %channel%.%level_name%: %message% %context%\n",
        'dateFormat' => 'Y-m-d H:i:s',
    ],
],

And then log something like:

Log::channel('custom_payment')->info('Payment processed', [
    'order_id' => 98765,
    'user_id' => 123,
]);

The Output in payment.log will look like:

[2025-07-29 19:45:32] custom_payment.INFO: Payment processed {"order_id":98765,"user_id":123}

3. Log Based on Environment

Use .env to dynamically set the log channel:

LOG_CHANNEL=custom_payment

In logging.php:

'default' => env('LOG_CHANNEL', 'stack'),

This helps switch between channels in local, staging, or production environments easily.

4. Contextual Logging

Use context data to attach extra info:

Log::channel('custom_payment')->info('Payment completed', [
    'user_id' => auth()->id(),
    'order_id' => 123,
    'amount' => 500,
]);

This is especially useful when debugging API calls or tracing actions.

Bonus: Log Viewer Package

To visually inspect logs in your browser:

composer require opcodesio/log-viewer

Visit /log-viewer in your browser. It supports filtering, searching, and more!

🔗 Log Viewer by opcodesio

Conclusion

Laravel’s logging system is powerful and highly customizable. Whether you’re managing logs per module or setting up advanced Monolog formatting, these techniques will give you clean, organized, and maintainable logs.

Want more Laravel tips?

Visit LaravelDailyTips for practical guides, interview questions, and tricks.

Subscribe now and get battle-tested Laravel insights delivered to your inbox before anyone else!

0
Subscribe to my newsletter

Read articles from Laravel Daily tips directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Laravel Daily tips
Laravel Daily tips

As a FULL-Stack, TALL Stack developer, and owner of laraveldailytips.com, I am from Pakistan. My passion is to write short and useful tips and tricks that can assist other people who are trying to learn something new and helpful. Since the beginning, I have loved PHP, Laravel, VueJS, JavaScript, jQuery, and Bootstrap. I believe in hard work combined with consistency.