Skip to content

sunaoka/laravel-query-logger

Repository files navigation

Query logger for Laravel 5.8 to 11

Latest Stable Version License PHP from Packagist Laravel Test codecov


Support Policy

Version (*1) Laravel (*2) PHP (*3)
1 5.7 - 11 7.1 - 8.3
2 10.15 - 11 8.1 - 8.3

(*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;