elminson/db-logger

This is my package db-logger

0.0.4 2025-01-31 01:56 UTC

This package is auto-updated.

Last update: 2025-01-31 01:57:35 UTC


README

Latest Version on Packagist Tests Total Downloads

A simple PHP package to log SQL queries from Eloquent or Query Builder instances.

Installation

You can install the package via Composer:

composer require elminson/database-query-logger

This is where your description should go. Try and limit it to a paragraph or two. Consider adding a small example.

Features

  • Log SQL queries from Eloquent or Query Builder instances
  • Log SQL queries to a file or the console
  • Log SQL queries with or without bindings
  • Log SQL queries with or without execution time
  • Log SQL queries with or without the backtrace
  • Log SQL queries with or without the caller
  • Log SQL queries with or without the memory usage
  • Log SQL queries with or without the memory peak usage

Usage

Logging a Query

To log a query, you can use the log_query helper function:

use Illuminate\Database\Capsule\Manager as DB;

// Set up the database connection
$db = new DB;
$db->addConnection([
    'driver' => 'sqlite',
    'database' => ':memory:',
]);
$db->setAsGlobal();
$db->bootEloquent();

// Create a test table
DB::schema()->create('users', function ($table) {
    $table->increments('id');
    $table->string('email');
});

// Insert a test record
DB::table('users')->insert([
    'email' => 'example@example.com',
]);

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

// Log the query
log_query($query, true, true);

Testing

composer test

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.