DEV Community

Cover image for How to Log into a Custom Log File in Laravel
Laravel Daily tips
Laravel Daily tips

Posted on

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',
    ],
],
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

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
],
Enter fullscreen mode Exit fullscreen mode

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',
    ],
],
Enter fullscreen mode Exit fullscreen mode

And then log something like:

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

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}
Enter fullscreen mode Exit fullscreen mode

3. Log Based on Environment

Use .env to dynamically set the log channel:

LOG_CHANNEL=custom_payment
Enter fullscreen mode Exit fullscreen mode

In logging.php:

'default' => env('LOG_CHANNEL', 'stack'),
Enter fullscreen mode Exit fullscreen mode

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,
]);
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Original Blog Link

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

Top comments (0)