From ca1758dc480e43c36dae878fc2238f0e612d3e52 Mon Sep 17 00:00:00 2001 From: xboard Date: Mon, 1 Jan 2024 04:52:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0Command=20(php=20arti?= =?UTF-8?q?san=20log:export)=E5=AF=BC=E5=87=BA=E6=97=A5=E5=BF=97=E5=91=BD?= =?UTF-8?q?=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/ExportV2Log.php | 52 ++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 app/Console/Commands/ExportV2Log.php diff --git a/app/Console/Commands/ExportV2Log.php b/app/Console/Commands/ExportV2Log.php new file mode 100644 index 0000000..5c1fdfe --- /dev/null +++ b/app/Console/Commands/ExportV2Log.php @@ -0,0 +1,52 @@ +argument('days'); + $date = Carbon::now()->subDays($days)->startOfDay(); + + $logs = \DB::table('v2_log') + ->where('created_at', '>=', $date->timestamp) + ->get(); + + $fileName = "v2_logs_" . Carbon::now()->format('Y_m_d_His') . ".csv"; + $handle = fopen(storage_path("logs/$fileName"), 'w'); + + // 根据您的表结构 + fputcsv($handle, ['Level', 'ID', 'Title', 'Host', 'URI', 'Method', 'Data', 'IP', 'Context', 'Created At', 'Updated At']); + + foreach ($logs as $log) { + fputcsv($handle, [ + $log->level, + $log->id, + $log->title, + $log->host, + $log->uri, + $log->method, + $log->data, + $log->ip, + $log->context, + Carbon::createFromTimestamp($log->created_at)->toDateTimeString(), + Carbon::createFromTimestamp($log->updated_at)->toDateTimeString() + ]); + } + + fclose($handle); + $this->info("Logs exported to $fileName"); + } +}