How to set daily log in Laravel

How to set daily log in Laravel

In this article let’s understand how to set daily log in Laravel.

So we use logs to track data in background while the request are processing. Or to check any error or exceptions occurred while processing the request.

Laravel provides a way to Log anything which will basically stored in storage/logs/laravel.log single file. This is a very simple and default approach provided by Laravel.

But as your project grows, it is a headache to check logs from a single file as it is filled with too much data and errors and difficulties increases.

In such cases we always wants to arrange our logs in a way it can be easy to track.

Laravel provides a way to organize your logs. Logs in a daily basis which will create single file for each day and store it. We can configure it within below file, check syntax :

At config/logging.php

'channels' => [
    'stack' => [
        'driver' => 'stack',
        'channels' => ['daily'],
    ],

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

    'daily' => [
        'driver' => 'daily',
        'path' => storage_path('logs/laravel.log'),
        'level' => 'debug',
        'days' => 7, // The number of days to keep the logs before rotating them
    ],
    ...
]

Check above code we have changes at the line

 'channels' => ['single'] 

to

'channels' => ['daily'],

So, updating the channel to daily in configuration file will tell Laravel to create separate log file daily.

You can update other configuration such as ‘days’ => 14 , will keep last 14 days log files and discard the older automatically.

Above change will create files with date as below and so on:

laravel-2022-05-09.log

laravel-2022-05-10.log

laravel-2022-05-11.log

That’s all on how to set daily log in Laravel. Hope this finds you helpful.

Read more articles on Laravel here.