#feature: update log format

This commit is contained in:
2026-01-19 15:42:44 +08:00
parent 0646c8612b
commit ddd0f531fd
9 changed files with 180 additions and 170 deletions

View File

@@ -30,12 +30,12 @@ class LogAnalysisCommand extends Command
): int {
// 检查配置
if (!$slsService->isConfigured()) {
Log::error('SLS 服务未配置,请检查 .env 中的 SLS_* 配置项');
Log::channel('log-analysis')->error('SLS 服务未配置,请检查 .env 中的 SLS_* 配置项');
return Command::FAILURE;
}
if (!$aiService->isConfigured()) {
Log::error('AI 服务未配置,请在页面上配置 AI 提供商或设置 .env 中的 AI_* 配置项');
Log::channel('log-analysis')->error('AI 服务未配置,请在页面上配置 AI 提供商或设置 .env 中的 AI_* 配置项');
return Command::FAILURE;
}
@@ -44,7 +44,7 @@ class LogAnalysisCommand extends Command
$to = $this->parseTime($this->option('to') ?? 'now');
if ($from >= $to) {
Log::error('开始时间必须早于结束时间');
Log::channel('log-analysis')->error('开始时间必须早于结束时间');
return Command::FAILURE;
}
@@ -56,11 +56,10 @@ class LogAnalysisCommand extends Command
$query = $this->option('query');
Log::info("开始分析日志...");
Log::info(" 时间范围: {$from->format('Y-m-d H:i:s')} ~ {$to->format('Y-m-d H:i:s')}");
Log::info(" 查询语句: " . ($query ?: '*'));
Log::info(" 分析模式: {$mode->label()}");
$this->newLine();
Log::channel('log-analysis')->info("开始分析日志...");
Log::channel('log-analysis')->info(" 时间范围: {$from->format('Y-m-d H:i:s')} ~ {$to->format('Y-m-d H:i:s')}");
Log::channel('log-analysis')->info(" 查询语句: " . ($query ?: '*'));
Log::channel('log-analysis')->info(" 分析模式: {$mode->label()}");
try {
$result = $analysisService->analyze(
@@ -75,17 +74,17 @@ class LogAnalysisCommand extends Command
if ($outputPath = $this->option('output')) {
$json = json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
file_put_contents($outputPath, $json);
Log::info("报告已保存到: {$outputPath}");
Log::channel('log-analysis')->info("报告已保存到: {$outputPath}");
}
// 推送到钉钉
if ($this->option('push')) {
Log::info("正在推送到钉钉...");
Log::channel('log-analysis')->info("正在推送到钉钉...");
$pushed = $analysisService->pushToNotification($result);
if ($pushed) {
Log::info("已推送到钉钉");
Log::channel('log-analysis')->info("已推送到钉钉");
} else {
Log::warning("钉钉推送失败");
Log::channel('log-analysis')->warning("钉钉推送失败");
}
}
@@ -94,7 +93,7 @@ class LogAnalysisCommand extends Command
return Command::SUCCESS;
} catch (\Exception $e) {
Log::error("分析失败: {$e->getMessage()}");
Log::channel('log-analysis')->error("分析失败: {$e->getMessage()}");
return Command::FAILURE;
}
}
@@ -133,57 +132,45 @@ class LogAnalysisCommand extends Command
*/
private function displaySummary(array $result): void
{
$this->newLine();
$this->info('=== 分析摘要 ===');
$this->line("总日志数: {$result['metadata']['total_logs']}");
$this->line("分析应用数: {$result['metadata']['apps_analyzed']}");
$this->line("执行时间: {$result['metadata']['execution_time_ms']}ms");
$this->newLine();
Log::channel('log-analysis')->info('=== 分析摘要 ===');
Log::channel('log-analysis')->info("总日志数: {$result['metadata']['total_logs']}");
Log::channel('log-analysis')->info("分析应用数: {$result['metadata']['apps_analyzed']}");
Log::channel('log-analysis')->info("执行时间: {$result['metadata']['execution_time_ms']}ms");
if (empty($result['results'])) {
$this->warn('未找到匹配的日志');
Log::channel('log-analysis')->warning('未找到匹配的日志');
return;
}
foreach ($result['results'] as $appName => $appResult) {
$this->line("{$appName}");
Log::channel('log-analysis')->info("{$appName}");
if (isset($appResult['error'])) {
$this->error(" 分析失败: {$appResult['error']}");
Log::channel('log-analysis')->error(" 分析失败: {$appResult['error']}");
continue;
}
$impact = $appResult['impact'] ?? 'unknown';
$impactColor = match ($impact) {
'high' => 'red',
'medium' => 'yellow',
'low' => 'green',
default => 'white',
};
$this->line(" 日志数: {$appResult['log_count']}");
$this->line(" 代码上下文: " . ($appResult['has_code_context'] ? '是' : '否'));
$this->line(" 影响级别: <fg={$impactColor}>{$impact}</>");
$this->line(" 摘要: " . ($appResult['summary'] ?? 'N/A'));
Log::channel('log-analysis')->info(" 日志数: {$appResult['log_count']}");
Log::channel('log-analysis')->info(" 代码上下文: " . ($appResult['has_code_context'] ? '是' : '否'));
Log::channel('log-analysis')->info(" 影响级别: {$impact}");
Log::channel('log-analysis')->info(" 摘要: " . ($appResult['summary'] ?? 'N/A'));
$anomalies = $appResult['core_anomalies'] ?? [];
if (!empty($anomalies)) {
$this->line(" 异常数: " . count($anomalies));
Log::channel('log-analysis')->info(" 异常数: " . count($anomalies));
$table = [];
foreach (array_slice($anomalies, 0, 5) as $anomaly) {
$table[] = [
Log::channel('log-analysis')->info(sprintf(
" - [%s] %s (数量: %d) - %s",
$anomaly['type'] ?? 'N/A',
$anomaly['classification'] ?? 'N/A',
$anomaly['count'] ?? 1,
mb_substr($anomaly['possible_cause'] ?? 'N/A', 0, 40),
];
mb_substr($anomaly['possible_cause'] ?? 'N/A', 0, 40)
));
}
$this->table(['类型', '分类', '数量', '可能原因'], $table);
}
$this->newLine();
}
}
}