Skip to content

Commit dc3d87c

Browse files
committed
Can log things to DB, even if not logging SQL queries to file #8635
The middleware is unconditionally enabled, so it can notify the DbHandler whenever we get an active connection. However, the middleware may or may not log SQL queries to files. This is controlled via the new config key `logSql`.
1 parent daf37e6 commit dc3d87c

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

phpstan-baseline.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ parameters:
108108
count: 1
109109
path: src/DBAL/Logging/MiddlewareFactory.php
110110

111+
-
112+
message: '#^Parameter \#2 \$logSql of class Ecodev\\Felix\\DBAL\\Logging\\Middleware constructor expects bool, mixed given\.$#'
113+
identifier: argument.type
114+
count: 1
115+
path: src/DBAL/Logging/MiddlewareFactory.php
116+
111117
-
112118
message: '#^Binary operation "\." between ''Invalid \\'''' and mixed results in an error\.$#'
113119
identifier: binaryOp.invalid

src/ConfigProvider.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ public function __invoke(): array
1414
'keys' => [],
1515
'allowedIps' => [],
1616
],
17+
'logSql' => false,
18+
'doctrine' => [
19+
'configuration' => [
20+
'orm_default' => [
21+
// Log all SQL queries from Doctrine (to logs/all.log)
22+
'middlewares' => [DBAL\Logging\Middleware::class],
23+
],
24+
],
25+
],
1726
'dependencies' => [
1827
'invokables' => [
1928
],

src/DBAL/Logging/Driver.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,25 @@
1111

1212
final class Driver extends AbstractDriverMiddleware
1313
{
14-
public function __construct(DriverInterface $driver, private readonly DbHandler $dbHandler)
15-
{
14+
public function __construct(
15+
DriverInterface $driver,
16+
private readonly DbHandler $dbHandler,
17+
private readonly bool $logSql,
18+
) {
1619
parent::__construct($driver);
1720
}
1821

19-
public function connect(#[SensitiveParameter] array $params): Connection
22+
public function connect(#[SensitiveParameter] array $params): DriverInterface\Connection
2023
{
2124
_log()->debug('Connecting to DB', $this->maskPassword($params));
2225

23-
$connection = new Connection(parent::connect($params));
26+
// Don't bother to wrap the connection if we will never log SQL queries to file...
27+
$connection = parent::connect($params);
28+
if ($this->logSql) {
29+
$connection = new Connection($connection);
30+
}
2431

32+
// ... but always notify that we are now connected. so we can log other things to DB
2533
$this->dbHandler->enable();
2634

2735
return $connection;

src/DBAL/Logging/Middleware.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
*/
1414
final class Middleware implements MiddlewareInterface
1515
{
16-
public function __construct(private readonly DbHandler $dbHandler)
17-
{
16+
public function __construct(
17+
private readonly DbHandler $dbHandler,
18+
private readonly bool $logSql,
19+
) {
1820
}
1921

2022
public function wrap(DriverInterface $driver): DriverInterface
2123
{
22-
return new Driver($driver, $this->dbHandler);
24+
return new Driver($driver, $this->dbHandler, $this->logSql);
2325
}
2426
}

src/DBAL/Logging/MiddlewareFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ class MiddlewareFactory implements FactoryInterface
1515
*/
1616
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): Middleware
1717
{
18+
/** @var array $config */
19+
$config = $container->get('config');
20+
$logSql = $config['logSql'];
21+
1822
$dbHandler = $container->get(DbHandler::class);
1923

20-
return new Middleware($dbHandler);
24+
return new Middleware($dbHandler, $logSql);
2125
}
2226
}

0 commit comments

Comments
 (0)