PHP code example of stackwatch / laravel

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

    

stackwatch / laravel example snippets


use StackWatch\Laravel\Facades\StackWatch;

try {
    // Your code
} catch (Exception $e) {
    StackWatch::captureException($e);
}

use StackWatch\Laravel\Facades\StackWatch;

// Info message
StackWatch::captureMessage('User signed up', 'info', [
    'plan' => 'pro',
]);

// Warning
StackWatch::captureMessage('Rate limit approaching', 'warning');

// Error
StackWatch::captureMessage('Payment failed', 'error', [
    'user_id' => $user->id,
]);

use StackWatch\Laravel\Facades\StackWatch;

// Backup event
StackWatch::captureEvent('backup', 'info', 'Daily backup completed', [
    'size' => '2.5 GB',
    'duration' => '5 minutes',
]);

// Health check
StackWatch::captureEvent('health', 'warning', 'Disk space low', [
    'disk' => 'primary',
    'usage' => '85%',
]);

use StackWatch\Laravel\Facades\StackWatch;

// Set user context
StackWatch::setUser([
    'id' => $user->id,
    'email' => $user->email,
    'name' => $user->name,
]);

// Add tags
StackWatch::setTag('feature', 'checkout');
StackWatch::setTags([
    'version' => '2.0',
    'region' => 'eu-west',
]);

// Add extra context
StackWatch::setExtra('order_id', $order->id);

// Add custom context
StackWatch::setContext('payment', [
    'provider' => 'stripe',
    'amount' => 9900,
]);

use StackWatch\Laravel\Facades\StackWatch;

StackWatch::addBreadcrumb('user', 'Clicked checkout button');
StackWatch::addBreadcrumb('api', 'Called payment API', [
    'provider' => 'stripe',
    'amount' => 9900,
]);

'channels' => [
    'stack' => [
        'driver' => 'stack',
        'channels' => ['daily', 'stackwatch'],
    ],
    
    'stackwatch' => [
        'driver' => 'custom',
        'via' => \StackWatch\Laravel\Logging\StackWatchLogChannel::class,
        'level' => env('STACKWATCH_LOG_LEVEL', 'debug'),
    ],
],

// app/Http/Kernel.php (Laravel 10)
protected $middleware = [
    // ...
    \StackWatch\Laravel\Middleware\StackWatchMiddleware::class,
];

// Or for specific routes
Route::middleware(['stackwatch'])->group(function () {
    // Routes
});

// Laravel 11+ (bootstrap/app.php)
->withMiddleware(function (Middleware $middleware) {
    $middleware->append(\StackWatch\Laravel\Middleware\StackWatchMiddleware::class);
})

'integrations' => [
    'spatie_activitylog' => [
        'enabled' => true,
        'log_names' => ['default', 'audit'], // Only these log names
        'event_types' => ['created', 'deleted'], // Only these events
    ],
],

'ignored_exceptions' => [
    // Keep defaults
    Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class,
    Illuminate\Database\Eloquent\ModelNotFoundException::class,
    Illuminate\Auth\AuthenticationException::class,
    Illuminate\Auth\Access\AuthorizationException::class,
    Illuminate\Validation\ValidationException::class,
    Illuminate\Session\TokenMismatchException::class,
    
    // Add your own
    App\Exceptions\SomeCustomException::class,
],

'ignored_exceptions' => [],

// config/stackwatch.php
'rate_limit' => [
    'buffer_on_limit' => false,
],

// config/stackwatch.php
'flood_protection' => [
    'enabled' => true,
    'duplicate_window' => 60,      // seconds
    'max_duplicates' => 5,         // same message max 5x per minute
    'circuit_breaker' => [
        'enabled' => true,
        'threshold' => 100,        // 100 logs in 10 seconds trips breaker
        'window' => 10,            // seconds
        'cooldown' => 30,          // block all logs for 30 seconds
    ],
],

use StackWatch\Laravel\FloodProtection;

// Check circuit breaker state
$state = FloodProtection::getCircuitBreakerState();
// ['status' => 'open', 'remaining_cooldown' => 15, ...]

// Get statistics
$stats = FloodProtection::getStats();

// Reset flood protection (useful for testing)
FloodProtection::reset();

// config/stackwatch.php
'breadcrumbs' => [
    'max_breadcrumbs' => 20, // Default: 50
],
bash
php artisan stackwatch:test
bash
php artisan stackwatch:deploy --release=v1.2.3
bash
php artisan stackwatch:deploy --release=$GITHUB_SHA
bash
# Notify StackWatch about a deployment
php artisan stackwatch:deploy --release=v1.2.3
bash
# View buffer status and statistics
php artisan stackwatch:buffer status

# Force flush the buffer (sends aggregated metrics now)
php artisan stackwatch:buffer flush

# Clear the buffer without sending (discards data)
php artisan stackwatch:buffer clear
bash
   php artisan stackwatch:test
   
bash
   # View buffered events count
   php artisan tinker
   >>> app(\StackWatch\Laravel\Transport\HttpTransport::class)->getBufferSize()