作为程序员一定要保持良好的睡眠,才能好编程

laravel将日志记录在不同的文件

发布时间:2020-04-19

工作中,有这么一个场景:


1、web访问页面日志


2、运行脚本日志


在运行脚本的时候,会记录日志,但是记录的日志和 web访问记录的日志会在同一个文件中。

查看起来特别不方便,那么如何将脚本的日志记录在单独一个的一个文件中?




需要在配置文件中添加一个配置即可


config\logging.php


'aaa' => [
'driver' => 'daily',
'path' => storage_path('logs/aaa/aaa.log'),
'level' => 'debug',
'days' => 14,
],

调用的时候切一下
Log::channel('aaa')->info('aaaaaa');






<?php

use Monolog\Handler\StreamHandler;return [

    /*
    |--------------------------------------------------------------------------
    | Default Log Channel
    |--------------------------------------------------------------------------
    |
    | This option defines the default log channel that gets used when writing
    | messages to the logs. The name specified in this option should match
    | one of the channels defined in the "channels" configuration array.
    |
    */

    'default' => env('LOG_CHANNEL', 'stack'),

    /*
    |--------------------------------------------------------------------------
    | Log Channels
    |--------------------------------------------------------------------------
    |
    | Here you may configure the log channels for your application. Out of
    | the box, Laravel uses the Monolog PHP logging library. This gives
    | you a variety of powerful log handlers / formatters to utilize.
    |
    | Available Drivers: "single", "daily", "slack", "syslog",
    |                    "errorlog", "monolog",
    |                    "custom", "stack"
    |
    */

    'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['single', 'singleInfo', 'singleAlert', 'singleWarning', 'singleCritical', 'singleEmergency'],
        ],

        'single' => [
            'driver' => 'single',
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
        ],
        'singleInfo' => [
            'driver' => 'single',
            'path' => storage_path('logs/info.log'),
            'level' => 'info',
        ],
        'singleAlert' => [
            'driver' => 'single',
            'path' => storage_path('logs/alert.log'),
            'level' => 'alert',
        ],
        'singleWarning' => [
            'driver' => 'single',
            'path' => storage_path('logs/warning.log'),
            'level' => 'warning',
        ],
        'singleCritical' => [
            'driver' => 'single',
            'path' => storage_path('logs/critical.log'),
            'level' => 'critical',
        ],
        'singleEmergency' => [
            'driver' => 'single',
            'path' => storage_path('logs/emergency.log'),
            'level' => 'emergency',
        ],

        'daily' => [
            'driver' => 'daily',
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
            'days' => 0,
        ],

        'slack' => [
            'driver' => 'slack',
            'url' => env('LOG_SLACK_WEBHOOK_URL'),
            'username' => 'Laravel Log',
            'emoji' => ':boom:',
            'level' => 'critical',
        ],

        'stderr' => [
            'driver' => 'monolog',
            'handler' => StreamHandler::class,
            'with' => [
                'stream' => 'php://stderr',
            ],
        ],

        'syslog' => [
            'driver' => 'syslog',
            'level' => 'debug',
        ],

        'errorlog' => [
            'driver' => 'errorlog',
            'level' => 'debug',
        ],
    ],];