Skip to content

Commit 7f401f9

Browse files
committed
use event connection
1 parent 2bf9a5b commit 7f401f9

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This package tries to replace the bindings with their values so SQL queries beco
1010
## Installation
1111

1212
```
13-
composer require rodrigopedra/laravel-query-logger
13+
composer require rodrigopedra/laravel-query-logger --dev
1414
```
1515

1616
## Configuration

src/QueryLogger/QueryLogger.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,34 @@
1414

1515
namespace RodrigoPedra\QueryLogger;
1616

17-
use Illuminate\Database\ConnectionInterface;
1817
use Illuminate\Database\Events\QueryExecuted;
1918
use Psr\Log\LoggerInterface;
2019

2120
class QueryLogger
2221
{
23-
protected ConnectionInterface $connection;
2422
protected LoggerInterface $logger;
25-
protected $pdo = null;
2623

27-
public function __construct(ConnectionInterface $connection, LoggerInterface $logger)
24+
public function __construct(LoggerInterface $logger)
2825
{
29-
$this->connection = $connection;
3026
$this->logger = $logger;
31-
32-
if (\method_exists($connection, 'getPdo')) {
33-
$this->pdo = $connection->getPdo();
34-
}
3527
}
3628

3729
public function handle(QueryExecuted $event)
3830
{
39-
$bindings = $this->connection->prepareBindings($event->bindings);
40-
$bindings = \array_map([$this, 'prepareValue'], $bindings);
31+
$pdo = \method_exists($event->connection, 'getPdo')
32+
? $event->connection->getPdo()
33+
: null;
34+
35+
$bindings = $event->connection->prepareBindings($event->bindings);
36+
$bindings = \array_map(fn ($value) => $this->prepareValue($pdo, $value), $bindings);
4137

4238
$query = $this->prepareQuery($event->sql, $bindings);
4339

44-
$this->logger->info($query, ['bindings' => $event->bindings, 'time' => $event->time]);
40+
$this->logger->info($query, [
41+
'bindings' => $event->bindings,
42+
'time' => $event->time,
43+
'connection' => $event->connectionName,
44+
]);
4545
}
4646

4747
protected function prepareQuery(string $query, array $bindings): string
@@ -57,7 +57,7 @@ protected function prepareQuery(string $query, array $bindings): string
5757
return $query;
5858
}
5959

60-
protected function prepareValue($value): string
60+
protected function prepareValue($pdo, $value): string
6161
{
6262
if (\is_null($value)) {
6363
return 'NULL';
@@ -72,7 +72,7 @@ protected function prepareValue($value): string
7272
}
7373

7474
if (\is_string($value) && ! \mb_check_encoding($value, 'UTF-8')) {
75-
return $this->quote('[BINARY DATA]');
75+
return $this->quote($pdo, '[BINARY DATA]');
7676
}
7777

7878
if (\is_object($value) && \method_exists($value, '__toString')) {
@@ -88,13 +88,13 @@ protected function prepareValue($value): string
8888
}
8989

9090
// objects not implementing __toString() or toString() will fail here
91-
return $this->quote(\strval($value));
91+
return $this->quote($pdo, \strval($value));
9292
}
9393

94-
protected function quote(string $value): string
94+
protected function quote($pdo, string $value): string
9595
{
96-
if ($this->pdo) {
97-
return $this->pdo->quote($value);
96+
if ($pdo) {
97+
return $pdo->quote($value);
9898
}
9999

100100
$search = ["\\", "\x00", "\n", "\r", "'", '"', "\x1a"];

src/QueryLogger/QueryLoggerServiceProvider.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@
44

55
use Illuminate\Contracts\Config\Repository;
66
use Illuminate\Contracts\Events\Dispatcher;
7-
use Illuminate\Database\Connection;
87
use Illuminate\Database\Events\QueryExecuted;
98
use Illuminate\Support\ServiceProvider;
109

1110
class QueryLoggerServiceProvider extends ServiceProvider
1211
{
13-
public function boot(Repository $config, Dispatcher $events, Connection $connection)
12+
public function boot(Repository $config, Dispatcher $events)
1413
{
1514
if ($config->get('app.debug') === true) {
1615
$events->listen(QueryExecuted::class, QueryLogger::class);
17-
} else {
18-
$connection->disableQueryLog();
1916
}
2017
}
2118
}

0 commit comments

Comments
 (0)