From b98b66e15d4f8682187515ea78b48e4d1b136dcf Mon Sep 17 00:00:00 2001 From: Alf Drollinger Date: Mon, 2 Sep 2024 21:53:25 +0200 Subject: [PATCH] Sync and Core Log Levels --- config/core.php | 16 ++++++++ packages/core/config/core.php | 16 ++++++++ packages/core/src/Traits/LogLevel.php | 38 +++++++++++++++++++ packages/press/routes/web.php | 2 - .../Controllers/SyncWebhookController.php | 12 ++---- packages/sync/src/Listener/SyncListener.php | 29 +++++++------- 6 files changed, 87 insertions(+), 26 deletions(-) create mode 100644 packages/core/src/Traits/LogLevel.php diff --git a/config/core.php b/config/core.php index b2a640e95..fb16a80a9 100644 --- a/config/core.php +++ b/config/core.php @@ -463,4 +463,20 @@ 'use_google_icons' => true, + /* + |-------------------------------------------------------------------------- + | Logging + |-------------------------------------------------------------------------- + | + | This config array sets the logging level and whether to log in + | production. It is used by some Moox packages where verbose + | logging is a good thing while implementing complex stuff. + | + */ + + 'logging' => [ + 'verbose_level' => env('VERBOSE_LEVEL', 0), // 0: Off, 1: Debug, 2: Info, 3: All + 'log_in_production' => env('LOG_IN_PRODUCTION', false), + ], + ]; diff --git a/packages/core/config/core.php b/packages/core/config/core.php index 4e63e882a..a2755d0f1 100644 --- a/packages/core/config/core.php +++ b/packages/core/config/core.php @@ -382,4 +382,20 @@ 'use_google_icons' => true, + /* + |-------------------------------------------------------------------------- + | Logging + |-------------------------------------------------------------------------- + | + | This config array sets the logging level and whether to log in + | production. It is used by some Moox packages where verbose + | logging is a good thing while implementing complex stuff. + | + */ + + 'logging' => [ + 'verbose_level' => env('VERBOSE_LEVEL', 0), // 0: Off, 1: Debug, 2: Info, 3: All + 'log_in_production' => env('LOG_IN_PRODUCTION', false), + ], + ]; diff --git a/packages/core/src/Traits/LogLevel.php b/packages/core/src/Traits/LogLevel.php new file mode 100644 index 000000000..082a5cb85 --- /dev/null +++ b/packages/core/src/Traits/LogLevel.php @@ -0,0 +1,38 @@ +log('debug', $message, $context); + } + + protected function logInfo($message, array $context = []) + { + $this->log('info', $message, $context); + } + + protected function log($level, $message, array $context = []) + { + $verboseLevel = Config::get('core.logging.verbose_level', 0); + $logInProduction = Config::get('core.logging.log_in_production', false); + + if ( + ($verboseLevel > 0 && app()->environment() !== 'production') || + ($logInProduction && app()->environment() === 'production') + ) { + if ( + ($level === 'debug' && $verboseLevel >= 1) || + ($level === 'info' && $verboseLevel >= 2) || + $verboseLevel >= 3 + ) { + Log::$level($message, $context); + } + } + } +} diff --git a/packages/press/routes/web.php b/packages/press/routes/web.php index 4dd02f551..7bb67a4ac 100644 --- a/packages/press/routes/web.php +++ b/packages/press/routes/web.php @@ -33,7 +33,6 @@ // TODO: Would be better as middleware? // this must be the last route - /* if (config('press.redirect_to_wp') === true) { Route::any('{any}', function ($any) { if (! str_contains(request()->server()['REQUEST_URI'], config('press.wordpress_slug').'/')) { @@ -41,5 +40,4 @@ } })->where('any', '.*'); } - */ }); diff --git a/packages/sync/src/Http/Controllers/SyncWebhookController.php b/packages/sync/src/Http/Controllers/SyncWebhookController.php index a4f592d01..19fdbc9d9 100644 --- a/packages/sync/src/Http/Controllers/SyncWebhookController.php +++ b/packages/sync/src/Http/Controllers/SyncWebhookController.php @@ -7,9 +7,12 @@ use Illuminate\Support\Facades\Log; use Moox\Sync\Jobs\SyncJob; use Moox\Sync\Models\Sync; +use Moox\Core\Traits\LogLevel; class SyncWebhookController extends Controller { + use LogLevel; + public function __construct() { Log::info('SyncWebhookController instantiated'); @@ -24,7 +27,7 @@ public function handle(Request $request) $sync = Sync::findOrFail($validatedData['sync']['id']); - $this->logdebug('Moox Sync: Webhook recieved for sync', ['sync' => $sync->id]); + $this->verboseLog('Moox Sync: Webhook recieved for sync', ['sync' => $sync->id]); SyncJob::dispatch($sync, $validatedData['model'], $validatedData['event_type']); @@ -40,11 +43,4 @@ protected function validateRequest(Request $request) 'sync.id' => 'required|integer|exists:syncs,id', ]); } - - protected function logDebug($message, array $context = []) - { - if (app()->environment() !== 'production') { - Log::debug($message, $context); - } - } } diff --git a/packages/sync/src/Listener/SyncListener.php b/packages/sync/src/Listener/SyncListener.php index a7fe42f47..8c2391222 100644 --- a/packages/sync/src/Listener/SyncListener.php +++ b/packages/sync/src/Listener/SyncListener.php @@ -7,11 +7,14 @@ use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; +use Moox\Core\Traits\LogLevel; use Moox\Sync\Models\Platform; use Moox\Sync\Models\Sync; class SyncListener { + use LogLevel; + protected $currentPlatformId; public function __construct() @@ -27,9 +30,9 @@ public function __construct() if ($platform) { $this->currentPlatformId = $platform->id; - $this->logdebug('Moox Sync: Platform found for domain: '.$domain); + $this->verboseLog('Moox Sync: Platform found for domain: '.$domain); } else { - $this->logDebug("Platform not found for domain: {$domain}"); + $this->verboseLog("Platform not found for domain: {$domain}"); $this->currentPlatformId = null; } } catch (QueryException $e) { @@ -51,26 +54,27 @@ public function registerListeners() $this->registerModelListeners($sync); } } + $this->verboseLog('SyncListener registered', ['platform_id' => $this->currentPlatformId]); } protected function registerModelListeners(Sync $sync) { $modelClass = $sync->source_model; - $this->logdebug('Moox Sync: Listen to Events for '.$modelClass); + $this->verboseLog('Moox Sync: Listen to Events for '.$modelClass); Event::listen("eloquent.created: {$modelClass}", function ($model) use ($sync) { - $this->logdebug('Moox Sync: Event created for '.$model->id); + $this->verboseLog('Moox Sync: Event created for '.$model->id); $this->handleEvent($model, 'created', $sync); }); Event::listen("eloquent.updated: {$modelClass}", function ($model) use ($sync) { - $this->logdebug('Moox Sync: Event updated for '.$model->title); + $this->verboseLog('Moox Sync: Event updated for '.$model->title); $this->handleEvent($model, 'updated', $sync); }); Event::listen("eloquent.deleted: {$modelClass}", function ($model) use ($sync) { - $this->logdebug('Moox Sync: Event deleted for '.$model->id); + $this->verboseLog('Moox Sync: Event deleted for '.$model->id); $this->handleEvent($model, 'deleted', $sync); }); } @@ -84,7 +88,7 @@ protected function handleEvent($model, $eventType, Sync $sync) 'sync' => $sync->toArray(), ]; - $this->logdebug('Moox Sync: Invoke Webhook for '.$this->currentPlatformId); + $this->verboseLog('Moox Sync: Invoke Webhook for '.$this->currentPlatformId); $this->invokeWebhook($sync, $syncData); } @@ -93,13 +97,13 @@ protected function invokeWebhook(Sync $sync, array $data) { $webhookUrl = 'https://'.$sync->targetPlatform->domain.'/sync-webhook'; - $this->logdebug('Moox Sync: Push to Webhook:', ['url' => $webhookUrl, 'data' => $data]); + $this->verboseLog('Moox Sync: Push to Webhook:', ['url' => $webhookUrl, 'data' => $data]); try { $response = Http::asJson()->post($webhookUrl, $data); if ($response->successful()) { - $this->logdebug('Moox Sync: Webhook invoked successfully.', ['url' => $webhookUrl, 'response' => $response->body()]); + $this->verboseLog('Moox Sync: Webhook invoked successfully.', ['url' => $webhookUrl, 'response' => $response->body()]); } elseif ($response->clientError()) { Log::warning('Client error occurred when invoking webhook.', [ 'url' => $webhookUrl, @@ -133,11 +137,4 @@ protected function invokeWebhook(Sync $sync, array $data) ]); } } - - protected function logDebug($message, array $context = []) - { - if (app()->environment() !== 'production') { - Log::debug($message, $context); - } - } }