Skip to content

Commit

Permalink
Sync and Core Log Levels
Browse files Browse the repository at this point in the history
  • Loading branch information
adrolli committed Sep 2, 2024
1 parent 27a8acc commit b98b66e
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 26 deletions.
16 changes: 16 additions & 0 deletions config/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
],

];
16 changes: 16 additions & 0 deletions packages/core/config/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
],

];
38 changes: 38 additions & 0 deletions packages/core/src/Traits/LogLevel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Moox\Core\Traits;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Config;

trait LogLevel
{
protected function verboseLog($message, array $context = [])
{
$this->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);
}
}
}
}
2 changes: 0 additions & 2 deletions packages/press/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@

// 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').'/')) {
return redirect('/wp/'.ltrim(request()->path(), '/'));
}
})->where('any', '.*');
}
*/
});
12 changes: 4 additions & 8 deletions packages/sync/src/Http/Controllers/SyncWebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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']);

Expand All @@ -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);
}
}
}
29 changes: 13 additions & 16 deletions packages/sync/src/Listener/SyncListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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) {
Expand All @@ -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);
});
}
Expand All @@ -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);
}
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit b98b66e

Please sign in to comment.