-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 增加Command (php artisan log:export)导出日志命令
- Loading branch information
xboard
committed
Dec 31, 2023
1 parent
59c21a9
commit ca1758d
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Carbon\Carbon; | ||
|
||
class ExportV2Log extends Command | ||
{ | ||
protected $signature = 'log:export {days=1 : The number of days to export logs for}'; | ||
protected $description = 'Export v2_log table records of the specified number of days to a file'; | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
public function handle() | ||
{ | ||
$days = $this->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"); | ||
} | ||
} |