PHP code example of zemkogabor / xinfra-php

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

    

zemkogabor / xinfra-php example snippets


use XInfra\Client;
use XInfra\Config;
use XInfra\Level;
use XInfra\Processor\GlobalRequestProcessor;

$client = new Client(
    new Config(
        url: 'https://xinfra.example.com/api/ingest/events',
        key: 'your-key',
        environment: 'prod',
        minLevel: Level::Warning,
        release: 'v1.4.0',
        server: null,        // default: the hostname
        maxBatchSize: 50,    // events per request, at most 100
        timeout: 2.0,        // seconds, for connecting and for the whole request
    ),
    processors: [new GlobalRequestProcessor()],
);

use XInfra\Client;
use XInfra\ErrorHandler;

$client = Client::fromEnvironment();

// Uncaught exceptions, PHP warnings/notices and fatal errors
ErrorHandler::register($client);

// Your own events
$client->captureMessage('warning', 'Stock sync was slow', ['seconds' => 42]);
$client->captureException($exception);
$client->captureException($exception, 'critical', 'Payment failed', ['orderId' => 42]);

set_exception_handler(function (Throwable $e) use ($client): void {
    $client->captureException($e, 'error', 'Uncaught ' . $e::class . ': ' . $e->getMessage());
    // ... your own handling
});

set_error_handler(function (int $level, string $message, string $file, int $line) use ($client): bool {
    $client->captureError('warning', 'Warning: ' . $message, $file, $line);
    // ... your own handling
    return false;
});

use Monolog\Logger;
use Monolog\Processor\PsrLogMessageProcessor;
use XInfra\Client;
use XInfra\Monolog\XInfraHandler;

$logger = new Logger('app', [new XInfraHandler(Client::fromEnvironment())], [new PsrLogMessageProcessor()]);

$logger->error('Import of {file} failed', ['file' => 'products.csv', 'exception' => $e]);

$logger->pushProcessor(function (LogRecord $record): LogRecord {
    $record->extra['tags'] = ['tenant' => (string) $tenantId];

    return $record;
});

$client->flush();

use XInfra\Event;
use XInfra\EventProcessor;

final class TenantProcessor implements EventProcessor
{
    public function process(Event $event): ?Event
    {
        $event->tags['tenant'] = (string) current_tenant_id();

        return $event;
    }
}

$client->addProcessor(new TenantProcessor());
bash
composer