Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel log file based on date

By default laravel saves the log file to a single log file called laravel.log located in /storage/logs/laravel.log

my question is how can i get a new log file everyday and store the log files like /storage/logs/laravel-2016-02-23.log for the current date, so i need everyday a new log file saved to /storage/logs/

i think we can do that by extending the default Illuminate\Foundation\Bootstrap\ConfigureLogging bootstraper class but i'm not sure how i can do that

i would really appreciate it if anyone could help me.

Thanks in advance.

like image 364
Fatih Akgun Avatar asked Feb 23 '16 20:02

Fatih Akgun


1 Answers

In the version of Laravel 5.6 that I am using, the configuration file for logging is config/logging.php

There you will find the following section

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

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

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

Change the line

'channels' => ['single'],

into

'channels' => ['daily'],

Then it will be like:

'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,
    ],
    ...
]

It will create log files for each day in the format laravel-2018-08-13.log in the logs directory. The log directory will be like

Previously enter image description here

After applying rotation configuration the directory is having the log file created for the current date (as circled one which is created for today 2018-08-13). enter image description here

like image 122
Shantha Kumara Avatar answered Sep 28 '22 16:09

Shantha Kumara