PHP code example of yesccx / laravel-database-logger

1. Go to this page and download the library: Download yesccx/laravel-database-logger library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yesccx / laravel-database-logger example snippets


use Yesccx\DatabaseLogger\Services\DatabaseLoggerUtils;

DatabaseLoggerUtils::withoutLogger(function() {
    // 该闭包内执行的SQL语句将不会产生日志记录
    \App\Models\User::query()->first();
});

namespace App\Services\DatabaseLogger;

use Yesccx\DatabaseLogger\Contracts\LoggerContract;
use Yesccx\DatabaseLogger\Supports\ResolvingResult;

class MongoLogger implements LoggerContract
{
    public function write(ResolvingResult $resolvingResult)
    {
        // 100ms 格式化的执行耗时
        $formatExecuteTime = $resolvingResult->getFormatExecuteTime();

        // 1000000 执行耗时(纳秒)
        $executeTime = $resolvingResult->getExecuteTime();

        // SELECT .... 执行的SQL语句
        $executeSql = $resolvingResult->getExecuteSql();

        // 编写存储逻辑
    }
}
shell
> php artisan database-logger:install

# Publishing Service Provider... [app/Provider/DatabaseLoggerProvider.php]
# Publishing Configuration... [config/database-logger.php]
# Database Logger installed successfully.
shell
> php artisan database-logger:migration

# Publishing Migration... [database/migrations/2022_12_06_194505_create_database_logs_table.php]
# Migration created successfully!

> php artisan migrate --path=database/migrations/2022_12_06_194505_create_database_logs_table.php