option('count'); $sizeStr = $this->option('size'); $maxFileSizeStr = $this->option('max-file-size'); $size = $this->parseSize($sizeStr); $maxFileSize = $this->parseSize($maxFileSizeStr); $this->info("开始测试日志驱动..."); $this->info("将写入 {$count} 条日志,每条大小约 {$sizeStr}"); $this->info("最大文件大小: {$maxFileSizeStr}"); /** * 获取测试日志实例 * @var Logger $testLogger */ $testLogger= Log::channel('size_rotating_daily'); // 生成指定大小的日志内容 $content = str_repeat('A', $size - 100); // 减去100字节用于时间戳等信息 $progressBar = $this->output->createProgressBar($count); $progressBar->start(); for ($i = 1; $i <= $count; $i++) { $message = "测试日志 #{$i} - " . $content; // 随机使用不同的日志级别 $level = $this->getRandomLogLevel(); // 使用测试日志实例 $testLogger->$level($message); $progressBar->advance(); // 每10条日志暂停一下,避免过快写入 if ($i % 10 === 0) { usleep(1000); // 1ms } } $progressBar->finish(); $this->newLine(); // 显示生成的日志文件 $this->showTestLogFiles(); $this->info("测试完成!"); return 0; } /** * 解析大小字符串 * * @param string $sizeStr * @return int */ private function parseSize(string $sizeStr): int { $sizeStr = trim($sizeStr); $unit = strtoupper(substr($sizeStr, -1)); $value = (int) substr($sizeStr, 0, -1); return match ($unit) { 'K' => $value * 1024, 'M' => $value * 1024 * 1024, 'G' => $value * 1024 * 1024 * 1024, default => (int) $sizeStr, }; } /** * 获取随机日志级别 * * @return string */ private function getRandomLogLevel(): string { $levels = ['debug', 'info', 'notice', 'warning', 'error']; return $levels[array_rand($levels)]; } /** * 显示生成的测试日志文件 * * @return void */ private function showTestLogFiles(): void { $logPath = storage_path('logs'); $pattern = $logPath . '/laravel-' . date('Y-m-d') . '*.log'; $files = glob($pattern); if (empty($files)) { $this->warn("没有找到今天的测试日志文件"); return; } $this->newLine(); $this->info("生成的测试日志文件:"); $tableData = []; foreach ($files as $file) { $filename = basename($file); $size = filesize($file); $sizeFormatted = $this->formatFileSize($size); $tableData[] = [$filename, $sizeFormatted, $size]; } // 按文件名排序 usort($tableData, function ($a, $b) { return strcmp($a[0], $b[0]); }); $this->table(['文件名', '大小', '字节数'], $tableData); } /** * 格式化文件大小 * * @param int $bytes * @return string */ private function formatFileSize(int $bytes): string { if ($bytes >= 1024 * 1024 * 1024) { return round($bytes / (1024 * 1024 * 1024), 2) . ' GB'; } elseif ($bytes >= 1024 * 1024) { return round($bytes / (1024 * 1024), 2) . ' MB'; } elseif ($bytes >= 1024) { return round($bytes / 1024, 2) . ' KB'; } else { return $bytes . ' B'; } } }