Skip to content

Commit d5057cd

Browse files
committed
add callSpot
1 parent 0857cdb commit d5057cd

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/QueryLogger/QueryLogger.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use Illuminate\Contracts\Config\Repository;
1818
use Illuminate\Database\Events\QueryExecuted;
19+
use Illuminate\Support\Arr;
1920
use Psr\Log\LoggerInterface;
2021

2122
class QueryLogger
@@ -29,7 +30,7 @@ public function __construct(LoggerInterface $logger, Repository $config)
2930
$this->config = $config;
3031
}
3132

32-
public function handle(QueryExecuted $event)
33+
public function handle(QueryExecuted $event): void
3334
{
3435
$pdo = \method_exists($event->connection, 'getPdo')
3536
? $event->connection->getPdo()
@@ -45,6 +46,7 @@ public function handle(QueryExecuted $event)
4546
'time' => $event->time,
4647
'connection' => $event->connectionName,
4748
'database' => $this->config->get("database.connections.{$event->connectionName}.database"),
49+
'callSpot' => $this->guessCallSpot(),
4850
]);
4951
}
5052

@@ -106,4 +108,18 @@ protected function quote(?\PDO $pdo, string $value): string
106108

107109
return "'" . \str_replace($search, $replace, $value) . "'";
108110
}
111+
112+
protected function guessCallSpot(): array
113+
{
114+
$stack = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
115+
$vendor = \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR;
116+
117+
foreach ($stack as $trace) {
118+
if (! \str_contains($trace['file'], $vendor)) {
119+
return Arr::only($trace, ['file', 'line', 'function']);
120+
}
121+
}
122+
123+
return ['file' => null, 'line' => null, 'function' => null];
124+
}
109125
}

0 commit comments

Comments
 (0)