Laravel Log 流程

Laravel 記錄日誌也是用了monolog/log ,只是在上面加了一層可配置和包裹了一層

測試代碼
Log::channel('zip')->info(' begin');

對應日誌配置

'zip' => [
    'driver' => 'daily',
    'path' => storage_path('logs/zip/zip.log'),
    'formatter' => \App\System\Logger\Formatter\IntrospectionFormatter::class,
],

配置可以傳入 驅動,日誌路徑,日誌格式,創建日誌文件的權限,文件鎖等。

最後請求結束,類析構的時候纔會fclose對應的文件流

1. 首先laravel在啓動的時候會有

class LogServiceProvider extends ServiceProvider
{
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('log', function () {
            return new LogManager($this->app);
        });
    }
}

將log註冊進去

然後調用方直接用門面模式就可以LogManager類

Illuminate\Log\LogManager
/**
 * Get a log channel instance.
 *
 * @param  string|null  $channel
 * @return mixed
 */
public function channel($channel = null)
{
    return $this->driver($channel);
}

/**
 * Get a log driver instance.
 *
 * @param  string|null  $driver
 * @return mixed
 */
public function driver($driver = null)
{
    return $this->get($driver ?? $this->getDefaultDriver());
}
/**
 * Attempt to get the log from the local cache.
 *
 * @param  string  $name
 * @return \Psr\Log\LoggerInterface
 */
protected function get($name)
{
    try {
        return $this->channels[$name] ?? with($this->resolve($name), function ($logger) use ($name) {
            return $this->channels[$name] = $this->tap($name, new Logger($logger, $this->app['events']));
        });
    } catch (Throwable $e) {
        return tap($this->createEmergencyLogger(), function ($logger) use ($e) {
            $logger->emergency('Unable to create configured logger. Using emergency logger.', [
                'exception' => $e,
            ]);
        });
    }
}

 

/**
 * Resolve the given log instance by name.
 *
 * @param  string  $name
 * @return \Psr\Log\LoggerInterface
 *
 * @throws \InvalidArgumentException
 */
protected function resolve($name)
{
    $config = $this->configurationFor($name);

    if (is_null($config)) {
        throw new InvalidArgumentException("Log [{$name}] is not defined.");
    }

    if (isset($this->customCreators[$config['driver']])) {
        return $this->callCustomCreator($config);
    }

    $driverMethod = 'create'.ucfirst($config['driver']).'Driver';

    if (method_exists($this, $driverMethod)) {
        return $this->{$driverMethod}($config);
    }

    throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported.");
}

最終還是生成了monolog類

/**
 * Create an instance of the daily file log driver.
 *
 * @param  array  $config
 * @return \Psr\Log\LoggerInterface
 */
protected function createDailyDriver(array $config)
{
    return new Monolog($this->parseChannel($config), [
        $this->prepareHandler(new RotatingFileHandler(
            $config['path'], $config['days'] ?? 7, $this->level($config),
            $config['bubble'] ?? true, $config['permission'] ?? null, $config['locking'] ?? false
        ), $config),
    ]);
}
Illuminate\Log\Logger
/**
 * Log an informational message to the logs.
 *
 * @param  string  $message
 * @param  array  $context
 * @return void
 */
public function info($message, array $context = [])
{
    $this->writeLog(__FUNCTION__, $message, $context);
}

 

/**
 * Write a message to the log.
 *
 * @param  string  $level
 * @param  string  $message
 * @param  array  $context
 * @return void
 */
protected function writeLog($level, $message, $context)
{
    $this->fireLogEvent($level, $message = $this->formatMessage($message), $context);

    $this->logger->{$level}($message, $context);
}

最後還是掉了monolog\log->info

Monolog\Logger
/**
 * Adds a log record at the INFO level.
 *
 * This method allows for compatibility with common interfaces.
 *
 * @param string $message The log message
 * @param array  $context The log context
 */
public function info($message, array $context = []): void
{
    $this->addRecord(static::INFO, (string) $message, $context);
}

具體細節,細品 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章