Laravel Vapor - Look into logs in different channel in cloudwatch (Sync Code from Shipping Portal)

This commit is contained in:
Dillon Ngo
2024-05-25 02:24:27 +08:00
parent a2276ac977
commit 9d4ebdf9fe
4 changed files with 74 additions and 9 deletions
+51
View File
@@ -0,0 +1,51 @@
<?php
namespace App\Classes\General;
use Illuminate\Support\Facades\Log;
class LogHelper
{
private String $channelName;
public static function channel($channelName): self
{
$logHelper = new self;
$logHelper->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);
}
}
}
@@ -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);
}
}
}
+10 -4
View File
@@ -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);
}
}
}
+1
View File
@@ -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.*",