Skip to content

Commit fea4d76

Browse files
committed
Fix duplicate logging where both RequestHandled and CommandFinished events got fired during the same request
1 parent 3932b18 commit fea4d76

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

CHANGELOG.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# CHANGELOG
22

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

5-
- Made datetime format of formatted header configurable and added TZ offset to default format `Y-m-d H:i:s P`.
65
- ...
76

7+
## [v1.2.3 (2024-09-12)](https://github.com/onlime/laravel-sql-reporter/compare/v1.2.2...v1.2.3)
8+
9+
- Made datetime format of formatted header configurable and added TZ offset to default format `Y-m-d H:i:s P`.
10+
- Fix | Prevent duplicate logging when a console command is called from a regular web request (e.g. programmatically executing an Artisan command with `Artisan::call()`). In web context, we're now only logging on `RequestHandled` event, while in console only on `CommandFinished` event.
11+
812
## [v1.2.2 (2024-03-21)](https://github.com/onlime/laravel-sql-reporter/compare/v1.2.1...v1.2.2)
913

1014
- The `Writer` object now has a static `shouldReportSqlQuery()` method to define a custom callback for filtering queries included in the `QueryLogWritten` event. by @pascalbaljet in #4

src/Listeners/LogSqlQuery.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
use Illuminate\Console\Events\CommandFinished;
66
use Illuminate\Foundation\Http\Events\RequestHandled;
7+
use Illuminate\Support\Facades\App;
78
use Onlime\LaravelSqlReporter\SqlLogger;
89

9-
class LogSqlQuery
10+
readonly class LogSqlQuery
1011
{
1112
/**
1213
* Create the event listener.
@@ -21,6 +22,12 @@ public function __construct(
2122
*/
2223
public function handle(CommandFinished|RequestHandled $event): void
2324
{
25+
// Prevent duplicate logging when running in request (RequestHandled event)
26+
// and programmatically executing Artisan commands (CommandFinished event).
27+
if ($event instanceof RequestHandled === App::runningInConsole()) {
28+
return;
29+
}
30+
2431
$this->logger->log();
2532
}
2633
}

0 commit comments

Comments
 (0)