-->

Laravel log file based on date

2020-08-20 17:52发布

问题:

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.

回答1:

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.



回答2:

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).



回答3:

You can also split daily logs by year/month folders (for Laravel >= 5.6)

<?php
    // ./config/logging.php
   return [
    /*
    |    ./storage/logs/
    |
    |    ├── 2018
    |    │   └── 12
    |    │       ├── laravel-2018-12-30.log
    |    │       └── laravel-2018-12-31.log
    |    └── 2019
    |        ├── 01
    |        │   ├── laravel-2019-01-01.log
    |        │   ├── laravel-2019-01-02.log
    |
    |        .....
    |
    |        │   └── laravel-2019-01-31.log
    |
    */
    'default' => env('LOG_CHANNEL', 'stack'),
    'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['daily'], // set 'daily' channel
        ],
        'daily' => [
            'driver' => 'daily',
            // add dynamic folder structure
            'path' => storage_path('logs/' . date('Y/m/') . 'laravel.log'),
            'level' => 'debug',
            // set the maximum number of days in a month
            'days' => 31,
        ],
    ],
];

Full example - https://gist.github.com/andriichuk/893be964de09a96d90d33b56c9b32333



回答4:

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.



回答5:

New log file every month

config/logging.php

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

        'single' => [
            'driver' => 'single',
            'path' => storage_path('logs/laravel' . date('-Y-m') . '.log'),
            'level' => 'debug',
        ],
        ...
];

laravel-2020-05.log

laravel-2020-06.log

laravel-2020-07.log

...