PHPackages                             firehed/dbal-logger - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. firehed/dbal-logger

ActiveLibrary[Database &amp; ORM](/categories/database)

firehed/dbal-logger
===================

Reimplementation of the SQLLogger

3.0.0(3mo ago)6324.4k↓52.8%[1 issues](https://github.com/Firehed/doctrine-dbal-logging-middleware/issues)[2 PRs](https://github.com/Firehed/doctrine-dbal-logging-middleware/pulls)2MITPHPPHP ^8.2CI passing

Since Nov 13Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/Firehed/doctrine-dbal-logging-middleware)[ Packagist](https://packagist.org/packages/firehed/dbal-logger)[ RSS](/packages/firehed-dbal-logger/feed)WikiDiscussions main Synced today

READMEChangelog (5)Dependencies (16)Versions (19)Used By (2)

DBAL Logger
===========

[](#dbal-logger)

A customizable query logger for Doctrine DBAL.

This can be used in a number of ways:

- Debug logging
- Telemetry
- Capturing SQL queries for audit trails
- And more! Be creative!

[![Test](https://github.com/Firehed/doctrine-dbal-logging-middleware/actions/workflows/test.yml/badge.svg)](https://github.com/Firehed/doctrine-dbal-logging-middleware/actions/workflows/test.yml)[![Lint](https://github.com/Firehed/doctrine-dbal-logging-middleware/actions/workflows/lint.yml/badge.svg)](https://github.com/Firehed/doctrine-dbal-logging-middleware/actions/workflows/lint.yml)[![Static analysis](https://github.com/Firehed/doctrine-dbal-logging-middleware/actions/workflows/static-analysis.yml/badge.svg)](https://github.com/Firehed/doctrine-dbal-logging-middleware/actions/workflows/static-analysis.yml)[![codecov](https://camo.githubusercontent.com/c70caf1a6c89f4feb4ccdcddb75195da15dd6080a04ca62dcafdf0235413c2db/68747470733a2f2f636f6465636f762e696f2f67682f466972656865642f646f637472696e652d6462616c2d6c6f6767696e672d6d6964646c65776172652f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d72636576546c434b6a33)](https://codecov.io/gh/Firehed/doctrine-dbal-logging-middleware)

Installation
------------

[](#installation)

```
composer require firehed/dbal-logger
```

Usage
-----

[](#usage)

1. Implement `Firehed\DbalLogger\DbalLogger`
2. Wrap it in the middleware
3. Add the middleware to your DBAL configuration

```
use Firehed\DbalLogger\DbalLogger;
use Firehed\DbalLogger\Middleware;

class MyLogger implements DbalLogger
{
    public function startQuery(string $sql, ?array $params = null, ?array $types = null): void
    {
        // Called before each query
        // Commonly: store $sql and start a timer
    }

    public function stopQuery(?Throwable $exception): void
    {
        // Called after each query completes (or fails)
        // Commonly: determine the query duration based on the start timer and log or send a telemetry event
    }

    public function connect(): void
    {
        // Called when a connection is established
    }

    public function disconnect(): void
    {
        // Called when a connection is closed
    }
}

$logger = new MyLogger();
$middleware = new Middleware($logger);

// Add to your Doctrine\DBAL\Configuration
$config = new \Doctrine\DBAL\Configuration();
$config->setMiddlewares([$middleware]);

$connection = \Doctrine\DBAL\DriverManager::getConnection($connectionDetails, $config);
```

Tip

Use dependency injection to provide services to your DbalLogger implementation, such as a log writer (e.g. PSR-3) or telemetry system.

Error Handling
--------------

[](#error-handling)

The `stopQuery()` method receives the exception if a query fails, or `null` on success. This enables query timing, failure tracking, and telemetry:

```
public function stopQuery(?Throwable $exception): void
{
    $duration = hrtime(true) - $this->start;
    if ($exception !== null) {
        $this->logger->error('Query failed', [
            'sql' => $this->sql,
            'duration' => $duration,
            'exception' => $exception,
        ]);
    } else {
        $this->logger->debug('Query completed', [
            'sql' => $this->sql,
            'duration' => $duration,
        ]);
    }
}
```

Multiple Loggers
----------------

[](#multiple-loggers)

Use `ChainLogger` to send events to multiple loggers:

```
use Firehed\DbalLogger\ChainLogger;
use Firehed\DbalLogger\Middleware;

$chain = new ChainLogger([
    new QueryLogger(),
    new MetricsLogger(),
    new AuditTrailLogger(),
]);

$config = new \Doctrine\DBAL\Configuration();
$config->setMiddlewares([new Middleware($chain)]);
```

Migrating from SQLLogger
------------------------

[](#migrating-from-sqllogger)

If you're migrating from the deprecated `Doctrine\DBAL\Logging\SQLLogger`:

- Change `implements SQLLogger` to `implements DbalLogger`
- Add `connect(): void` and `disconnect(): void` methods (can be no-ops)
- The `stopQuery()` method now accepts `?Throwable $exception`
- Wrap your logger in the middleware and configure DBAL:

```
-$config->setSQLLogger($yourSQLLogger);
+$config->setMiddlewares([new Middleware($yourLogger)]);
```

Why this library?
-----------------

[](#why-this-library)

Doctrine's bundled middleware-based replacement for SQLLogger has limitations:

- It's tied directly to a PSR-3 logger
- The log format and levels cannot be customized
- There is no event for queries completing, making telemetry impossible

This library provides full control over logging behavior while using DBAL's middleware system.

Misc
----

[](#misc)

This project follows semantic versioning.

Please use Github for reporting any issues or making any feature requests.

###  Health Score

53

—

FairBetter than 96% of packages

Maintenance76

Regular maintenance activity

Popularity39

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~304 days

Total

5

Last Release

112d ago

Major Versions

1.0.1 → 2.0.02024-02-11

2.1.0 → 3.0.02026-03-13

PHP version history (3 changes)1.0.0PHP ^7.4 || ^8.0

2.0.0PHP ^8.1

3.0.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/354842?v=4)[Eric Stern](/maintainers/Firehed)[@Firehed](https://github.com/Firehed)

---

Top Contributors

[![Firehed](https://avatars.githubusercontent.com/u/354842?v=4)](https://github.com/Firehed "Firehed (38 commits)")

---

Tags

dbaldoctrineloggerormphppsr-3loggingdoctrinedballoggersqllogger

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/firehed-dbal-logger/health.svg)

```
[![Health](https://phpackages.com/badges/firehed-dbal-logger/health.svg)](https://phpackages.com/packages/firehed-dbal-logger)
```

###  Alternatives

[martin-georgiev/postgresql-for-doctrine

Extends Doctrine with native PostgreSQL support for arrays, JSONB, ranges, PostGIS geometries, text search, ltree, uuid, and 100+ PostgreSQL-specific functions.

4585.8M4](/packages/martin-georgiev-postgresql-for-doctrine)[flow-php/doctrine-dbal-bulk

Bulk inserts and updates for Doctrine DBAL

14385.8k4](/packages/flow-php-doctrine-dbal-bulk)[samsonasik/error-hero-module

A Hero for your Laminas and Mezzio application to trap php errors &amp; exceptions

5133.6k1](/packages/samsonasik-error-hero-module)[jawira/db-draw

📐 Takes a DoctrineORM connection and generates a database diagram in .puml format

22113.6k3](/packages/jawira-db-draw)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
