Laravel daily日志保留N天源码分析 + 门面源码分析

daily日志保留N天源码分析

loggin.php 配置

'channels' => [
        'stack' => [
            'driver' => 'stack',
            // 管道只采用每日记录
            'channels' => ['daily'],
            'ignore_exceptions' => false,
        ],

        'daily' => [
            'driver' => 'daily',
            'path' => storage_path('logs/laravel.log'),
            'level' => env('LOG_LEVEL', 'debug'),
            // 保留一天
            'days' => 1,
        ]
        ....
]

controller 代码

class Controller extends BaseController
{
    public function test()
    {
        $message = "test";
        Log::emergency($message);
        return "success";
    }
}

堆栈追踪:

  1. Illuminate\Support\Facades\Facade:调用方法 __callStatic,得知Log门面实例类为:Illuminate\Log\LogManager
  2. Illuminate\Log\LogManager:调用方法emergency
  3. Illuminate\Log\LogManager:调用方法Get解析出日志驱动
  4. Illuminate\Log\Logger:调用__call
  5. Monolog\Logger:调用getHandlers
  6. Illuminate\Support\Collection:调用map
  7. 中间省略…
  8. Monolog\Handler\RotatingFileHandler:调用write方法写入日志
protected function write(array $record): void
    {
        // on the first record written, if the log is new, we should rotate (once per day)
        // 翻译过来就是:第一次记录写入,如果日志是新的,我们就应该转换(每天一次) 
        if (null === $this->mustRotate) {
            $this->mustRotate = null === $this->url || !file_exists($this->url);
        }

		// 这里没有用,nextRotation设置的是明天 
        if ($this->nextRotation <= $record['datetime']) {
            $this->mustRotate = true;
            $this->close();
        }

        parent::write($record);
    }
  1. 中间省略…
  2. 最终 RotatingFileHandler 继承自 StreamHandler 继承自 AbstractProcessingHandler 继承自 AbstractHandler 继承自 vendor/monolog/monolog/src/Monolog/Handler/Handler.php
    在 抽象类 Handler 的__destruct析构方法
public function __destruct()
 {
     try {
     	// 调用了RotatingFileHandler的close
         $this->close();   
     } catch (\Throwable $e) {
         // do nothing
     }
 }
  1. Monolog\Handler\RotatingFileHandler调用close
 /**
     * Rotates the files.
     */
    protected function rotate(): void
    {
        // update filename
        $this->url = $this->getTimedFilename();
        $this->nextRotation = new \DateTimeImmutable('tomorrow');

        // skip GC of old logs if files are unlimited
        if (0 === $this->maxFiles) {
            return;
        }

        $logFiles = glob($this->getGlobPattern());
        if (false === $logFiles) {
            // failed to glob
            return;
        }

        if ($this->maxFiles >= count($logFiles)) {
            // no files to remove
            return;
        }

        // Sorting the files by name to remove the older ones
        // 这里通过文件名排序
        usort($logFiles, function ($a, $b) {
            return strcmp($b, $a);
        });

		// 通过array_slice获取符合条件的文件路径信息
        foreach (array_slice($logFiles, $this->maxFiles) as $file) {
            if (is_writable($file)) {
                // suppress errors here as unlink() might fail if two processes
                // are cleaning up/rotating at the same time
                set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline): bool {
                    return false;
                });
                // 这里执行文件的删除
                unlink($file);
                restore_error_handler();
            }
        }

        $this->mustRotate = false;
    }

门面源码分析

先总结:其实就是根据配置的好的别名与实现类的绑定关系,通过魔术方法 __callStatic 进行方法的动态调用

问:那么 Illuminate\Support\Facades\Log 这个东西的意义在哪,直接循环配置文件的数组不就好了?
答:为了phpstorm的代码提示功能


借助这次查询daily日志程序,分析门面是源码的堆栈执行
Illuminate\Support\Facades\Log::emergency($message);

  1. Illuminate\Support\Facades\Log 继承了 Illuminate\Support\Facades\Facade
  2. Illuminate\Support\Facades\Log 调用静态方法 emergency,找不到。于是走到了Facade的魔术方法 __callStatic
  3. __callStatic 做了两件事
    a. 根据 getFacadeRoot 方法解析出门面调用的实例类。
    这里又是如何解析的呢?跟踪代码 __callStatic 调用 getFacadeRoot 方法
    public static function getFacadeRoot()
    {
        // static::getFacadeAccessor()  这里就访问到了 Illuminate\Support\Facades\Log 里面的 getFacadeAccessor()
        // 获取到别名为Log
        return static::resolveFacadeInstance(static::getFacadeAccessor());
    }
    
    获取到别名后,通过 static::resolveFacadeInstance 获取到别名的实例
    系统内的别名与实例类的绑定配置在 Illuminate\Foundation\Application 中
    public function registerCoreContainerAliases()
    {
        foreach ([
            ...省略其他
            'log' => [\Illuminate\Log\LogManager::class, \Psr\Log\LoggerInterface::class],
            ...
        ] as $key => $aliases) {
        	// 循环以log做key,给出一个静态数组
            foreach ($aliases as $alias) {
                $this->alias($key, $alias);
            }
        }
    }
    
    b. 调用方法 emergency
  4. Illuminate\Log\LogManager 调用方法 emergency
public function emergency($message, array $context = [])
  {
      $this->driver()->emergency($message, $context);
  }

$this->deriver() 获取驱动,其实就是解析出配置文件的内容

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

这时候又出现了driver驱动,再次一系列转换
调用创建驱动方法

/**
    * 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),
       ]);
   }

总算是兜兜转转到了 RotatingFileHandler 这个类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
由于股票分析涉及到较多的数据获取和处理,因此常用的一些库包括: 1. pandas:用于数据处理和分析,包括数据读取、数据清洗、数据筛选、数据排序、数据统计等功能。 2. numpy:用于科学计算,包括数组处理、矩阵运算、随机数生成等功能。 3. matplotlib:用于数据可视化,包括折线图、柱状图、散点图、饼图等。 4. seaborn:基于matplotlib的数据可视化库,提供更多的图形样式和更高级的绘图功能。 5. yfinance:用于获取股票数据,包括历史股价、交易量、财务数据等。 下面是一个简单的股票分析代码,用于展示如何使用以上库进行股票分析: ```python import yfinance as yf import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns # 获取股票数据 stock_data = yf.download('AAPL', start='2015-01-01', end='2021-12-31') # 数据清洗和预处理 stock_data = stock_data.dropna() # 删除缺失值 stock_data['daily_return'] = stock_data['Close'].pct_change() # 计算每日收益率 # 统计股票收益率和波动性 mean_return = np.mean(stock_data['daily_return']) std_return = np.std(stock_data['daily_return']) # 绘制股票收益率分布图和收益率时间序列图 fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(10, 8)) sns.histplot(stock_data['daily_return'], ax=axes[0]) axes[0].set_title('Daily Return Distribution') sns.lineplot(x=stock_data.index, y='daily_return', data=stock_data, ax=axes[1]) axes[1].set_title('Daily Return Time Series') plt.show() ``` 以上代码中,首先使用yfinance库获取了AAPL股票在2015年1月1日至2021年12月31日的历史股价数据,然后对数据进行了清洗和预处理,计算了每日收益率,并统计了股票收益率和波动性。最后,使用matplotlib和seaborn库绘制了股票收益率分布图和收益率时间序列图。 当然,这只是一个简单的股票分析示例,实际的股票分析还需要结合更多的数据和指标,并使用更复杂的模型进行分析和预测。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值