35

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.

5 Answers 5

69

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

6
  • 4
    The stack driver is making it possible to use multiple drivers at the same time. If you don't need that, you can simply set the default to daily instead of stack. You can do that by setting it in your .env file as LOG_CHANNEL=daily
    – Matthias S
    Commented May 27, 2020 at 11:49
  • 11
    Keep in mind that the daily driver deletes the logs after the amount of days set under the days key . To keep them forever, just set it to 0.
    – totymedli
    Commented Sep 7, 2020 at 15:53
  • not working , i have set from many days 'default' => env('LOG_CHANNEL', 'daily'), but not working . single file laravel.log being generate.
    – pankaj
    Commented Oct 25, 2020 at 10:24
  • 2
    @totymedli out of curiosity, where one can find detail in Laravel docs which says if we set 0 to days it will not delete the logs. I have hard time figuring out which params channel supports and what they do. i can't find it in laravel docs.
    – DS9
    Commented Feb 5, 2021 at 13:24
  • 3
    @DS9 Just search for daily in the framework source code. In LogManager they pass the value to Monolog's RotatingFileHandler where the value is checked and if it is 0 the GC is turned off.
    – totymedli
    Commented Feb 5, 2021 at 21:30
36

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.

0
21

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.

4

To generate a new file for everyday in Laravel, all you need to do is change 'channels' value in the \config\logging.php file from 'single' to 'daily'.

'channels' => [
    'stack' => [
        'driver' => 'stack',
        'channels' => ['daily'],
        'ignore_exceptions' => false,
    ], 
3

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
6
  • 1
    I like this suggestion. However, Laravel 8 caches configuration. This means the cache would need to be cleared every day at midnight. What was your solution? Commented Jun 20, 2021 at 5:26
  • Hey @John Hanley. Can you elaborate a better question, I don't know what caching has in relation with logging.
    – francisco
    Commented Jun 21, 2021 at 8:44
  • 1
    Laravel caches the logging configuration. Your date would become static and not change each day. Commented Jun 21, 2021 at 18:39
  • Laravel doesn't cache logging config. The database cache driver to load the logging files is unecessary and unpractic.
    – francisco
    Commented Jun 22, 2021 at 11:05
  • What version are you using? Laravel 8 caches the logging configuration in bootstrap/cache/config.php. Commented Jun 22, 2021 at 17:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.