sunaoka/laravel-query-logger

Query logger for Laravel.

v2.0.1 2024-09-04 09:20 UTC

README

Latest Stable Version License PHP from Packagist Laravel Test codecov

Support Policy

(*1) Supported Query logger version

(*2) Supported Laravel versions

(*3) Supported PHP versions

Installation

composer require --dev sunaoka/laravel-query-logger

Configurations

php artisan vendor:publish --tag=query-logger-config
<?php

declare(strict_types=1);

return [

    /*
    |--------------------------------------------------------------------------
    | Output Log Color
    |--------------------------------------------------------------------------
    |
    | Sets the foreground and background colors of the log output.
    |
    | Supported: "black", "red", "green", "yellow", "blue", "magenta", "cyan",
    |            "white", "default", "gray", "bright-red", "bright-green",
    |            "bright-yellow", "bright-blue", "bright-magenta",
    |            "bright-cyan", "bright-white"
    */

    'color' => [
        'foreground' => env('QUERY_LOGGER_COLOR_FOREGROUND', ''),
        'background' => env('QUERY_LOGGER_COLOR_BACKGROUND', ''),
    ],

    /*
    |--------------------------------------------------------------------------
    | Slow Query Log
    |--------------------------------------------------------------------------
    |
    | Sets the number of milliseconds to output the slow query.
    | If less than 0 is specified, all logs are output.
    */

    'slow_query' => [
        'milliseconds' => (int) env('QUERY_LOGGER_SLOW_QUERY_MILLISECONDS', 0),
    ],
];

Usage

<?php

\DB::beginTransaction();
\App\User::whereEmail('example@example.com')->update(['name' => 'example']);
\DB::commit();

\DB::beginTransaction();
\App\User::whereEmail('example@example.com')->update(['name' => 'example']);
\DB::rollBack();
tail -F storage/logs/laravel.log
[2020-09-11 01:08:37] local.DEBUG: BEGIN;  
[2020-09-11 01:08:37] local.DEBUG: [0.31ms] update "users" set "name" = 'example' where "email" = 'example@example.com';  
[2020-09-11 01:08:37] local.DEBUG: COMMIT;  

[2020-09-11 01:08:37] local.DEBUG: BEGIN;  
[2020-09-11 01:08:37] local.DEBUG: [0.12ms] update "users" set "name" = 'example' where "email" = 'example@example.com';  
[2020-09-11 01:08:37] local.DEBUG: ROLLBACK;