thesis / log
PSR-3 logger with a configurable clock, bounded buffering, and asynchronous JSON and colored console output.
Fund package maintenance!
Requires
- php: ^8.6
- psr/clock: ^1.0
- psr/log: ^3.0
- thesis/async: ^0.1
- thesis/io: ^0.1
- thesis/time: ^0.4
Requires (Dev)
- monolog/monolog: ^3.0
- testo/assert: ^0.1.17
- testo/codecov: ^0.2.5
- testo/test: ^0.1.8
- testo/testo: ^0.10.29
- thesis/dispatcher: ^0.4
- thesis/testo-plugin: ^0.1
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-24 00:23:56 UTC
README
Thesis Logger
This read-only repository mirrors
packages/logger. Open issues and pull requests in the thesis-php/thesis monorepo.
Logger implements PSR-3 and writes formatted records from a background task. Use it in CLI servers and
workers that need logging to return even when standard error or another destination is slow.
Installation
composer require thesis/logger
Requires 64-bit PHP 8.6+.
Usage
use Thesis\Async\Group; use Thesis\Logger; use function Thesis\Async\group; use function Thesis\Io\stderr; group(static function (Group $group) { $logger = new Logger(stderr()); $logger->run($group); try { $logger->info('Worker {worker} is ready', ['worker' => 3]); } finally { $logger->close(); } });
Output formats
Formatter\Json is the default. Each NDJSON line has time (RFC 3339 with microseconds), level, message
and a context object. Scalar fields keep their JSON types.
Use Formatter\Console for a text line with a timestamp, a colored level label, the message and any context.
Pass colors: false for plain text.
use Thesis\Logger\Formatter\Console; $logger = new Logger(stderr(), formatter: new Console());
Implement Formatter::format(Record $record) to provide your own format. The writer task calls it with the
DateTimeImmutable captured at the log call, the interpolated message and the normalized context.
Return one line. Logger adds \n and counts output as dropped if it contains a line break or exceeds 16 KiB
including the terminator.
Time and timezone
Logger reads its PSR-20 ClockInterface in the calling fiber. The default Thesis\Time\WallClock uses the
PHP default timezone. Both built-in formatters preserve the offset returned by the clock. Pass your own clock
to control the timestamp, or use WallClock with a fixed timezone.
use Thesis\Time\WallClock; $logger = new Logger(stderr(), clock: new WallClock(new DateTimeZone('UTC')));
Messages and context
Scalar context values fill matching {key} placeholders in every format. Missing keys stay in the message.
The context keeps its fields after interpolation. An exception under
exception becomes an object with type, message, file and line. Other objects, arrays and resources in the context
are represented by their type. A Stringable message is cast to a string on the calling fiber.
Non-finite floats are written as null. Context is capped at 32 entries, keys at 128 bytes and
text at 1024 bytes.
Buffering and shutdown
The default minimum level is info and the default queue holds 1024 lines. Pass minLevel or capacity
to change these. A full queue drops the incoming record. $dropped counts records refused or lost, including
before run() or after close(). Records filtered by level do not count.
Read $logger->failure to check whether formatting or writing stopped the background task. It holds the
original throwable as soon as the writer catches it, and close() rethrows it. Queue overflow, normal
shutdown and task cancellation leave this property null.
Stop producers first and then call close() while the group is still running. close() stops accepting
records and drains the queue. It leaves the destination open and throws if formatting or writing failed. In a cancelled
scope use shield() with a timeout() to give the writer time to drain. A cancelled wait leaves the writer
free to continue until the group ends, and a later close() can wait for it again.