PHP code example of logtrace / logtrace-php

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

    

logtrace / logtrace-php example snippets


use Logtrace\Client;
use Logtrace\CreateEventRequest;

$client = new Client(getenv('LOGTRACE_API_KEY'));

$client->createEvent(new CreateEventRequest(
    name:      'user.signup',
    user_id:          '123',
    metadata:        ['plan' => 'pro'],
));

$client->createSession(new CreateSessionRequest(...));
$client->createAuditLog(new CreateAuditLogRequest(...));

use Logtrace\Middleware;

$app->add(new Middleware($client));

$rc = $request->getAttribute(Middleware::ATTRIBUTE);

$rc->createEvent(new CreateEventRequest(
    name: 'order.placed',
    // ...
));

use Logtrace\LogtraceException;

try {
    $client->createEvent($req);
} catch (LogtraceException $e) {
    echo $e->statusCode; // HTTP status
    echo $e->getMessage();
}
bash
composer