PHP code example of code-distortion / clarity-context

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

    

code-distortion / clarity-context example snippets

 php
// in a file in your project
Clarity::context('Performing checkout', ['user-id' => $userId, 'order-id' => $orderId]);
…
 bash
php artisan vendor:publish --provider="CodeDistortion\ClarityContext\ServiceProvider" --tag="config"
 php
Clarity::context("A quick description of what's currently happening");
Clarity::context(['some-relevant-id' => 123]);
 php
Clarity::context("Processing csv file", ['file' => $file, 'category' => $categoryId]);
 php
Clarity::traceIdentifier($traceId);
 php
// app/Exceptions/Handler.php

namespace App\Exceptions;

use CodeDistortion\ClarityContext\Clarity; // <<<
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;

class Handler extends ExceptionHandler
{
    …

    /**
     * Register the exception handling callbacks for the application.
     */
    public function register(): void
    {
        $this->reportable(function (Throwable $e) {

            $context = Clarity::getExceptionContext($e); // <<<
            // … perform formatting and logging here
        });
    }
}
 php
$context->getStackTrace()->getMeta(ContextMeta::class); // only ContextMeta objects will be returned
 php
$context->getStackTrace()->getMetaGroups();
$context->getCallStack()->getMetaGroups();
 php
$context = Clarity::buildContextHere();