Laravel log file based on date
The laravel on daily logging is fine, but on 1st year, you will have 365 files of laravel.log
. My approach is to do the following on the 'path'
of the 'single'
channel.
storage_path('logs/' . date("Y") . '/' . date("m") . '/' . date("d") . '/' . 'laravel.log')
This way you have organize the logs by each year/month/day.
- 2021/
- 06/
- 01/
- laravel.log
- 01/
- 06/
It's actually a lot simpler than that. In your config/app.php
you'll see the line:
'log' => 'single',
closer to the bottom of the file. Laravel by default uses the single
method, which stores all errors in a single, expanding file. If you change this line to:
'log' => 'daily',
it will tell Laravel that you'd prefer to have multiple logs, each suffixed with the date of the when the error occurs.
There's a few other methods available, so be sure to check out the official documentation for more info.
This answer is for Laravel 5.2, which is the version specified in the original question. In never versions of Laravel, the Logging config has been moved to it's own config file, as seen by @ShanthaKumara's answer (https://stackoverflow.com/a/51816907/3965631). Please do not suggest edits to change this answer to reflect the new version.
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
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).
just open .env file and change
LOG_CHANNEL=stack
to
LOG_CHANNEL=daily
then run the command
php artisan config:cache
now i think your problem will solve.