PHP code example of gilbitron / canonical-context-logging-laravel

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

    

gilbitron / canonical-context-logging-laravel example snippets


use CanonicalContextLogging\Laravel\Facades\CanonicalContext;

// Add single context
CanonicalContext::addContext('org_id', 123);
CanonicalContext::addContext('plan', 'premium');
CanonicalContext::addContext('feature_flags', ['feature_a', 'feature_b']);

// Add multiple at once
CanonicalContext::addContexts([
    'org_id' => 123,
    'plan' => 'premium',
    'feature_flags' => ['feature_a', 'feature_b'],
]);

// Set error
try {
    // ...
} catch (\Exception $e) {
    CanonicalContext::setError($e);
    throw $e;
}

// Get current context
$context = canonical_context();

// Add context
canonical_add_context('org_id', 123);

// Set error
canonical_set_error($exception);

use CanonicalContextLogging\Laravel\Facades\CanonicalContext;

$context = CanonicalContext::context();
if ($context !== null) {
    $context->addContext('custom_key', 'custom_value');
}

// app/Exceptions/Handler.php
use CanonicalContextLogging\Laravel\Facades\CanonicalContext;

public function render($request, \Throwable $e)
{
    CanonicalContext::setError($e);
    return parent::render($request, $e);
}

'exporter' => [
    'type' => 'console',
    'console' => [
        'use_stderr' => true,  // Use stderr instead of stdout
        'pretty_print' => false, // Pretty-print JSON
    ],
],

'exporter' => [
    'type' => 'file',
    'file' => [
        'path' => storage_path('logs/canonical.jsonl'),
        'pretty_print' => false,
    ],
],

'exporter' => [
    'type' => 'otlp',
    'otlp' => [
        'endpoint' => 'https://otel-collector:4318',
        'protocol' => 'http/protobuf', // or 'http/json'
        'headers' => ['Authorization' => 'Bearer token'],
        'timeout' => 10,
    ],
],

'logger' => [
    'slow_request_threshold' => 1.0, // Always log requests slower than 1 second
    'sample_rate' => 0.1, // Sample 10% of normal requests
],

'service' => [
    'name' => 'my-service', // Defaults to config('app.name')
    'version' => '1.0.0',   // Defaults to config('app.version', '1.0.0')
],

'middleware' => [
    'enabled' => true,              // Enable/disable middleware
    'capture_user' => true,         // Capture authenticated user info
    'capture_request_headers' => false, // Capture all request headers
],

// config/canonical-context-logging.php
'middleware' => [
    'enabled' => false,
],

use CanonicalContextLogging\Laravel\Facades\CanonicalContext;
use CanonicalContextLogging\Middleware\RequestMiddleware;

$middleware = app(RequestMiddleware::class);

// Start context
$context = $middleware->start();
$context->setService('my-service', '1.0.0');
$context->addContext('user_id', 123);

// ... your application logic ...

// End context
$context->setStatus(200);
$middleware->end($context);
bash
php artisan vendor:publish --tag=canonical-context-logging-config
bash
export OTEL_EXPORTER_OTLP_ENDPOINT=https://otel-collector:4318
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer token123"

traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01