Configuration

Configuration file config/logging.php

By default, Laravel uses the stack channel to record log information, and the stack channel is used to aggregate multiple log channels into a single channel.

Example: The single channel is written to the larave.log file by default, and the daily channel is written to the larave-*.log file by default. If the stack is configured as follows

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

The log will be written to the larave.log and larave-*.log files at the same time

Log level

LOG_LEVEL=The lowest "level" that debug log information must be recorded by the channel

Emergency , Alert , Critical , error , warning , Notice , info and Debug

Assuming LOG_LEVEL=error, Log::debug('An informational message.'); will not record the log

Write log information

Log::emergency($error);
Log::alert($error);
Log::critical($error);
Log::error($error);
Log::warning($error);
Log::notice($error);
Log::info($error);
Log::debug($error);

Write context information

Log::info('User failed to login.', ['id' => 123]);

//local.INFO: User failed to login. {"id":123} 

Write to the specified channel

'test' => [
  'driver' => 'single',
  'path' => storage_path('logs/laravel.log'),
],

Log::channel('test')->info('Something happened!');

Channel customization

'single' => [
    'driver' => 'single',
    'tap' => [App\Logging\CustomizeFormatter::class],
    'path' => storage_path('logs/laravel.log'),
    'level' => 'debug',
],
Note: All "tap" classes are service container , so all the constructor dependencies they need will be automatically injected.
<?php

namespace App\Logging;

use Monolog\Formatter\LineFormatter;

class CustomizeFormatter
{
    /**
     * Customize the given logger instance.
     *
     * @param  \Illuminate\Log\Logger  $logger
     * @return void
     */
    public function __invoke($logger)
    {
        foreach ($logger->getHandlers() as $handler) {
            $handler->setFormatter(new LineFormatter(
                '[%datetime%] %channel%.%level_name%: %message% %context% %extra%'
            ));
        }
    }
}

IT小马
1.2k 声望166 粉丝

Php - Go - Vue - 云原生