Skip to content

Commit c2a7c63

Browse files
committed
added queries.exclude_pattern config option
1 parent a7327fe commit c2a7c63

File tree

7 files changed

+68
-21
lines changed

7 files changed

+68
-21
lines changed

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
# CHANGELOG
22

3-
## [v0.9.1 (Unreleased)](https://github.com/onlime/laravel-sql-reporter/compare/v0.9...main)
3+
## [v0.9.2 (Unreleased)](https://github.com/onlime/laravel-sql-reporter/compare/v0.9.1...main)
44

5+
## [v0.9.1 (2021-06-03)](https://github.com/onlime/laravel-sql-reporter/releases/tag/v0.9.1)
56

6-
## [v0.9 (2021-06-01)](https://github.com/onlime/laravel-sql-reporter/releases/tag/v0.9)
7+
### Added
8+
- Added new config param `queries.exclude_pattern` (env var `SQL_REPORTER_QUERIES_EXCLUDE_PATTERN`) to narrow down queries to be logged without bloating include pattern regex.
9+
10+
### Changed
11+
- Renamed `SQL_REPORTER_QUERIES_PATTERN` env var to `SQL_REPORTER_QUERIES_INCLUDE_PATTERN`
12+
13+
## [v0.9 (2021-06-02)](https://github.com/onlime/laravel-sql-reporter/releases/tag/v0.9)
714

815
- Initial release

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ It reports a lot of metadata like total query count, total execution time, origi
4040
SQL_REPORTER_LOG_EXTENSION=".sql"
4141
SQL_REPORTER_QUERIES_ENABLED=true
4242
SQL_REPORTER_QUERIES_OVERRIDE_LOG=false
43-
SQL_REPORTER_QUERIES_PATTERN="#.*#i"
43+
SQL_REPORTER_QUERIES_INCLUDE_PATTERN="#.*#i"
44+
SQL_REPORTER_QUERIES_EXCLUDE_PATTERN="/^$/"
4445
SQL_REPORTER_QUERIES_MIN_EXEC_TIME=0
4546
SQL_REPORTER_QUERIES_FILE_NAME="[Y-m]-log"
4647
SQL_REPORTER_FORMAT_HEADER_FIELDS="origin,datetime,status,user,env,agent,ip,host,referer"
@@ -49,10 +50,12 @@ It reports a lot of metadata like total query count, total execution time, origi
4950

5051
and adjust values to your needs. You can skip variables for which you want to use default values.
5152

52-
To only log DML / modifying queries like `INSERT`, `UPDATE`, `DELETE`, I recommend to use:
53+
To only log DML / modifying queries like `INSERT`, `UPDATE`, `DELETE`, but not logging any updates on
54+
`last_visit`, I recommend to use:
5355

5456
```ini
55-
SQL_REPORTER_QUERIES_PATTERN="/^(?!SELECT).*$/i"
57+
SQL_REPORTER_QUERIES_INCLUDE_PATTERN="/^(?!SELECT).*$/i"
58+
SQL_REPORTER_QUERIES_EXCLUDE_PATTERN="/^UPDATE.*last_visit/i"
5659
```
5760

5861
If you have also `.env.example` it's recommended to add those entries also in `.env.example` file just to make sure everyone knows about those env variables. Be aware that `SQL_REPORTER_DIRECTORY` is directory inside storage directory.

config/sql-reporter.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,17 @@
8383
* '/^SELECT.*$/i' will log only SELECT queries
8484
* '/^(?!SELECT).*$/i' will log all queries other than SELECT (modifying queries)
8585
*/
86-
'pattern' => env('SQL_REPORTER_QUERIES_PATTERN', '/.*/i'),
86+
'include_pattern' => env('SQL_REPORTER_QUERIES_INCLUDE_PATTERN', '/.*/i'),
87+
88+
/*
89+
* Pattern that should not be matched to log query. This limits the queries that were
90+
* matched by 'include_pattern'. By default no queries are excluded.
91+
*
92+
* examples:
93+
* '/^$/' don't exclude any queries
94+
* '/^UPDATE.*last_visit/i' excludes UPDATE queries that modify `last_visit`
95+
*/
96+
'exclude_pattern' => env('SQL_REPORTER_QUERIES_EXCLUDE_PATTERN', '/^$/'),
8797

8898
/*
8999
* Only log queries with slow execution time (in milliseconds)

src/Config.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,23 @@ public function queriesOverrideLog(): bool
8484
}
8585

8686
/**
87-
* Get pattern for all queries.
87+
* Get include pattern for queries.
8888
*
8989
* @return string
9090
*/
91-
public function queriesPattern(): string
91+
public function queriesIncludePattern(): string
9292
{
93-
return $this->repository->get('sql-reporter.queries.pattern');
93+
return $this->repository->get('sql-reporter.queries.include_pattern');
94+
}
95+
96+
/**
97+
* Get exclude pattern for queries.
98+
*
99+
* @return string
100+
*/
101+
public function queriesExcludePattern(): string
102+
{
103+
return $this->repository->get('sql-reporter.queries.exclude_pattern');
94104
}
95105

