PHP code example of cosmastech / laravel-wide-load

1. Go to this page and download the library: Download cosmastech/laravel-wide-load 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/ */

    

cosmastech / laravel-wide-load example snippets


use Cosmastech\WideLoad\Facades\WideLoad;

// Enable auto-reporting for this lifecycle (request, job, or command)
WideLoad::enableAutoReporting();

// Disable it
WideLoad::enableAutoReporting(false);

// Check if enabled
WideLoad::isAutoReportingEnabled();

use Cosmastech\WideLoad\Facades\WideLoad;

// Add data
WideLoad::add('user_id', $user->id);
WideLoad::add(['plan' => 'pro', 'locale' => 'en']);

// Add only if the key doesn't exist yet
WideLoad::addIf('request_id', Str::uuid());

// Increment a counter
WideLoad::increment('db_queries');
WideLoad::decrement('remaining_credits');

// bootstrap/app.php
use Cosmastech\WideLoad\Http\Middleware\WideLoadMiddleware;

->withMiddleware(function (Middleware $middleware) {
    $middleware->append(WideLoadMiddleware::class);
})

use Cosmastech\WideLoad\Facades\WideLoad;
use Illuminate\Support\Facades\Log;

public function boot(): void
{
    WideLoad::reportUsing(static function (array $data): void {
        if (empty($data)) {
            return;
        }

        Log::info("[Shutdown] Request details", $data);
    });
}

use Cosmastech\WideLoad\Events\WideLoadReporting;
use Cosmastech\WideLoad\Events\NoWideLoadToReport;
use Illuminate\Support\Facades\Event;

Event::listen(WideLoadReporting::class, function (WideLoadReporting $event) {
    // $event->data contains the reported key-value pairs
});

Event::listen(NoWideLoadToReport::class, function () {
    // No wide load data was collected during this lifecycle
});
bash
php artisan vendor:publish --tag=wide-load-config