From 26cd1359a6b53c924d11a80d9eb9f67e631f5ec6 Mon Sep 17 00:00:00 2001 From: Arpan Rank Date: Fri, 13 Jul 2018 15:37:52 +0530 Subject: [PATCH] Fix: Enabling/Disabling Tracker with Configuration --- src/Console/IndexCommand.php | 4 +-- src/Exceptions/ErrorHandler.php | 5 ++-- src/Middleware/TrackerCookie.php | 4 +-- src/Middleware/TrackerMiddleware.php | 6 ++-- src/Tracker.php | 42 +++++++++++++--------------- src/TrackerServiceProvider.php | 27 +++++++++++------- src/config/tracker.php | 8 ++---- 7 files changed, 48 insertions(+), 48 deletions(-) diff --git a/src/Console/IndexCommand.php b/src/Console/IndexCommand.php index 7553319..7742bc8 100644 --- a/src/Console/IndexCommand.php +++ b/src/Console/IndexCommand.php @@ -52,9 +52,9 @@ public function handle(ElasticSearch $es) $this->info("The {$index}_write alias for the {$index} index was created!"); $types = [ - 'log_queries', + 'logs', 'sql_queries', - 'error_queries' + 'errors' ]; foreach ($types as $type) { diff --git a/src/Exceptions/ErrorHandler.php b/src/Exceptions/ErrorHandler.php index dab5d56..23ad2e3 100644 --- a/src/Exceptions/ErrorHandler.php +++ b/src/Exceptions/ErrorHandler.php @@ -46,14 +46,13 @@ public function report(Exception $exception) * * @param \Illuminate\Http\Request $request * @param \Exception $exception + * * @return \Illuminate\Http\Response */ public function render($request, Exception $exception) { - $exception = $this->prepareException($exception); - $tracker = new Tracker; - $tracker->errorQuery($request, $exception); + $tracker->trackError($request, $this->prepareException($exception)); return $this->previous ? $this->previous->render($request, $exception) : null; } diff --git a/src/Middleware/TrackerCookie.php b/src/Middleware/TrackerCookie.php index cb911cd..1e06604 100644 --- a/src/Middleware/TrackerCookie.php +++ b/src/Middleware/TrackerCookie.php @@ -34,9 +34,9 @@ public function handle($request, Closure $next) { $tracker = new Tracker; - if ($tracker->getTrackerDisabled() || ($tracker->getLogTrackerDisabled() && $tracker->getSqlTrackerDisabled())) { + /*if ($tracker->getTrackerDisabled()) { return $next($request); - } + }*/ $cookie = $tracker->cookieTracker(); diff --git a/src/Middleware/TrackerMiddleware.php b/src/Middleware/TrackerMiddleware.php index 8356e96..fdeff7f 100644 --- a/src/Middleware/TrackerMiddleware.php +++ b/src/Middleware/TrackerMiddleware.php @@ -67,9 +67,9 @@ public function handle($request, Closure $next) { $tracker = new Tracker; - if ($tracker->getTrackerDisabled() || $tracker->getLogTrackerDisabled()) { + /*if ($tracker->getTrackerDisabled('logs')) { return $next($request); - } + }*/ $cookie = $tracker->cookieTracker(); @@ -83,7 +83,7 @@ public function handle($request, Closure $next) $response = $next($request); - $tracker->logQuery($request, $model); + $tracker->trackLog($request, $model); return $response; } diff --git a/src/Tracker.php b/src/Tracker.php index 70a4183..c0dc07d 100644 --- a/src/Tracker.php +++ b/src/Tracker.php @@ -45,23 +45,23 @@ public function __construct() $this->es = new ElasticSearch; } - public function logQuery($request, $model) + public function trackLog($request, $model) { if ($this->excludedTracker()) { return false; } - $model = $this->indexLogQueryDocument($request, $model); - $type = 'log_queries'; + $model = $this->indexLogDocument($request, $model); + $type = 'logs'; if (Config::get('tracker.queue', false)) { - return $this->indexQueueLogQueryDocument($model, $type); + return $this->indexQueueLogDocument($model, $type); } return $this->es->indexDocument($model, $type); } - public function indexQueueLogQueryDocument($model, $type) + public function indexQueueLogDocument($model, $type) { dispatch((new TrackerIndexQueuedModels($model, $type)) ->onQueue($this->syncWithTrackerUsingQueue()) @@ -70,7 +70,7 @@ public function indexQueueLogQueryDocument($model, $type) return true; } - public function indexLogQueryDocument($request, $model) + public function indexLogDocument($request, $model) { $agent = new Agent; $browser = $agent->browser(); @@ -169,23 +169,23 @@ public function indexSqlQueryDocument($sql, $bindings, $time, $connection_name) return $model; } - public function errorQuery($request, $exception) + public function trackError($request, $exception) { if ($this->excludedTracker()) { return false; } - $model = $this->indexErrorQueryDocument($request, $exception); - $type = 'error_queries'; + $model = $this->indexErrorDocument($request, $exception); + $type = 'errors'; if (Config::get('tracker.queue', false)) { - return $this->indexQueueErrorQueryDocument($model, $type); + return $this->indexQueueErrorDocument($model, $type); } return $this->es->indexDocument($model, $type); } - public function indexQueueErrorQueryDocument($model, $type) + public function indexQueueErrorDocument($model, $type) { dispatch((new TrackerIndexQueuedModels($model, $type)) ->onQueue($this->syncWithTrackerUsingQueue()) @@ -194,7 +194,7 @@ public function indexQueueErrorQueryDocument($model, $type) return true; } - public function indexErrorQueryDocument($request, $exception) + public function indexErrorDocument($request, $exception) { $cookie = $this->cookieTracker(); $log = Cache::tags('tracker')->get($cookie); @@ -215,19 +215,15 @@ public function indexErrorQueryDocument($request, $exception) return $model; } - public function getTrackerDisabled(): bool + public function getTrackerDisabled($type = true): bool { - return Config::get('tracker.disabled.all_queries', false); - } - - public function getLogTrackerDisabled(): bool - { - return Config::get('tracker.disabled.log_queries', false); - } + $disabled_type = Config::get('tracker.disabled', false); - public function getSqlTrackerDisabled(): bool - { - return Config::get('tracker.disabled.sql_queries', false); + if (! is_array($disabled_type)) { + return $type === $disabled_type; + } else { + return in_array($type, $disabled_type); + } } public function excludedTracker(): bool diff --git a/src/TrackerServiceProvider.php b/src/TrackerServiceProvider.php index 911e182..557e094 100644 --- a/src/TrackerServiceProvider.php +++ b/src/TrackerServiceProvider.php @@ -38,10 +38,19 @@ public function boot() __DIR__.'/config/tracker.php' => config_path('tracker.php'), ], 'tracker'); - $this->registerMiddlewareToGroup('web', TrackerCookie::class); - $this->registerMiddlewareToGroup('web', TrackerMiddleware::class); + $tracker = new Tracker; + + if (! $tracker->getTrackerDisabled()) { + $this->registerMiddlewareToGroup('web', TrackerCookie::class); + } - $this->registerErrorHandler(); + if (! $tracker->getTrackerDisabled('logs')) { + $this->registerMiddlewareToGroup('web', TrackerMiddleware::class); + } + + if (! $tracker->getTrackerDisabled('errors')) { + $this->registerErrorHandler(); + } } /** @@ -62,17 +71,15 @@ public function register() ]); } - $this->registerSqlQueryLogWatcher(); - } - - protected function registerSqlQueryLogWatcher() - { $tracker = new Tracker; - if ($tracker->getTrackerDisabled() || $tracker->getSqlTrackerDisabled()) { - return false; + if (! $tracker->getTrackerDisabled('sql_queries')) { + $this->registerSqlQueryLogWatcher($tracker); } + } + protected function registerSqlQueryLogWatcher(Tracker $tracker) + { if (class_exists('Illuminate\Database\Events\QueryExecuted')) { $this->app['events']->listen('Illuminate\Database\Events\QueryExecuted', function ($query) use ($tracker) { $tracker->sqlQuery($query->sql, $query->bindings, $query->time, $query->connectionName); diff --git a/src/config/tracker.php b/src/config/tracker.php index a01e6c5..84d8c85 100644 --- a/src/config/tracker.php +++ b/src/config/tracker.php @@ -20,12 +20,10 @@ | | By setting this value to true, the tracker will be disabled completely. | + | Supported: ['logs', 'sql_queries', 'errors'] + | */ - 'disabled' => [ - 'all_queries' => env('TRACKER_DISABLED', false), - 'log_queries' => env('TRACKER_LOG_QUERIES_DISABLED', false), - 'sql_queries' => env('TRACKER_SQL_QUERIES_DISABLED', false) - ], + 'disabled' => env('TRACKER_DISABLED', false), /* |--------------------------------------------------------------------------