| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use Illuminate\Log\Logger;
- use Illuminate\Support\Facades\Log;
- /**
- * 测试文件大小限制的日志驱动
- */
- class TestSizeRotatingLog extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'log:test-size-rotating {--count=100 : 写入日志的数量} {--size=1K : 每条日志的大小} {--max-file-size=10K : 最大文件大小}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试支持文件大小限制的日志驱动功能';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $count = (int) $this->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';
- }
- }
- }
|