96106
/**

src/Writer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ protected function shouldLogQuery(SqlQuery $query)
8282
{
8383
return $this->config->queriesEnabled() &&
8484
$query->time() >= $this->config->queriesMinExecTime() &&
85-
preg_match($this->config->queriesPattern(), $query->raw());
85+
preg_match($this->config->queriesIncludePattern(), $query->raw()) &&
86+
!preg_match($this->config->queriesExcludePattern(), $query->raw());
8687
}
8788

8889
/**

tests/Unit/ConfigTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,21 @@ public function it_returns_valid_values_for_consoleSuffix()
9191
}
9292

9393
/** @test */
94-
public function it_returns_valid_value_for_queriesPattern()
94+
public function it_returns_valid_value_for_queriesIncludePattern()
9595
{
9696
$value = 'sample pattern';
97-
$this->repository->shouldReceive('get')->once()->with('sql-reporter.queries.pattern')
97+
$this->repository->shouldReceive('get')->once()->with('sql-reporter.queries.include_pattern')
9898
->andReturn($value);
99-
$this->assertSame($value, $this->config->queriesPattern());
99+
$this->assertSame($value, $this->config->queriesIncludePattern());
100+
}
101+
102+
/** @test */
103+
public function it_returns_valid_value_for_queriesExcludePattern()
104+
{
105+
$value = 'sample pattern';
106+
$this->repository->shouldReceive('get')->once()->with('sql-reporter.queries.exclude_pattern')
107+
->andReturn($value);
108+
$this->assertSame($value, $this->config->queriesExcludePattern());
100109
}
101110

102111
/** @test */

