定制 elminson/database-query-logger 二次开发

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

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

elminson/database-query-logger

Composer 安装命令:

composer require elminson/database-query-logger

包简介

A powerful PHP package for logging SQL queries from Laravel applications, supporting both Eloquent and Query Builder instances, with flexible output options.

README 文档

README

Latest Version on Packagist Tests Total Downloads

A powerful PHP package for logging SQL queries from Laravel applications, supporting both Eloquent and Query Builder instances, with flexible output options.

Installation

You can install the package via Composer:

composer require elminson/database-query-logger

Features

  • Log SQL queries from Eloquent or Query Builder instances
  • Log SQL queries to a file or the console
  • Log SQL queries with parameter bindings
  • Support for PDO statements
  • Configurable logging options
  • Timestamp-based log entries
  • Automatic directory creation for log files
  • Support for different parameter types (string, integer, boolean, null)
  • JSON log formatting
  • Log rotation (daily/weekly, max files)

Configuration

Environment Variables

Add these variables to your .env file:

DB_LOGGER_ENABLED=true
DB_LOGGER_CONSOLE_OUTPUT=true
DB_LOGGER_FILE_LOGGING=true
DB_LOGGER_FILE_PATH=storage/logs/queries.log
DB_LOGGER_LOG_FORMAT=text # or json
DB_LOGGER_ROTATION_ENABLED=false
DB_LOGGER_ROTATION_PERIOD=daily # or weekly
DB_LOGGER_ROTATION_MAX_FILES=7

Configuration Options

The logger can be configured using environment variables (as shown above) or by passing an array to its constructor. If you publish the configuration file (config/db-logger.php), you can manage these settings there.

Here are the available options:

  • enabled (boolean): Enable or disable the logger completely. Default: false.
    • Env: DB_LOGGER_ENABLED
  • console_output (boolean): Enable or disable console output. Default: false.
    • Env: DB_LOGGER_CONSOLE_OUTPUT
  • file_logging (boolean): Enable or disable file logging. Default: false.
    • Env: DB_LOGGER_FILE_LOGGING
  • log_file (string): Path to the log file. Default: storage_path('logs/database-queries.log').
    • Env: DB_LOGGER_FILE_PATH
  • log_format (string): Log format. Possible values: text, json. Default: text.
    • Env: DB_LOGGER_LOG_FORMAT
  • log_rotation_enabled (boolean): Enable or disable log rotation. Default: false.
    • Env: DB_LOGGER_ROTATION_ENABLED
  • log_rotation_period (string): Log rotation period. Possible values: daily, weekly. Default: daily.
    • Env: DB_LOGGER_ROTATION_PERIOD
  • log_rotation_max_files (integer): Maximum number of rotated log files to keep. Default: 7.
    • Env: DB_LOGGER_ROTATION_MAX_FILES

Publishing the Configuration File

If you want to customize the logger configuration, you can publish the config file to your Laravel project using the following Artisan command:

php artisan vendor:publish --provider="Elminson\\DbLogger\\DatabaseQueryLoggerServiceProvider" --tag=config

This will copy the configuration file to config/db-logger.php in your Laravel application, where you can adjust the settings as needed.

Service Provider

Register the service provider in config/app.php:

'providers' => [
    // ...
    App\Providers\DatabaseQueryLoggerServiceProvider::class,
],

Usage

Basic Usage

use Elminson\DbLogger\DatabaseQueryLogger;
use Illuminate\Database\Capsule\Manager as DB;

// Initialize the logger
$logger = new DatabaseQueryLogger([
    'enabled' => true,
    'console_output' => true,
    'file_logging' => true,
    'log_file' => storage_path('logs/queries.log'),
    'log_format' => 'text',   // or 'json'
    'log_rotation_enabled' => false, // or true
    'log_rotation_period' => 'daily', // or 'weekly'
    'log_rotation_max_files' => 7
]);

// Log a query
$query = DB::table('users')->where('email', 'example@example.com');
$logger->logQuery($query);

PDO Statement Logging

use PDO;
use PDOStatement;

$pdo = new PDO('sqlite::memory:');
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = :email');
$stmt->bindParam(':email', 'example@example.com');

$logger->logQuery($stmt, ['example@example.com']);

Raw Query Logging

$sql = 'SELECT * FROM users WHERE email = ?';
$bindings = ['example@example.com'];
$logger->logQuery($sql, $bindings, $connection);

Configuration Methods

// Enable/disable logging
$logger->enable(true);

// Enable/disable console output
$logger->enableConsoleOutput(true);

// Set log file path
$logger->setLogFile(storage_path('logs/queries.log'));

Testing

Run the tests with:

composer test

Run tests with coverage:

composer test-coverage

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

Log All Queries in Laravel

To log every SQL query executed by your Laravel application, add the following to your app/Providers/AppServiceProvider.php:

use Illuminate\Support\Facades\DB;
use Elminson\DbLogger\DatabaseQueryLogger;

public function boot()
{
    $logger = new DatabaseQueryLogger(config('db-logger'));

    DB::listen(function ($query) use ($logger) {
        $logger->logQuery(
            $query->sql,
            $query->bindings,
            $query->connection
        );
    });
}

Make sure your config/db-logger.php and .env are set up as described above. This will ensure all queries are logged according to your configuration.

elminson/database-query-logger 适用场景与选型建议

elminson/database-query-logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 55 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 01 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 elminson/database-query-logger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 elminson/database-query-logger 我们能提供哪些服务?
定制开发 / 二次开发

基于 elminson/database-query-logger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-31