基于日期的 Laravel 日志文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35587239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:17:19  来源:igfitidea点击:

Laravel log file based on date

phplaravellogginglaravel-5.2

提问by Fatih Akgun

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

默认情况下,laravel 将日志文件保存到名为 laravel.log 的单个日志文件中 /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.logfor the current date, so i need everyday a new log file saved to /storage/logs/

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

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

我认为我们可以通过扩展默认的Illuminate\Foundation\Bootstrap\ConfigureLogging引导程序类来做到这一点,但我不确定如何做到这一点

i would really appreciate it if anyone could help me.

如果有人可以帮助我,我将不胜感激。

Thanks in advance.

提前致谢。

回答by Tim Lewis

It's actually a lot simpler than that. In your config/app.phpyou'll see the line:

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

'log' => 'single',

closer to the bottom of the file. Laravel by default uses the singlemethod, which stores all errors in a single, expanding file. If you change this line to:

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

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

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

There's a few other methods available, so be sure to check out the official documentationfor 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.

此答案适用于 Laravel 5.2,这是原始问题中指定的版本。在 Laravel 的 never 版本中,日志配置已移动到它自己的配置文件中,如@ShanthaKumara 的回答(https://stackoverflow.com/a/51816907/3965631)所见。请不要建议修改以更改此答案以反映新版本。

回答by Shantha Kumara

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

在我使用的Laravel 5.6版本中,日志的配置文件是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.login the logs directory. The log directory will be like

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

Previouslyenter image description here

之前在此处输入图片说明

After applying rotation configurationthe 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

应用轮换配置后,目录将创建为当前日期创建的日志文件(如为今天2018-08-13创建的圆圈所示)。 在此处输入图片说明

回答by Serhii Andriichuk

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

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

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