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";}
}
堆栈追踪:
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);}
public function __destruct(){try {// 调用了RotatingFileHandler的close$this->close(); } catch (\Throwable $e) {// do nothing}}
/*** 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 unlimitedif (0 === $this->maxFiles) {return;}$logFiles = glob($this->getGlobPattern());if (false === $logFiles) {// failed to globreturn;}if ($this->maxFiles >= count($logFiles)) {// no files to removereturn;}// 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 timeset_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);
public static function getFacadeRoot()
{// static::getFacadeAccessor() 这里就访问到了 Illuminate\Support\Facades\Log 里面的 getFacadeAccessor()// 获取到别名为Logreturn static::resolveFacadeInstance(static::getFacadeAccessor());
}
获取到别名后,通过 static::resolveFacadeInstance 获取到别名的实例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. 调用方法 emergencypublic 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 这个类
下一篇:代码工程化问题