定制 harmonicdigital/doctrine-dbal-logger 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

harmonicdigital/doctrine-dbal-logger

最新稳定版本:v1.0.1

Composer 安装命令:

composer require harmonicdigital/doctrine-dbal-logger

包简介

A customisable and flexible logging middleware alternative for Doctrine DBAL

README 文档

README

A more flexible logging middleware for Doctrine DBAL that provides database operation logging with configurable log levels and messages.

Installation

composer require harmonicdigital/doctrine-dbal-logger

Basic Usage

The middleware can be used as a drop-in replacement for the built-in DBAL logging middleware.

use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use HarmonicDigital\DoctrineDbal\Logging\Middleware;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// Create your PSR-3 logger
$logger = new Logger('doctrine');
$logger->pushHandler(new StreamHandler('doctrine.log', Logger::DEBUG));

// Create the middleware
$middleware = new Middleware($logger);

// Configure DBAL with the middleware
$configuration = new Configuration();
$configuration->setMiddlewares([$middleware]);

// Create your connection
$connection = DriverManager::getConnection([
    'driver' => 'pdo_sqlite',
    'path' => ':memory:',
], $configuration);

Custom Log levels

Sometimes, you may want to log certain database operations at a different levels to the defaults. E.g. you may want connection to be a debug level, but rollback to to be a warning.

Instead of passing a Logger instance to the middleware, you can pass a DoctrineLogger instance, with customised log levels. Additionally, you may want to change the log message for a certain action.

You pass the overriding levels and messages to the DoctrineLogger constructor.

Example

use HarmonicDigital\DoctrineDbal\Logging\DoctrineLogger;
use HarmonicDigital\DoctrineDbal\Logging\DoctrineLoggerInterface;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;


// Create your PSR-3 logger
$logger = new Logger('doctrine');
$logger->pushHandler(new StreamHandler('doctrine.log', Logger::DEBUG));
$middleware = new Middleware(new DoctrineLogger(
    $logger,
    [
        // Overriding log levels
        DoctrineLoggerInterface::LOG_CONNECT => Logger::DEBUG,
        DoctrineLoggerInterface::LOG_ROLL_BACK => Logger::WARNING,
    ],
    [
        // Overriding log messages
        DoctrineLoggerInterface::LOG_ROLL_BACK => 'Rolling back transaction',
    ],
));

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-08