diff --git a/app/Classes/General/LogHelper.php b/app/Classes/General/LogHelper.php new file mode 100644 index 00000000..1fcf584a --- /dev/null +++ b/app/Classes/General/LogHelper.php @@ -0,0 +1,26 @@ +info($message); + } + else{ + Log::channel(self::$channelName)->info($message); + } + } +} diff --git a/app/Classes/Modules/Transactions/Processors/CheckStorageInvoiceTransactionProcessor.php b/app/Classes/Modules/Transactions/Processors/CheckStorageInvoiceTransactionProcessor.php index dfe76267..2217ca57 100644 --- a/app/Classes/Modules/Transactions/Processors/CheckStorageInvoiceTransactionProcessor.php +++ b/app/Classes/Modules/Transactions/Processors/CheckStorageInvoiceTransactionProcessor.php @@ -4,6 +4,7 @@ namespace App\Classes\Modules\Transactions\Processors; use App\Classes\Exceptions\MalformedRequestException; +use App\Classes\General\LogHelper; use App\Classes\Modules\Orders\Services\FetchesOrder; use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber; use App\Classes\Modules\Transactions\Services\CreatesTransaction; @@ -104,13 +105,13 @@ class CheckStorageInvoiceTransactionProcessor $results = []; $marking = $order->companyModule->inviters()->withPivot('invitee_reference')->first()->pivot->invitee_reference; $is_credit_term = $order->companyModule->inviters()->withPivot('is_credit_term')->first()->pivot->is_credit_term; - Log::channel('storage_invoices')->info('orderId: '.$order->id.', orderReference: '.$order->reference.', marking: '.$marking.', is_credit_term: '.$is_credit_term); + LogHelper::channel('storage_invoices')->info('orderId: '.$order->id.', orderReference: '.$order->reference.', marking: '.$marking.', is_credit_term: '.$is_credit_term); $packingLists = $order->destinationWarehousePackages; foreach ($packingLists as $packingList){ $arrivalDateAtChinaWarehouse = $this->getArrivalDateAtChinaWarehoue($packingList); - Log::channel('storage_invoices')->info('arrivalDateAtChinaWarehouse: '.json_encode($arrivalDateAtChinaWarehouse)); - Log::channel('storage_invoices')->info('destinationWarehousePackage: '.json_encode($packingList)); + LogHelper::channel('storage_invoices')->info('arrivalDateAtChinaWarehouse: '.json_encode($arrivalDateAtChinaWarehouse)); + LogHelper::channel('storage_invoices')->info('destinationWarehousePackage: '.json_encode($packingList)); $eta = $this->getEtaFromPackingList($packingList); if($arrivalDateAtChinaWarehouse && $eta){ $transactions = $packingList->transactions()->where('transactions.type', TransactionType::SHIPPING_INVOICE)->whereIn('transactions.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])->get(); @@ -138,15 +139,15 @@ class CheckStorageInvoiceTransactionProcessor private function getArrivalDateAtChinaWarehoue($packingList){ if($packingList->type === PackingListType::SHIPPING_PACKING_LIST){ $receive_packing_list = PackingList::where('reference', $packingList->reference)->where('type', PackingListType::WAREHOUSE_RECEIVE_LIST)->first(); - Log::channel('storage_invoices')->info('receive_packing_list: '.json_encode($receive_packing_list->transports)); + LogHelper::channel('storage_invoices')->info('receive_packing_list: '.json_encode($receive_packing_list->transports)); $transport = $receive_packing_list->transports->first(); if ($transport) { $arrivalDateAtChinaWarehouse = Carbon::parse($transport->drop_date); $dateToCompare = Carbon::parse(env('STORAGE_FEE_LAUNCH_DATE', '2023-12-11 00:00:00')); // dd($dateToCompare); if ($arrivalDateAtChinaWarehouse->isAfter($dateToCompare)) { - Log::channel('storage_invoices')->info('transport: '.json_encode($transport)); - Log::channel('storage_invoices')->info('dateToCompare: '.$dateToCompare.', arrivalDateAtChinaWarehouse: '.$arrivalDateAtChinaWarehouse); + LogHelper::channel('storage_invoices')->info('transport: '.json_encode($transport)); + LogHelper::channel('storage_invoices')->info('dateToCompare: '.$dateToCompare.', arrivalDateAtChinaWarehouse: '.$arrivalDateAtChinaWarehouse); return $arrivalDateAtChinaWarehouse; } } @@ -202,9 +203,9 @@ class CheckStorageInvoiceTransactionProcessor $dateToCompare = Carbon::parse(env('SST_START_DATE', '2024-04-01 00:00:00')); $shippingInvoiceTransactionCreatedDate = Carbon::now(); - Log::channel('storage_invoices')->info('storageInvoice: '.json_encode($storageInvoice).', $transaction->status: '.$transaction->status); + LogHelper::channel('storage_invoices')->info('storageInvoice: '.json_encode($storageInvoice).', $transaction->status: '.$transaction->status); if(!$storageInvoice && $resultNumberOfDaysExceeded > 0 && $transaction->status != ApprovalStatus::COMPLETED){ - Log::channel('storage_invoices')->info('Created $transaction->id: '.$transaction->id); + LogHelper::channel('storage_invoices')->info('Created $transaction->id: '.$transaction->id); $billNumber = $this->generatesTransactionBillNumber->execute('STOR-'); if ($shippingInvoiceTransactionCreatedDate->isAfter($dateToCompare)) { @@ -228,7 +229,7 @@ class CheckStorageInvoiceTransactionProcessor $paymentStorageTransaction = $storageInvoice->transactions()->where('transactions.type', TransactionType::PAYMENT)->where('transactions.status', ApprovalStatus::APPROVED)->first(); if($paymentStorageTransaction){ $dateStorageInvoicePaid = $paymentStorageTransaction->created_at->copy()->addDay()->startOfDay(); - Log::channel('storage_invoices')->info('dateStorageInvoicePaid: '.$dateStorageInvoicePaid.', resultCurrentDate: '.$resultCurrentDate); + LogHelper::channel('storage_invoices')->info('dateStorageInvoicePaid: '.$dateStorageInvoicePaid.', resultCurrentDate: '.$resultCurrentDate); $intervalRecalculate = Carbon::parse($dateStorageInvoicePaid)->diff($dt1); $resultNumberOfDaysExceeded = $intervalRecalculate->days - $resultNumberOfDaysFree; $price_cbm = $pricePerCBM * $cbm * $resultNumberOfDaysExceeded; @@ -242,10 +243,10 @@ class CheckStorageInvoiceTransactionProcessor $amount = $storageInvoice->amount; $epsilon = 0.0001; // Tolerance for the comparison - Log::channel('storage_invoices')->info('Update $transaction->id: '.$transaction->id); - Log::channel('storage_invoices')->info('$storageInvoice->status: '.$storageInvoice->status); - Log::channel('storage_invoices')->info('price_cbm: '.$price_cbm."-".gettype($price_cbm)); - Log::channel('storage_invoices')->info('amount: '.$amount); + LogHelper::channel('storage_invoices')->info('Update $transaction->id: '.$transaction->id); + LogHelper::channel('storage_invoices')->info('$storageInvoice->status: '.$storageInvoice->status); + LogHelper::channel('storage_invoices')->info('price_cbm: '.$price_cbm."-".gettype($price_cbm)); + LogHelper::channel('storage_invoices')->info('amount: '.$amount); $proceedToUpdate = false; @@ -264,7 +265,7 @@ class CheckStorageInvoiceTransactionProcessor } if($proceedToUpdate || ($pricePerCBM == 0 && $amount != 0)){ - Log::channel('storage_invoices')->info('proceedToUpdate'); + LogHelper::channel('storage_invoices')->info('proceedToUpdate'); $storageInvoice = $this->updateStorageInvoiceTransaction($storageInvoice, $price_cbm, $total_tax); $invoiceTransactionDetails = $storageInvoice->transactionDetails()->first(); @@ -347,7 +348,7 @@ class CheckStorageInvoiceTransactionProcessor } private function updatePaymentTransactionViaGroupPayment($storageInvoice){ - Log::channel('storage_invoices')->info('updatePaymentTransactionViaGroupPayment'); + LogHelper::channel('storage_invoices')->info('updatePaymentTransactionViaGroupPayment'); $groups = $storageInvoice->groups()->whereNotIn('status', [ApprovalStatus::COMPLETED, ApprovalStatus::SUSPENDED])->get(); foreach ($groups as $grp){ $groupReference = $grp->reference; @@ -359,19 +360,19 @@ class CheckStorageInvoiceTransactionProcessor $this->updatesTransactionStatus->execute($walletTransaction, ApprovalStatus::EXPIRED); if($walletTransaction->payment_reference){ $deletedBillplzBill = $this->deletesBillplzBill->execute($walletTransaction->payment_reference); - Log::channel('storage_invoices')->info('deletedBillplzBill TransactionType::WALLET: '.json_encode($deletedBillplzBill)); + LogHelper::channel('storage_invoices')->info('deletedBillplzBill TransactionType::WALLET: '.json_encode($deletedBillplzBill)); } } } } private function updatePaymentTransactionViaNonGroupPayment($paymentTransactions){ - Log::channel('storage_invoices')->info('updatePaymentTransactionViaNonGroupPayment'); + LogHelper::channel('storage_invoices')->info('updatePaymentTransactionViaNonGroupPayment'); foreach ($paymentTransactions as $paymentTransaction){ $this->updatesTransactionStatus->execute($paymentTransaction, ApprovalStatus::EXPIRED); if($paymentTransaction->payment_reference){ $deletedBillplzBill = $this->deletesBillplzBill->execute($paymentTransaction->payment_reference); - Log::channel('storage_invoices')->info('deletedBillplzBill TransactionType::WALLET: '.json_encode($deletedBillplzBill)); + LogHelper::channel('storage_invoices')->info('deletedBillplzBill TransactionType::WALLET: '.json_encode($deletedBillplzBill)); } } } diff --git a/app/Logging/CloudWatchLoggerFactory.php b/app/Logging/CloudWatchLoggerFactory.php new file mode 100644 index 00000000..863dfa55 --- /dev/null +++ b/app/Logging/CloudWatchLoggerFactory.php @@ -0,0 +1,46 @@ +setFormatter(new JsonFormatter()); + // Create a log channel + $logger = new Logger($name); + // Set handler + $logger->pushHandler($handler); + //$logger->pushProcessor(new CompanyLogProcessor()); //Use this if you want to adjust the JSON output using a log processor + return $logger; + } +} diff --git a/composer.json b/composer.json index b8b2d7d1..8244c4b0 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ "laravel/vapor-cli": "^1.60", "laravel/vapor-core": "^2.33", "maatwebsite/excel": "^3.1", + "maxbanton/cwh": "^2.0", "rinvex/countries": "^6.1", "spatie/laravel-activitylog": "^3.14", "spatie/laravel-permission": "^4.2", diff --git a/config/logging.php b/config/logging.php index 3e34a467..7191f30a 100644 --- a/config/logging.php +++ b/config/logging.php @@ -54,20 +54,27 @@ return [ 'days' => 14, ], - // 'storage_invoices' => [ - // 'driver' => 'single', - // 'path' => storage_path('logs/laravel_storage_invoices.log'), - // 'level' => 'info', - // ], - 'storage_invoices' => [ - 'driver' => 'monolog', - 'level' => env('LOG_LEVEL', 'debug'), - 'handler' => StreamHandler::class, - 'formatter' => env('LOG_STDERR_FORMATTER'), - 'with' => [ - 'stream' => 'php://stderr', + 'driver' => 'single', + 'path' => storage_path('logs/laravel_storage_invoices.log'), + 'level' => 'info', + ], + + 'storage_invoices_vapor' => [ + 'driver' => 'custom', + 'via' => \App\Logging\CloudWatchLoggerFactory::class, + 'formatter' => Monolog\Formatter\JsonFormatter::class, + 'cloudwatch_stream_name' => 'cief-test', + 'sdk' => [ + 'region' => env('AWS_MY_REGION'), + 'version' => 'latest', + 'credentials' => [ + 'key' => env('AWS_CW_ACCESS'), + 'secret' => env('AWS_CW_SECRET') + ] ], + 'retention' => 3, //cief todo: to be updated + 'level' => 'debug', ], 'slack' => [