Skip to content

Commit

Permalink
add callSpot
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigopedra committed May 4, 2022
1 parent 0857cdb commit d5057cd
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/QueryLogger/QueryLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Illuminate\Contracts\Config\Repository;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Arr;
use Psr\Log\LoggerInterface;

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

public function handle(QueryExecuted $event)
public function handle(QueryExecuted $event): void
{
$pdo = \method_exists($event->connection, 'getPdo')
? $event->connection->getPdo()
Expand All @@ -45,6 +46,7 @@ public function handle(QueryExecuted $event)
'time' => $event->time,
'connection' => $event->connectionName,
'database' => $this->config->get("database.connections.{$event->connectionName}.database"),
'callSpot' => $this->guessCallSpot(),
]);
}

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

return "'" . \str_replace($search, $replace, $value) . "'";
}

protected function guessCallSpot(): array
{
$stack = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
$vendor = \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR;

foreach ($stack as $trace) {
if (! \str_contains($trace['file'], $vendor)) {
return Arr::only($trace, ['file', 'line', 'function']);
}
}

return ['file' => null, 'line' => null, 'function' => null];
}
}

0 comments on commit d5057cd

Please sign in to comment.