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(2mo ago)6289.5k—9.5%[2 PRs](https://github.com/Firehed/doctrine-dbal-logging-middleware/pulls)2MITPHPPHP ^8.2CI passing

Since Nov 13Pushed 2mo 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 1mo ago

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

55

—

FairBetter than 98% of packages

Maintenance86

Actively maintained with recent releases

Popularity40

Moderate usage in the ecosystem

Community11

Small or concentrated contributor base

Maturity67

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

66d 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.

4485.3M4](/packages/martin-georgiev-postgresql-for-doctrine)[fresh/doctrine-enum-bundle

Provides support of ENUM type for Doctrine2 in Symfony applications.

4636.8M12](/packages/fresh-doctrine-enum-bundle)[vlucas/spot2

Simple DataMapper built on top of Doctrine DBAL

605392.8k7](/packages/vlucas-spot2)[jsor/doctrine-postgis

Spatial and Geographic Data with PostGIS and Doctrine.

2191.6M1](/packages/jsor-doctrine-postgis)[larapack/doctrine-support

Better Doctrine Support with Laravel (Support for `enum`)

1752.3M55](/packages/larapack-doctrine-support)[friendsofdoctrine/dbal-clickhouse

Doctrine DBAL driver for ClickHouse

1141.2M1](/packages/friendsofdoctrine-dbal-clickhouse)

PHPackages © 2026

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