PHP code example of marceli-to / wiretap
1. Go to this page and download the library: Download marceli-to/wiretap 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/ */
marceli-to / wiretap example snippets
'webhook' => [
'headers' => [
'X-API-Key' => env('WIRETAP_API_KEY'),
'X-Custom-Header' => 'custom-value',
],
],
use MarceliTo\Wiretap\Facades\Wiretap;
// Log levels
Wiretap::info('User logged in', ['user_id' => 123]);
Wiretap::error('Database connection failed', ['error' => $exception->getMessage()]);
Wiretap::warning('Low disk space detected', ['available' => '2GB']);
Wiretap::debug('Cache miss', ['key' => 'user:123']);
// Custom log level
Wiretap::log('custom', 'Custom event occurred', ['data' => $someData]);
class UserController extends Controller
{
public function store(Request $request)
{
try {
$user = User::create($request->validated());
Wiretap::info('New user registered', [
'user_id' => $user->id,
'email' => $user->email,
'ip' => $request->ip(),
'user_agent' => $request->userAgent()
]);
return response()->json($user, 201);
} catch (\Exception $e) {
Wiretap::error('User registration failed', [
'error' => $e->getMessage(),
'input' => $request->except(['password']),
'trace' => $e->getTraceAsString()
]);
throw $e;
}
}
}
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use MarceliTo\Wiretap\Facades\Wiretap;
use Throwable;
class Handler extends ExceptionHandler
{
// ... existing code ...
public function report(Throwable $exception)
{
// Smart exception filtering - automatically categorizes by severity
Wiretap::exception($exception, [
'url' => request()->fullUrl(),
'method' => request()->method(),
'user_id' => auth()->id(),
'ip' => request()->ip()
]);
parent::report($exception);
}
}
public function report(Throwable $exception)
{
Wiretap::error('Exception occurred', [
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'url' => request()->fullUrl(),
'method' => request()->method(),
'user_id' => auth()->id(),
'ip' => request()->ip()
]);
parent::report($exception);
}
// Only log as error if it's a critical condition
Wiretap::errorIf($user->isAdmin(), 'Admin action failed', [
'action' => $action,
'user_id' => $user->id
]);
// Traditional approach (more verbose)
if ($user->isAdmin()) {
Wiretap::error('Admin action failed', [
'action' => $action,
'user_id' => $user->id
]);
}
class ProcessOrderJob implements ShouldQueue
{
public function handle()
{
try {
Wiretap::info('Processing order job started', [
'order_id' => $this->order->id,
'queue' => $this->queue
]);
// Process order...
Wiretap::info('Order processed successfully', [
'order_id' => $this->order->id,
'processing_time' => $processingTime
]);
} catch (\Exception $e) {
Wiretap::error('Order processing failed', [
'order_id' => $this->order->id,
'error' => $e->getMessage()
]);
throw $e;
}
}
}
// User activity tracking
Wiretap::info('User action', [
'action' => 'profile_updated',
'user_id' => auth()->id(),
'changes' => $user->getChanges(),
'timestamp' => now()->toISOString()
]);
// Performance monitoring
Wiretap::info('Database query performance', [
'query' => 'SELECT * FROM users WHERE active = 1',
'execution_time' => $executionTime,
'rows_affected' => $rowCount
]);
// Security events
Wiretap::warning('Suspicious login attempt', [
'ip' => $request->ip(),
'user_agent' => $request->userAgent(),
'failed_attempts' => $failedAttempts,
'location' => $geoLocation
]);
// Direct class usage
use MarceliTo\Wiretap\Wiretap;
$wiretap = new Wiretap();
$wiretap->info('Direct usage example', ['test' => true]);
// Or using Laravel's service container
app('wiretap')->info('Service container usage', ['test' => true]);
\Wiretap::info('Testing Wiretap integration', [
'test' => true,
'timestamp' => now()->toISOString()
]);
return [
// Master switch to enable/disable all Wiretap functionality
'enabled' => env('WIRETAP_ENABLED', true),
// Enable Laravel logging integration
'log_to_laravel' => env('WIRETAP_LARAVEL_ENABLED', true),
'webhook' => [
// Enable/disable webhook functionality
'enabled' => env('WIRETAP_WEBHOOK_ENABLED', false),
// Webhook endpoint URL
'url' => env('WIRETAP_WEBHOOK_URL'),
// Bearer token secret for webhook authentication
'secret' => env('WIRETAP_WEBHOOK_SECRET'),
// Additional custom headers (Authorization header is automatically
// added if webhook secret is configured above)
'headers' => [
// 'X-API-Key' => 'additional-key',
],
// Log webhook failures
'log_failures' => env('WIRETAP_WEBHOOK_LOG_FAILURES', true),
],
// HTTP timeout for webhook requests
'timeout' => env('WIRETAP_TIMEOUT', 5),
// Exception level mapping for smart filtering
'exception_levels' => [
// Laravel Framework Exceptions
'Illuminate\Validation\ValidationException' => 'info',
'Illuminate\Auth\AuthenticationException' => 'warning',
'Illuminate\Auth\Access\AuthorizationException' => 'warning',
'Illuminate\Database\Eloquent\ModelNotFoundException' => 'info',
// Symfony HTTP Exceptions
'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' => 'info',
'Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException' => 'warning',
'Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException' => 'info',
'Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException' => 'info',
'Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException' => 'warning',
'Symfony\Component\HttpKernel\Exception\BadRequestHttpException' => 'warning',
// General HTTP Exception with closure
'Symfony\Component\HttpKernel\Exception\HttpException' => function ($exception) {
$statusCode = method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : 500;
return $statusCode >= 500 ? 'error' : 'info';
},
// Default level for unmapped exceptions
'default' => 'error',
],
];
'exception_levels' => [
// Skip logging certain exceptions entirely
'App\Exceptions\IgnorableException' => 'skip',
// Custom application exceptions
'App\Exceptions\BusinessLogicException' => 'warning',
'App\Exceptions\IntegrationException' => 'error',
// Use closures for dynamic level determination
'App\Exceptions\ConditionalException' => function ($exception) {
return $exception->isCritical() ? 'error' : 'info';
},
// Default fallback
'default' => 'error',
],
bash
php artisan vendor:publish --provider="MarceliTo\Wiretap\Providers\WiretapServiceProvider"
bash
php artisan tinker