Skip to content

Commit bb432ab

Browse files
committed
Trigger log reporting (QueryLogWritten event) directly from SqlLogger instead of Writer destructor
1 parent 09c2c9d commit bb432ab

File tree

5 files changed

+16
-7
lines changed

5 files changed

+16
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# CHANGELOG
22

3-
## [v1.1.x (Unreleased)](https://github.com/onlime/laravel-sql-reporter/compare/v1.2.0...main)
3+
## [v1.2.x (Unreleased)](https://github.com/onlime/laravel-sql-reporter/compare/v1.2.0...main)
44

55
- ...
66

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ It reports a lot of metadata like total query count, total execution time, origi
4242
SQL_REPORTER_QUERIES_OVERRIDE_LOG=false
4343
SQL_REPORTER_QUERIES_INCLUDE_PATTERN="#.*#i"
4444
SQL_REPORTER_QUERIES_EXCLUDE_PATTERN="/^\$/"
45+
SQL_REPORTER_QUERIES_REPORT_PATTERN='/^(?!select\s|start transaction|commit|(insert into|update|delete from) `(sessions|jobs|bans|logins)`).*/i'
4546
SQL_REPORTER_QUERIES_MIN_EXEC_TIME=0
4647
SQL_REPORTER_QUERIES_FILE_NAME="[Y-m]-log"
4748
SQL_REPORTER_FORMAT_HEADER_FIELDS="origin,datetime,status,user,env,agent,ip,host,referer"
@@ -100,23 +101,24 @@ This package was inspired by [mnabialek/laravel-sql-logger](https://github.com/m
100101
101102
- Query logging is not triggered upon each query execution but instead at a final step, using `RequestHandled` and `CommandFinished` events.
102103
- 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.
103-
- This package is greatly simplified and only provides support for Laravel 10 / PHP 8.1+
104+
- This package is greatly simplified and only provides support for Laravel 10+ / PHP 8.1+
104105
- 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.
105106
- By default, `onlime/laravel-sql-reporter` produces much nicer log output, especially since we only write header information before the first query.
106107
107108
Sample log output:
108109
109110
```
110111
-- --------------------------------------------------
111-
-- Datetime: 2021-05-28 15:24:46
112+
-- Datetime: 2024-03-11 09:33:22
112113
-- Origin: (request) GET http://localhost:8000/demo
113114
-- Status: Executed 3 queries in 1.85ms
114-
-- User:
115+
116+
-- Guard: web
115117
-- Env: local
116-
-- Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:88.0) Gecko/20100101 Firefox/88.0
118+
-- Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:123.0) Gecko/20100101 Firefox/123.0
117119
-- Ip: 127.0.0.1
118120
-- Host: localhost
119-
-- Referer:
121+
-- Referer:
120122
-- --------------------------------------------------
121123
-- Query 1 [1.45ms]
122124
select * from `users` where `id` = 1 limit 1;

src/SqlLogger.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ public function log(): void
2929
new SqlQuery(++$this->queryNumber, $query['raw_query'], $query['time'])
3030
);
3131
}
32+
$this->writer->report();
3233
}
3334
}

src/Writer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ protected function writeLine(string $line, bool $override = false): int|false
111111
);
112112
}
113113

114-
public function __destruct()
114+
/**
115+
* Report the log by triggering the QueryLogWritten event for further processing.
116+
*/
117+
public function report(): void
115118
{
116119
if (count($this->reportQueries) > 0) {
117120
QueryLogWritten::dispatch(

tests/Unit/SqlLoggerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
$sqlQuery = new SqlQuery(1, 'anything', 1.23);
1919
$this->writer->shouldReceive('writeQuery')->once()->with(Mockery::on(fn ($arg) => $sqlQuery == $arg));
20+
$this->writer->shouldReceive('report')->once()->withNoArgs();
2021

2122
$this->logger->log();
2223
expect(true)->toBeTrue();
@@ -34,6 +35,8 @@
3435
$sqlQuery2 = new SqlQuery(2, 'anything2', 4.56);
3536
$this->writer->shouldReceive('writeQuery')->once()->with(Mockery::on(fn ($arg) => $sqlQuery2 == $arg));
3637

38+
$this->writer->shouldReceive('report')->once()->withNoArgs();
39+
3740
$this->logger->log();
3841
expect(true)->toBeTrue();
3942
});

0 commit comments

Comments
 (0)