Skip to content

Commit

Permalink
Trigger log reporting (QueryLogWritten event) directly from SqlLogger…
Browse files Browse the repository at this point in the history
… instead of Writer destructor
  • Loading branch information
onlime committed Mar 11, 2024
1 parent 09c2c9d commit bb432ab
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

- ...

Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -100,23 +101,24 @@ 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.
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: [email protected]
-- 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;
Expand Down
1 change: 1 addition & 0 deletions src/SqlLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ public function log(): void
new SqlQuery(++$this->queryNumber, $query['raw_query'], $query['time'])
);
}
$this->writer->report();
}
}
5 changes: 4 additions & 1 deletion src/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions tests/Unit/SqlLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
});

0 comments on commit bb432ab

Please sign in to comment.