Flare PHP Client

repository·main·Indexed 41 days ago

https://github.com/spatie/flare-client-php

A standalone PHP client for sending errors, traces, and logs to Flare. It provides tools for distributed tracing via the Tracer class, error reporting through the ReportFactory, and environmental context enrichment using the Resource class. Requires PHP 8.2 or higher.

Tokens
7K
Snippets
29
Records
36
Agent score
51%

What's inside Flare

  1. How logging works in v3

    main

    v3 introduces Dedicated logging as a first-class feature via $flare->log(). This replaces the deprecated reportMessage() method and records messages in the OpenTelemetry log format. Log levels now use Monolog's Level enum.

    Standalone logging is opt-in. It is recommended to pair it with the Flare daemon to ensure logs are buffered in memory and flushed asynchronously, keeping delivery off the request thread.

    use Monolog\Level;
    use Spatie\FlareClient\FlareConfig;
    use Spatie\FlareClient\Senders\DaemonSender;
    
    FlareConfig::make('your-api-key')
        ->log(minimalLevel: Level::Info)
        ->sendUsing(DaemonSender::class);
  2. Use Attribute Providers for Recorders

    main

    In v3, recorders no longer accept loose arguments (like request objects or status codes) directly. Instead, they use Attribute Providers that implement specific contracts (e.g., RequestAttributesProvider, ResponseAttributesProvider).

    Recorders provide helper methods (e.g., recordStartFromSymfonyRequest()) to simplify creating these providers from common framework objects.

    // Request Recording Example
    // Before
    $flare->request()->recordStart(
        request: $request,
        route: '/users/{id}',
        entryPointClass: UserController::class,
    );
    
    // After (using provider)
    $flare->request()->recordStart(
        new SymfonyRequestAttributesProvider($redactor, $request),
    );
    
    // After (using helper)
    $flare->request()->recordStartFromSymfonyRequest($request);
  3. Register middleware in v2

    main

    In version 2, you can no longer add middleware inline using a closure. You must register middleware classes via the Flare config object using the collectFlareMiddleware method.

    $config->collectFlareMiddleware([
        MyMiddleware::class => [],
    ]);
  4. Upgrade from v2 to v3

    main
    Upgrading to v3 introduces better support for logging and tracing lifetimes. Most typical applications will not require significant code changes. If you use a framework integrator like spatie/laravel-flare, you can skip the 'Framework integrator changes' section of the migration guide.
  5. Update context usage in v2

    main

    In version 2, the group method has been removed. To add context data, use the context() method. You can add a single item or an associative array of multiple items.

    // Single item
    $flare->context('key', 'value');
    
    // Multiple items
    $flare->context([
        'key' => 'value',
        'key2' => 'value2',
    ]);
  6. Upgrade from v3.0 to v3.1 in framework-agnostic apps

    main

    In version 3.1, the way you initialize the Flare client in framework-agnostic applications has changed. You must replace the registerFlareHandlers() method with $flare->start(). Calling start() is required to open the lifecycle for the client.

    Note: If you are using the spatie/laravel-flare package, no changes are required as the integrator handles this automatically.

    $flare = Flare::make($config)->start();
  7. Remove Error Solutions usage

    main
    Error solutions are no longer collected or sent in v3. You should remove calls to $config->collectSolutions(), $flare->withSolutionProvider(), $config->ignoreSolutions(), and the AddSolutions middleware. The spatie/error-solutions dependency can also be removed.
  8. Migrate from v1 to v2

    main

    Upgrading from version 1 to version 2 involves a complete rewrite of the package. Key changes include:

    • PHP Requirement: The package now requires PHP 8.2 or higher.
    • Configuration Method Renames: Several methods that were previously called directly on the Flare instance must now be called on the Flare config object:
      • anonymizeIp() is now censorClientIps()
      • censorRequestBodyFields() is now censorBodyFields()
      • reportErrorLevels()
      • overrideGrouping()
    • Context Changes: The concept of 'groups' has been removed. Use the context() method instead of the group() method.
    • Glow Changes: MessageLevels is now an enum. The syntax for adding glows has changed.
    • Argument Reducers: argumentReducers() has been removed. Use collectStackFrameArguments() on the Flare config object instead.
    • Middleware: Inline middleware via closures is no longer supported. Middleware classes must be registered on the Flare config object.
    // New context usage
    $flare->context('key', 'value');
    $flare->context(['key' => 'value', 'key2' => 'value2']);
    
    // New glow usage
    $flare->glow()->record('This is a message from glow!', MessageLevels::Debug);
    
    // New middleware registration
    $config->collectFlareMiddleware([
        MyMiddleware::class => [],
    ]);
  9. Manage subtasks in long-running processes (v3.1+)

    main

    For applications running in subtask mode, such as queue workers or other long-running processes, continue to use $flare->lifecycle->startSubtask() for every individual unit of work to ensure proper trace separation.

    $flare->lifecycle->startSubtask();
  10. Update glow usage in v2

    main

    In version 2, MessageLevels is an enum and the method chain for adding glows has changed. Instead of calling glow() with arguments, you must call glow() and then record().

    // New way to add a glow
    $flare->glow()->record('This is a message from glow!', MessageLevels::Debug);