Skip to content
v1
Docs

PHP SDK

Logs from PHP

You need a log source token (WT15_LOGS_TOKEN, wt15ls_…): see custom log sources and the log ingest API. On a host that runs the WatchTower15 agent, the agent’s file tail is the better path: no token in the application and no HTTP from PHP. Use the SDK where it does not run. Every line is billed per GB, so pick the level with care.

$wt15->ingest->logs->push([
['message' => 'payment failed', 'log_level' => 'error', 'stream_name' => 'checkout'],
['message' => 'retrying', 'logged_at' => new DateTimeImmutable()],
]);

message must not be blank; it and raw_line are cut at 65,536 bytes on a character boundary. Lines are chunked (1,000 lines, 5,000,000 bytes), gzipped and keyed like metrics. A key reused for another batch within 24 hours is refused with 422 idempotency_key_reused.

use Monolog\Handler\BufferHandler;
use Monolog\Logger;
use Monolog\Processor\PsrLogMessageProcessor;
use WatchTower15\Client;
use WatchTower15\Logs\Monolog\WatchTower15Handler;
$wt15 = Client::fromEnvironment(); // WT15_LOGS_TOKEN
$log = new Logger('billing');
$log->pushProcessor(new PsrLogMessageProcessor());
$log->pushHandler(new BufferHandler(
new WatchTower15Handler($wt15, selfLogger: $otherLogger), // a logger that does not hold this handler
100,
flushOnOverflow: true,
));

Install Monolog yourself (composer require monolog/monolog): the SDK only suggests it. For Monolog 2, use Monolog’s PsrHandler over the PSR-3 logger below.

  • One line per record: message is the record formatted by the handler’s formatter, log_level the record’s level, logged_at the record’s time, and stream_name the handler’s stream option or else the record’s channel. A stream name that does not match ^[A-Za-z0-9._/@:+-]{1,255}$ is left out, so the source’s own name applies.
  • Wrap the handler in BufferHandler, so a request’s lines go in one push.
  • The push waits for the response to end where the runtime can (fastcgi_finish_request() on PHP-FPM and FrankenPHP, litespeed_finish_request() on LiteSpeed). Under mod_php the push holds the response for its round trip, up to about 8 s when ingest fails: use the agent’s file tail there.
  • On serverless, flush before the invocation answers (BufferHandler::close()). In a long-running loop, call flush() after each unit of work.
use WatchTower15\Logs\Psr3Logger;
$logger = new Psr3Logger($wt15, stream: 'billing', selfLogger: $otherLogger, bufferLimit: 100);
$logger->error('Payment failed for {order}', ['order' => 42, 'exception' => $e]);

A PSR-3 logger that buffers lines and sends them at bufferLimit lines, on flush(), at the end of the request after the response and on destruct.

Every push has a 3 s timeout, a 1 s connect timeout and one retry. A retried push is stored and billed once. A failing ingest must not tie up your workers, so a per-host breaker remembers it:

  • The volume stop (your organisation reached its log volume limit) lasts the answer’s Retry-After, at most an hour.
  • A hold of a minute follows an unreachable ingest, a 5xx, a 403 (a revoked token) or the per-token throttle.
  • While either is open the lines are dropped without a request; when it ends, one push probes ingest.

The breaker lives in files under the system temporary directory. Every user that runs the application (PHP-FPM, cron, a deploy user) must be able to write there, or pass a FileBreakerStore($directory); use one directory per application.

Nothing reaches your application. Lines that are not sent are counted by reason, and at most one report per flush goes to the selfLogger at warning:

WatchTower15 logs: lines not sent: rejected_503 12, invalid_payload 1