From aca0ed88ba9eb2dc703b713f3acc207a19c0be80 Mon Sep 17 00:00:00 2001
From: guanguans <ityaozm@gmail.com>
Date: Thu, 26 Sep 2024 14:33:36 +0800
Subject: [PATCH] feat: Introduced 'except' configuration to exclude specific
 queries from logging

- Introduced 'except' configuration to exclude specific queries from logging.
- Modified query execution listener to check for excluded queries in addition to the slower_than threshold.
- Improved readability of the code by formatting the conditional statement.
---
 README.md               | 9 +++++++--
 src/ServiceProvider.php | 7 +++++--
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/README.md b/README.md
index 74e1752..2a73003 100644
--- a/README.md
+++ b/README.md
@@ -38,11 +38,16 @@ return [
          
         // Only record queries that are slower than the following time
         // Unit: milliseconds
-        'slower_than' => 0, 
+        'slower_than' => 0,
         
         // Only record queries when the QUERY_LOG_TRIGGER is set in the environment, 
         // or when the trigger HEADER, GET, POST, or COOKIE variable is set.
-        'trigger' => env('QUERY_LOG_TRIGGER'), 
+        'trigger' => env('QUERY_LOG_TRIGGER'),
+
+        // Except record queries
+        'except' => [
+            // '*_telescope_*',
+        ],
         
         // Log Channel
         'channel' => 'stack',
diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php
index 118b393..73afcb1 100644
--- a/src/ServiceProvider.php
+++ b/src/ServiceProvider.php
@@ -33,7 +33,10 @@ public function boot()
         }
 
         $this->app['events']->listen(QueryExecuted::class, function (QueryExecuted $query) {
-            if ($query->time < $this->app['config']->get('logging.query.slower_than', 0)) {
+            if (
+                $query->time < $this->app['config']->get('logging.query.slower_than', 0)
+                || str($query->sql)->is($this->app['config']->get('logging.query.except', []))
+            ) {
                 return;
             }
 
@@ -46,7 +49,7 @@ public function boot()
 
             if (count($bindings) > 0) {
                 $realSql = vsprintf($sqlWithPlaceholders, array_map(
-                    static fn($binding) => $binding === null ? 'NULL' : $pdo->quote($binding),
+                    static fn ($binding) => $binding === null ? 'NULL' : $pdo->quote($binding),
                     $bindings
                 ));
             }