From 9d4ebdf9fe7334515679dc429b0bec9b0196ceac Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Sat, 25 May 2024 02:24:27 +0800 Subject: [PATCH] Laravel Vapor - Look into logs in different channel in cloudwatch (Sync Code from Shipping Portal) --- app/Classes/General/LogHelper.php | 51 +++++++++++++++++++ .../Commands/V2/DeleteOrderV2CommandJob.php | 17 +++++-- app/Console/Commands/DeleteOrderCommand.php | 14 +++-- composer.json | 1 + 4 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 app/Classes/General/LogHelper.php diff --git a/app/Classes/General/LogHelper.php b/app/Classes/General/LogHelper.php new file mode 100644 index 00000000..23d133c8 --- /dev/null +++ b/app/Classes/General/LogHelper.php @@ -0,0 +1,51 @@ +channelName = $channelName; + return $logHelper; + } + + public function info($message) + { + $envVar = env('LARAVEL_VAPOR_ENABLED'); + Log::info("LogHelper.info: {$message}, channelName: {$this->channelName}, envVar {$envVar}"); + if ($envVar) { + Log::channel($this->channelName.'_vapor')->info($message); + } + else{ + Log::channel($this->channelName)->info($message); + } + } + + public function warning($message) + { + $envVar = env('LARAVEL_VAPOR_ENABLED'); + Log::info("LogHelper.warning: {$message}, channelName: {$this->channelName}, envVar {$envVar}"); + if ($envVar) { + Log::channel($this->channelName . '_vapor')->warning($message); + } else { + Log::channel($this->channelName)->warning($message); + } + } + + public function error($message) + { + $envVar = env('LARAVEL_VAPOR_ENABLED'); + Log::info("LogHelper.error: {$message}, channelName: {$this->channelName}, envVar {$envVar}"); + if ($envVar) { + Log::channel($this->channelName . '_vapor')->error($message); + } else { + Log::channel($this->channelName)->error($message); + } + } +} diff --git a/app/Classes/Jobs/Commands/V2/DeleteOrderV2CommandJob.php b/app/Classes/Jobs/Commands/V2/DeleteOrderV2CommandJob.php index 256ed7b8..8b944e05 100644 --- a/app/Classes/Jobs/Commands/V2/DeleteOrderV2CommandJob.php +++ b/app/Classes/Jobs/Commands/V2/DeleteOrderV2CommandJob.php @@ -10,9 +10,11 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\Storage; use App\Classes\ValueObjects\Constants\ApprovalStatus; use App\Models\Booking; -use Illuminate\Support\Facades\Log; + class DeleteOrderV2CommandJob implements ShouldQueue { @@ -72,10 +74,15 @@ class DeleteOrderV2CommandJob implements ShouldQueue $text = implode(', ', $text); } - Log::info(Carbon::now() . ' : ' . $text); + Log::info(Carbon::now() . ' [DeleteOrderV2] : ' . $text); + $filesystemDriver = Storage::getDefaultDriver(); + if($filesystemDriver === 's3'){ - $filePath = storage_path('logs/delete-orders.log'); //cief todo: should map to the equivalent in AWS S3 bucket - $textToAppend = Carbon::now()->format('[Y-m-d H:i:s]') . ' ' . $text . PHP_EOL; - file_put_contents($filePath, $textToAppend, FILE_APPEND); + } + else{ + $filePath = storage_path('logs/delete-orders.log'); + $textToAppend = Carbon::now()->format('[Y-m-d H:i:s]') . ' ' . $text . PHP_EOL; + file_put_contents($filePath, $textToAppend, FILE_APPEND); + } } } diff --git a/app/Console/Commands/DeleteOrderCommand.php b/app/Console/Commands/DeleteOrderCommand.php index 5dced1bb..e2542cee 100644 --- a/app/Console/Commands/DeleteOrderCommand.php +++ b/app/Console/Commands/DeleteOrderCommand.php @@ -6,6 +6,7 @@ use Illuminate\Console\Command; use Carbon\Carbon; use App\Classes\ValueObjects\Constants\ApprovalStatus; use App\Classes\ValueObjects\Constants\TransactionType; +use Illuminate\Support\Facades\Storage; use App\Models\Booking; class DeleteOrderCommand extends Command @@ -77,10 +78,15 @@ class DeleteOrderCommand extends Command $text = implode(', ', $text); } - $this->info(Carbon::now() . ' : ' . $text); + $this->info(Carbon::now() . ' [DeleteOrderV1]: ' . $text); + $filesystemDriver = Storage::getDefaultDriver(); + if($filesystemDriver === 's3'){ - $filePath = storage_path('logs/delete-orders.log'); //cief todo: should map to the equivalent in AWS S3 bucket - $textToAppend = Carbon::now()->format('[Y-m-d H:i:s]') . ' ' . $text . PHP_EOL; - file_put_contents($filePath, $textToAppend, FILE_APPEND); + } + else{ + $filePath = storage_path('logs/delete-orders.log'); + $textToAppend = Carbon::now()->format('[Y-m-d H:i:s]') . ' ' . $text . PHP_EOL; + file_put_contents($filePath, $textToAppend, FILE_APPEND); + } } } diff --git a/composer.json b/composer.json index f9a24d18..757d16f2 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ "laravel/vapor-cli": "^1.55", "laravel/vapor-core": "^2.33", "maatwebsite/excel": "^3.1", + "maxbanton/cwh": "^2.0", "mpdf/mpdf": "^8.1", "rinvex/countries": "^6.1", "rspective/voucherify": " v2.0.*",