Skip to content

Commit

Permalink
feat: Introduced 'except' configuration to exclude specific queries f…
Browse files Browse the repository at this point in the history
…rom 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.
  • Loading branch information
guanguans committed Sep 26, 2024
1 parent b9fb545 commit aca0ed8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 5 additions & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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
));
}
Expand Down

0 comments on commit aca0ed8

Please sign in to comment.