Laravel基于日期的日志文件

问题描述 投票:4回答:3

默认情况下,laravel将日志文件保存到位于/storage/logs/laravel.log中的名为laravel.log的单个日志文件中

我的问题是如何每天获取一个新的日志文件并存储当前日期的/storage/logs/laravel-2016-02-23.log等日志文件,所以我每天都需要一个新的日志文件保存到/storage/logs/

我想我们可以通过扩展默认的Illuminate\Foundation\Bootstrap\ConfigureLogging bootstraper类来做到这一点,但我不知道我怎么能这样做

如果有人能帮助我,我真的很感激。

提前致谢。

php laravel logging laravel-5.2
3个回答
12
投票

它实际上比这简单得多。在你的config/app.php,你会看到这条线:

'log' => 'single',

靠近文件的底部。 Laravel默认使用single方法,该方法将所有错误存储在单个展开文件中。如果您将此行更改为:

'log' => 'daily',

它会告诉Laravel你更喜欢有多个日志,每个日志都以错误发生的日期为后缀。

还有一些其他方法可用,所以请务必查看official documentation以获取更多信息。


7
投票

在我使用的Laravel 5.6版本中,日志记录的配置文件是config / logging.php

在那里你会发现以下部分

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

改变线

'channels' => ['single'],

'channels' => ['daily'],

那就像是:

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

它将以logs目录中的laravel-2018-08-13.log格式为每一天创建日志文件。日志目录就像

以前enter image description here

应用旋转配置后,目录将为当前日期创建日志文件(如今为2018-08-13创建的带圆圈的日志文件)。 enter image description here


1
投票

您还可以按年/月文件夹拆分每日日志(对于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,
        ],
    ],
];

完整的例子 - https://gist.github.com/andriichuk/893be964de09a96d90d33b56c9b32333

© www.soinside.com 2019 - 2024. All rights reserved.