firehed/dbal-logger
Composer 安装命令:
composer require firehed/dbal-logger
包简介
Reimplementation of the SQLLogger
README 文档
README
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!
Installation
composer require firehed/dbal-logger
Usage
- Implement
Firehed\DbalLogger\DbalLogger - Wrap it in the middleware
- 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
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
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
If you're migrating from the deprecated Doctrine\DBAL\Logging\SQLLogger:
- Change
implements SQLLoggertoimplements DbalLogger - Add
connect(): voidanddisconnect(): voidmethods (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?
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
This project follows semantic versioning.
Please use Github for reporting any issues or making any feature requests.
firehed/dbal-logger 适用场景与选型建议
firehed/dbal-logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 316.93k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2023 年 03 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「logging」 「dbal」 「logger」 「doctrine」 「sqllogger」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 firehed/dbal-logger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 firehed/dbal-logger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 firehed/dbal-logger 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
A Zend Framework module that sets up Monolog for logging in applications.
A custom Doctine DBAL type to use PHP DateTime objects set to the system's default timezone.
Workflow logger
HTTP request logger middleware for Laravel
Asynchronous Sentry for Symfony - Fire and forget
统计信息
- 总下载量: 316.93k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 13
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-03-09