cleq / cleq-php
PHP client for the CLEQ ingest gateway, with a Monolog handler and Laravel integration.
Requires
- php: >=8.1
- ext-curl: *
- ext-json: *
- monolog/monolog: ^3.0
Requires (Dev)
- phpunit/phpunit: ^10.5
Suggests
- laravel/framework: Enables the cleq log channel through the service provider (Laravel 10+).
Provides
None
Conflicts
None
Replaces
None
README
PHP client for the CLEQ ingest gateway (cleq-ingest), with a Monolog handler and a Laravel log channel. PHP 8.1 or newer, ext-curl, ext-json, Monolog 3. It buffers entries, sends them in batches, and never throws into your app.
Install
composer require cleq/cleq-php
In Laravel the service provider is auto-discovered.
Unreleased builds
From a CLEQ checkout, for changes not yet on Packagist, with a Composer path repository (in your app's composer.json):
{
"repositories": [
{ "type": "path", "url": "../CLEQ/sdk/php", "options": { "symlink": false } }
],
"require": { "cleq/cleq-php": "*" }
}
A path repository copies the directory as it is, including a local vendor/ if one is present. The zip is the clean route: make sdk-pack strips vendor/, tests/ and the lock file.
From the zip: make sdk-pack in the CLEQ repo writes dist/cleq-php-0.1.0.zip. Put it in a directory and use an artifact repository:
{
"repositories": [{ "type": "artifact", "url": "vendor-zips/" }],
"require": { "cleq/cleq-php": "0.1.0" }
}
Publishing
Maintainers only. Development stays in the CLEQ monorepo; cleq-php is a read-only mirror, needed because Packagist reads only a root composer.json. Keep version here equal to the tag (the internal zip install needs it).
make sdk-split # split/php = sdk/php as a repository root
git push git@github.com:chaad-grayson/cleq-php.git split/php:main split/php:refs/tags/v0.1.0
Packagist picks up the tag through the GitHub webhook.
Configuration
Options go in an array. They win over environment variables. A missing url, key or dataset reports once and disables the client.
| Option | Env var | Default | Meaning |
|---|---|---|---|
url |
CLEQ_INGEST_URL |
required | Ingest base URL, e.g. https://logs.example.com:3101 |
key |
CLEQ_INGEST_KEY |
required | Ingest key (Bearer token, never logged) |
dataset |
CLEQ_DATASET |
required | Registered dataset slug |
project |
CLEQ_PROJECT |
none | Project slug. Required with an organization key; a project key ignores it |
max_batch_entries |
1000 | Entries per batch; reaching it flushes | |
max_buffer_entries |
10000 | Unsent entries kept; the oldest are dropped beyond this | |
max_batch_bytes |
4 MiB | JSON bytes per request; a larger single entry is dropped | |
flush_interval |
1.0 s | A log() call flushes when the oldest buffered entry is older than this |
|
flush_budget |
3.0 s | Total time one flush may take | |
failure_cooldown |
10.0 s | After a failed flush, log() stops triggering flushes for this long |
|
timeout |
2.0 s | Per-request timeout | |
max_attempts |
5 | Attempts per batch | |
retry_base_ms / retry_max_ms |
100 / 1000 | Backoff base and cap | |
on_error |
error_log |
callable(CleqException), called for every failure |
|
env |
getenv() |
Where CLEQ_* are read from |
Usage
Plain client:
$cleq = new \Cleq\Client(['url' => $url, 'key' => $key, 'dataset' => 'api', 'project' => 'shop']); $cleq->log('warning', 'slow query', ['ms' => 840]); $cleq->flush(); // optional: send now
Monolog:
use Cleq\Monolog\CleqHandler; use Monolog\Logger; $logger = new Logger('app', [new CleqHandler(['dataset' => 'api'])]); $logger->warning('slow query', ['ms' => 840]);
Exceptions in the context become {class, message, code, file, trace}.
Laravel. Set the variables in .env:
CLEQ_INGEST_URL=https://logs.example.com:3101
CLEQ_INGEST_KEY=...
CLEQ_DATASET=api
Add the channel in config/logging.php. Pass the values through options with env(), not by leaving them to getenv():
'channels' => [ 'cleq' => [ 'driver' => 'custom', 'via' => \Cleq\Laravel\CreateCleqLogger::class, 'level' => 'debug', 'options' => [ 'url' => env('CLEQ_INGEST_URL'), 'key' => env('CLEQ_INGEST_KEY'), 'dataset' => env('CLEQ_DATASET'), 'project' => env('CLEQ_PROJECT'), ], ], ],
After php artisan config:cache, Laravel no longer loads .env, so the SDK cannot see the variables through getenv(). Values read with env() inside the config file are cached with it and keep working.
To keep your existing log file and add CLEQ, use the stack channel. On Laravel 11 and later, set LOG_STACK:
LOG_CHANNEL=stack
LOG_STACK=single,cleq
On Laravel 10, list cleq in the stack channel's channels array in config/logging.php instead: 'channels' => ['single', 'cleq'].
The provider binds one shared client per process and flushes it when Laravel terminates. Client options for the provider come from logging.channels.cleq.options.
Behavior
- Flush triggers: 1000 buffered entries; a
log()call that finds the oldest buffered entry older than 1 s; end of the request (Laravel terminating); PHP shutdown; Monologclose()andreset(). - Budget: each flush has 3 s in total (
flush_budget). When it runs out, the undelivered entries are dropped and reported. - Cooldown: after a failed flush,
log()does not trigger flushes for 10 s (failure_cooldown); entries keep buffering and are tried again on an explicit flush or at shutdown. - Buffer: at most 10000 unsent entries; the oldest are dropped first.
- Retry: 429, 5xx, network errors and timeouts are retried, up to 5 attempts, backoff from 100 ms up to 1 s, honoring Retry-After (capped at 1 s). Request timeout is 2 s. Other 4xx answers (400, 401, 403, 413) are final and the batch is dropped.
- Errors:
on_errorreceives aCleqException. The default writes toerror_log, at most once per 10 s. The key is never part of a message. - Levels: the raw Monolog level name (for example
WARNING) is sent and mapped by the gateway.
Queue workers and Octane
Laravel's terminating callbacks do not run between jobs in a queue worker or between requests under Octane. The last entries of a job are sent on the next log() call after 1 s, on Monolog reset(), or at process exit. To send them after every job, flush explicitly from a service provider:
use Illuminate\Support\Facades\Queue; public function boot(): void { Queue::after(fn () => app(\Cleq\Client::class)->flush()); }