From bb432ab43d44f4252b2bee9a1e5011d178944c1e Mon Sep 17 00:00:00 2001 From: Philip Iezzi Date: Mon, 11 Mar 2024 16:39:32 +0100 Subject: [PATCH] Trigger log reporting (QueryLogWritten event) directly from SqlLogger instead of Writer destructor --- CHANGELOG.md | 2 +- README.md | 12 +++++++----- src/SqlLogger.php | 1 + src/Writer.php | 5 ++++- tests/Unit/SqlLoggerTest.php | 3 +++ 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 29e4b95..149510c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # CHANGELOG -## [v1.1.x (Unreleased)](https://github.com/onlime/laravel-sql-reporter/compare/v1.2.0...main) +## [v1.2.x (Unreleased)](https://github.com/onlime/laravel-sql-reporter/compare/v1.2.0...main) - ... diff --git a/README.md b/README.md index b247db9..7bec40e 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ It reports a lot of metadata like total query count, total execution time, origi SQL_REPORTER_QUERIES_OVERRIDE_LOG=false SQL_REPORTER_QUERIES_INCLUDE_PATTERN="#.*#i" SQL_REPORTER_QUERIES_EXCLUDE_PATTERN="/^\$/" + SQL_REPORTER_QUERIES_REPORT_PATTERN='/^(?!select\s|start transaction|commit|(insert into|update|delete from) `(sessions|jobs|bans|logins)`).*/i' SQL_REPORTER_QUERIES_MIN_EXEC_TIME=0 SQL_REPORTER_QUERIES_FILE_NAME="[Y-m]-log" SQL_REPORTER_FORMAT_HEADER_FIELDS="origin,datetime,status,user,env,agent,ip,host,referer" @@ -100,7 +101,7 @@ This package was inspired by [mnabialek/laravel-sql-logger](https://github.com/m - Query logging is not triggered upon each query execution but instead at a final step, using `RequestHandled` and `CommandFinished` events. - This allows us to include much more information about the whole query executions like total query count, total execution time, and very detailed header information like origin (request URL/console command), authenticated user, app environment, client browser agent / IP / hostname. -- This package is greatly simplified and only provides support for Laravel 10 / PHP 8.1+ +- This package is greatly simplified and only provides support for Laravel 10+ / PHP 8.1+ - It uses the Laravel built-in query logging (`DB::enableQueryLog()`) which logs all queries in memory, which should perform much better than writing every single query to the log file. - By default, `onlime/laravel-sql-reporter` produces much nicer log output, especially since we only write header information before the first query. @@ -108,15 +109,16 @@ Sample log output: ``` -- -------------------------------------------------- --- Datetime: 2021-05-28 15:24:46 +-- Datetime: 2024-03-11 09:33:22 -- Origin: (request) GET http://localhost:8000/demo -- Status: Executed 3 queries in 1.85ms --- User: +-- User: testuser@example.com +-- Guard: web -- Env: local --- Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:88.0) Gecko/20100101 Firefox/88.0 +-- Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:123.0) Gecko/20100101 Firefox/123.0 -- Ip: 127.0.0.1 -- Host: localhost --- Referer: +-- Referer: -- -------------------------------------------------- -- Query 1 [1.45ms] select * from `users` where `id` = 1 limit 1; diff --git a/src/SqlLogger.php b/src/SqlLogger.php index 50866bf..a874c51 100644 --- a/src/SqlLogger.php +++ b/src/SqlLogger.php @@ -29,5 +29,6 @@ public function log(): void new SqlQuery(++$this->queryNumber, $query['raw_query'], $query['time']) ); } + $this->writer->report(); } } diff --git a/src/Writer.php b/src/Writer.php index bbc8eb8..6e97699 100644 --- a/src/Writer.php +++ b/src/Writer.php @@ -111,7 +111,10 @@ protected function writeLine(string $line, bool $override = false): int|false ); } - public function __destruct() + /** + * Report the log by triggering the QueryLogWritten event for further processing. + */ + public function report(): void { if (count($this->reportQueries) > 0) { QueryLogWritten::dispatch( diff --git a/tests/Unit/SqlLoggerTest.php b/tests/Unit/SqlLoggerTest.php index abffc78..e319cd9 100644 --- a/tests/Unit/SqlLoggerTest.php +++ b/tests/Unit/SqlLoggerTest.php @@ -17,6 +17,7 @@ $sqlQuery = new SqlQuery(1, 'anything', 1.23); $this->writer->shouldReceive('writeQuery')->once()->with(Mockery::on(fn ($arg) => $sqlQuery == $arg)); + $this->writer->shouldReceive('report')->once()->withNoArgs(); $this->logger->log(); expect(true)->toBeTrue(); @@ -34,6 +35,8 @@ $sqlQuery2 = new SqlQuery(2, 'anything2', 4.56); $this->writer->shouldReceive('writeQuery')->once()->with(Mockery::on(fn ($arg) => $sqlQuery2 == $arg)); + $this->writer->shouldReceive('report')->once()->withNoArgs(); + $this->logger->log(); expect(true)->toBeTrue(); });