Laravel Vapor - Look into logs in different channel in cloudwatch

This commit is contained in:
Dillon Ngo
2024-05-26 17:27:15 +08:00
parent db82c8f805
commit 126c73ab47
4 changed files with 119 additions and 12 deletions
@@ -18,6 +18,7 @@ use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Classes\Exceptions\JobResourceNotFoundException;
use App\Classes\General\LogHelper;
abstract class AbstractControllerLogic
{
@@ -69,7 +70,7 @@ abstract class AbstractControllerLogic
} catch (ErrorException|GeneralExceptions $exception){
if ($exception instanceof JobResourceNotFoundException) {
Log::channel('vue_polling')->info(sprintf(
LogHelper::channel('vue_polling')->info(sprintf(
"Uncaught exception '%s' with message '%s' in %s:%d",
get_class($exception),
$exception->getMessage(),
+46
View File
@@ -0,0 +1,46 @@
<?php
namespace App\Logging;
use Aws\CloudWatchLogs\CloudWatchLogsClient;
use Maxbanton\Cwh\Handler\CloudWatch;
use Monolog\Formatter\JsonFormatter;
use Monolog\Logger;
class CloudWatchLoggerFactory
{
/**
* Create a custom Monolog instance.
*
* @param array $config
* @return \Monolog\Logger
*/
public function __invoke(array $config)
{
$sdkParams = $config["sdk"];
$tags = $config["tags"] ?? [ ];
$name = $config["name"] ?? 'cloudwatch';
// Instantiate AWS SDK CloudWatch Logs Client
$client = new CloudWatchLogsClient($sdkParams);
// Log group name, will be created if none
$groupName = $config["groupNamePrefix"] . '-' . env('VAPOR_ENV');
// Log stream name, will be created if none
// $streamName = config('app.hostname');
$streamName = $config["cloudwatch_stream_name"] . '-' . env('VAPOR_ENV');
// Days to keep logs, 14 by default. Set to `null` to allow indefinite retention.
$retentionDays = $config["retention"];
// Instantiate handler (tags are optional)
$handler = new CloudWatch($client, $groupName, $streamName, $retentionDays, 10000, $tags);
$handler->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;
}
}
+63 -4
View File
@@ -99,15 +99,18 @@ return [
'emergency' => [
'path' => storage_path('logs/laravel.log'),
],
'guzzleShippingPortal' => [
'driver' => 'errorlog',
'level' => 'debug',
],
//Custom Log Channels - Starts
'regenerateInvoice' => [
'driver' => 'single',
'path' => storage_path('logs/regenerateInvoice.log'),
'level' => 'info',
],
'guzzleShippingPortal' => [
'driver' => 'errorlog',
'level' => 'debug',
],
'vue_polling' => [
'driver' => 'single',
@@ -120,7 +123,63 @@ return [
'path' => storage_path('logs/laravel_perfex_crm.log'),
'level' => 'info',
],
//Custom Log Channels - Ends
//Custom Log Channels - For Laravel Vapor AWS - Starts
'regenerateInvoice_vapor' => [
'driver' => 'custom',
'via' => \App\Logging\CloudWatchLoggerFactory::class,
'formatter' => Monolog\Formatter\JsonFormatter::class,
'cloudwatch_stream_name' => 'regenerateInvoice_vapor',
'sdk' => [
'region' => env('AWS_MY_REGION'),
'version' => 'latest',
'credentials' => [
'key' => env('AWS_CW_ACCESS'),
'secret' => env('AWS_CW_SECRET')
]
],
'retention' => 31,
'level' => 'debug',
'groupNamePrefix' => env('CLOUDWATCH_LOGGROUP_PREFIX'),
],
'vue_polling_vapor' => [
'driver' => 'custom',
'via' => \App\Logging\CloudWatchLoggerFactory::class,
'formatter' => Monolog\Formatter\JsonFormatter::class,
'cloudwatch_stream_name' => 'vue_polling_vapor',
'sdk' => [
'region' => env('AWS_MY_REGION'),
'version' => 'latest',
'credentials' => [
'key' => env('AWS_CW_ACCESS'),
'secret' => env('AWS_CW_SECRET')
]
],
'retention' => 31,
'level' => 'debug',
'groupNamePrefix' => env('CLOUDWATCH_LOGGROUP_PREFIX'),
],
'perfex_crm_vapor' => [
'driver' => 'custom',
'via' => \App\Logging\CloudWatchLoggerFactory::class,
'formatter' => Monolog\Formatter\JsonFormatter::class,
'cloudwatch_stream_name' => 'perfex_crm_vapor',
'sdk' => [
'region' => env('AWS_MY_REGION'),
'version' => 'latest',
'credentials' => [
'key' => env('AWS_CW_ACCESS'),
'secret' => env('AWS_CW_SECRET')
]
],
'retention' => 31,
'level' => 'debug',
'groupNamePrefix' => env('CLOUDWATCH_LOGGROUP_PREFIX'),
],
//Custom Log Channels - For Laravel Vapor AWS - Ends
],
];
+8 -7
View File
@@ -1,5 +1,6 @@
<?php
use App\Classes\General\LogHelper;
use App\Classes\Modules\Transactions\ControllersLogic\DownloadMockUpWhiteFormPdfLogic;
use App\Classes\Modules\Transactions\Processors\CreateInvoiceTransactionProcessor;
use App\Http\Controllers\Accounting\BankStatementController;
@@ -667,7 +668,7 @@ Route::get('/invoice/fix', function(){
->chunk(100, function ($bookings) use (&$processed_invoice) {
foreach ($bookings as $booking) {
Log::channel('regenerateInvoice')->info('Counter ' . $processed_invoice);
LogHelper::channel('regenerateInvoice')->info('Counter ' . $processed_invoice);
dump('Counter ' . $processed_invoice);
$processed_invoice += 1;
@@ -703,18 +704,18 @@ Route::get('/invoice/fix', function(){
$existing_invoice_bill_no = Transaction::withTrashed()->where('bill_no', $bill_no)->first();
if ($existing_invoice_bill_no) {
Log::channel('regenerateInvoice')->info('Delete existing bill_no');
Log::channel('regenerateInvoice')->info(json_encode($existing_invoice_bill_no));
LogHelper::channel('regenerateInvoice')->info('Delete existing bill_no');
LogHelper::channel('regenerateInvoice')->info(json_encode($existing_invoice_bill_no));
$existing_invoice_bill_no->forceDelete();
}
(App()->make(CreateInvoiceTransactionWithInvoiceNoProcessor::class))->execute($booking, $bill_no);
dump('regenerated invoice. Booking Marking - ' . $booking->marking . '. Bill_no - ' . $bill_no . '. Old bill_no - ' . $deletedInvoice->bill_no);
Log::channel('regenerateInvoice')->info('regenerated invoice. Booking Marking - ' . $booking->marking . '. Bill_no - ' . $bill_no . '. Old bill_no - ' . $deletedInvoice->bill_no);
LogHelper::channel('regenerateInvoice')->info('regenerated invoice. Booking Marking - ' . $booking->marking . '. Bill_no - ' . $bill_no . '. Old bill_no - ' . $deletedInvoice->bill_no);
} else {
(App()->make(CreateInvoiceTransactionProcessor::class))->execute($booking);
dump('regenerated new invoice. Booking Marking - ' . $booking->marking);
Log::channel('regenerateInvoice')->info('regenerated new invoice. Booking Marking - ' . $booking->marking);
LogHelper::channel('regenerateInvoice')->info('regenerated new invoice. Booking Marking - ' . $booking->marking);
}
}
}
@@ -968,7 +969,7 @@ Route::get('/invoice/{marking}/{started_at}/{ended_at}/fix', function($marking,
->chunk(100, function ($bookings) use (&$processed_invoice) {
foreach ($bookings as $booking) {
Log::channel('regenerateInvoice')->info('Counter ' . $processed_invoice);
LogHelper::channel('regenerateInvoice')->info('Counter ' . $processed_invoice);
dump('Counter ' . $processed_invoice);
dump('Marking ' . $booking->marking);
$processed_invoice += 1;
@@ -1006,7 +1007,7 @@ Route::get('/invoice/{marking}/{started_at}/{ended_at}/fix', function($marking,
(App()->make(CreateInvoiceTransactionWithInvoiceNoProcessor::class))->execute($booking, $firstBillNo);
dump('regenerated invoice. Booking Marking - ' . $booking->marking . '. Bill_no - ' . $firstBillNo . '. Old bill_no - ' . $currentInvoice->bill_no);
Log::channel('regenerateInvoice')->info('regenerated invoice. Booking Marking - ' . $booking->marking . '. Bill_no - ' . $firstBillNo . '. Old bill_no - ' . $currentInvoice->bill_no);
LogHelper::channel('regenerateInvoice')->info('regenerated invoice. Booking Marking - ' . $booking->marking . '. Bill_no - ' . $firstBillNo . '. Old bill_no - ' . $currentInvoice->bill_no);
}
}
);