tests/Unit/WriterTest.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ public function it_creates_log_file()
100100
$this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent);
101101
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn('-- header');
102102
$this->config->shouldReceive('queriesEnabled')->once()->withNoArgs()->andReturn(true);
103-
$this->config->shouldReceive('queriesPattern')->once()->withNoArgs()->andReturn('#.*#i');
103+
$this->config->shouldReceive('queriesIncludePattern')->once()->withNoArgs()->andReturn('#.*#i');
104+
$this->config->shouldReceive('queriesExcludePattern')->once()->withNoArgs()->andReturn('/^$/');
104105
$this->config->shouldReceive('logDirectory')->times(3)->withNoArgs()->andReturn($this->directory);
105106
$this->config->shouldReceive('queriesOverrideLog')->once()->withNoArgs()->andReturn(false);
106107
$this->config->shouldReceive('queriesMinExecTime')->once()->withNoArgs()->andReturn(0);
@@ -127,7 +128,8 @@ public function it_appends_to_existing_log_file()
127128
$this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent);
128129
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn('-- header');
129130
$this->config->shouldReceive('queriesEnabled')->once()->withNoArgs()->andReturn(true);
130-
$this->config->shouldReceive('queriesPattern')->once()->withNoArgs()->andReturn('#.*#i');
131+
$this->config->shouldReceive('queriesIncludePattern')->once()->withNoArgs()->andReturn('#.*#i');
132+
$this->config->shouldReceive('queriesExcludePattern')->once()->withNoArgs()->andReturn('/^$/');
131133
$this->config->shouldReceive('logDirectory')->times(3)->withNoArgs()->andReturn($this->directory);
132134
$this->config->shouldReceive('queriesOverrideLog')->once()->withNoArgs()->andReturn(false);
133135
$this->config->shouldReceive('queriesMinExecTime')->once()->withNoArgs()->andReturn(0);
@@ -154,7 +156,8 @@ public function it_replaces_current_file_content_for_1st_query_when_overriding_i
154156
$this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent);
155157
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn('-- header');
156158
$this->config->shouldReceive('queriesEnabled')->once()->withNoArgs()->andReturn(true);
157-
$this->config->shouldReceive('queriesPattern')->once()->withNoArgs()->andReturn('#.*#i');
159+
$this->config->shouldReceive('queriesIncludePattern')->once()->withNoArgs()->andReturn('#.*#i');
160+
$this->config->shouldReceive('queriesExcludePattern')->once()->withNoArgs()->andReturn('/^$/');
158161
$this->config->shouldReceive('logDirectory')->times(3)->withNoArgs()->andReturn($this->directory);
159162
$this->config->shouldReceive('queriesOverrideLog')->once()->withNoArgs()->andReturn(true);
160163
$this->config->shouldReceive('queriesMinExecTime')->once()->withNoArgs()->andReturn(0);
@@ -182,7 +185,8 @@ public function it_appends_to_current_file_content_for_2nd_query_when_overriding
182185
$this->formatter->shouldReceive('getLine')->twice()->andReturn($lineContent);
183186
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn('-- header');
184187
$this->config->shouldReceive('queriesEnabled')->twice()->withNoArgs()->andReturn(true);
185-
$this->config->shouldReceive('queriesPattern')->twice()->withNoArgs()->andReturn('#.*#i');
188+
$this->config->shouldReceive('queriesIncludePattern')->twice()->withNoArgs()->andReturn('#.*#i');
189+
$this->config->shouldReceive('queriesExcludePattern')->twice()->withNoArgs()->andReturn('/^$/');
186190
$this->config->shouldReceive('logDirectory')->times(4)->withNoArgs()->andReturn($this->directory);
187191
$this->config->shouldReceive('queriesOverrideLog')->once()->withNoArgs()->andReturn(true);
188192
$this->config->shouldReceive('queriesMinExecTime')->twice()->withNoArgs()->andReturn(0);
@@ -206,7 +210,8 @@ public function it_saves_select_query_to_file_when_pattern_set_to_select_queries
206210
$this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent);
207211
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn('');
208212
$this->config->shouldReceive('queriesEnabled')->once()->withNoArgs()->andReturn(true);
209-
$this->config->shouldReceive('queriesPattern')->once()->withNoArgs()->andReturn('#^SELECT .*$#i');
213+
$this->config->shouldReceive('queriesIncludePattern')->once()->withNoArgs()->andReturn('#^SELECT .*$#i');
214+
$this->config->shouldReceive('queriesExcludePattern')->once()->withNoArgs()->andReturn('/^$/');
210215
$this->config->shouldReceive('logDirectory')->times(3)->withNoArgs()->andReturn($this->directory);
211216
$this->config->shouldReceive('queriesMinExecTime')->once()->withNoArgs()->andReturn(0);
212217
$this->filename->shouldReceive('getLogfile')->twice()->withNoArgs()->andReturn($expectedFileName);
@@ -224,7 +229,7 @@ public function it_doesnt_save_select_query_to_file_when_pattern_set_to_insert_o
224229
{
225230
$query = new SqlQuery(1, 'select * FROM test', [], 5.41);
226231
$this->config->shouldReceive('queriesEnabled')->once()->withNoArgs()->andReturn(true);
227-
$this->config->shouldReceive('queriesPattern')->once()->withNoArgs()->andReturn('#^(?:UPDATE|INSERT) .*$#i');
232+
$this->config->shouldReceive('queriesIncludePattern')->once()->withNoArgs()->andReturn('#^(?:UPDATE|INSERT) .*$#i');
228233
$this->config->shouldReceive('queriesMinExecTime')->once()->withNoArgs()->andReturn(0);
229234
$this->config->shouldReceive('logDirectory')->once()->withNoArgs()->andReturn($this->directory);
230235
$this->writer->save($query);
@@ -243,7 +248,8 @@ public function it_saves_insert_query_to_file_when_pattern_set_to_insert_or_upda
243248
$this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent);
244249
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn('');
245250
$this->config->shouldReceive('queriesEnabled')->once()->withNoArgs()->andReturn(true);
246-
$this->config->shouldReceive('queriesPattern')->once()->withNoArgs()->andReturn('#^(?:UPDATE|INSERT) .*$#i');
251+
$this->config->shouldReceive('queriesIncludePattern')->once()->withNoArgs()->andReturn('#^(?:UPDATE|INSERT) .*$#i');
252+
$this->config->shouldReceive('queriesExcludePattern')->once()->withNoArgs()->andReturn('/^$/');
247253
$this->config->shouldReceive('logDirectory')->times(3)->withNoArgs()->andReturn($this->directory);
248254
$this->config->shouldReceive('queriesMinExecTime')->once()->withNoArgs()->andReturn(0);
249255
$this->filename->shouldReceive('getLogfile')->twice()->withNoArgs()->andReturn($expectedFileName);
@@ -267,7 +273,8 @@ public function it_uses_raw_query_without_bindings_when_using_query_pattern()
267273
$this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent);
268274
$this->formatter->shouldReceive('getHeader')->once()->withNoArgs()->andReturn('');
269275
$this->config->shouldReceive('queriesEnabled')->once()->withNoArgs()->andReturn(true);
270-
$this->config->shouldReceive('queriesPattern')->once()->withNoArgs()->andReturn('#^(?:UPDATE test SET x = \? |INSERT ).*$#i');
276+
$this->config->shouldReceive('queriesIncludePattern')->once()->withNoArgs()->andReturn('#^(?:UPDATE test SET x = \? |INSERT ).*$#i');
277+
$this->config->shouldReceive('queriesExcludePattern')->once()->withNoArgs()->andReturn('/^$/');
271278
$this->config->shouldReceive('logDirectory')->times(3)->withNoArgs()->andReturn($this->directory);
272279
$this->filename->shouldReceive('getLogfile')->twice()->withNoArgs()->andReturn($expectedFileName);
273280
$this->config->shouldReceive('queriesMinExecTime')->once()->withNoArgs()->andReturn(0);

0 commit comments

Comments
 (0)