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.
composer require thesis/loggerRequires 64-bit PHP 8.6+.
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->start($group);
try {
$logger->info('Worker {worker} is ready', ['worker' => 3]);
} finally {
$logger->stop();
}
});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.
By default, the console omits fields already used in the message. When every field was used, the line ends with the
message. Pass removeUsedContextFields: false to print the full 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.
Logger snapshots the time from its PSR-20 ClockInterface in the calling fiber before converting the message. 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')));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. The logger considers the first 32 entries, skips keys with invalid
UTF-8, a leading NUL or more than 128 bytes, and limits text to 1024 bytes. If an exception carries a
non-scalar message, the exception summary uses its type. Unset file and line fields become '' and 0.
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 start() or after stop(). Logging to the same logger from its writer task or a task started by the
writer also counts as a drop. This prevents a formatter, output or trace listener from feeding records back
into the queue. 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 stop() rethrows it. Queue overflow, normal
shutdown and task cancellation leave this property null.
Stop producers first and then call stop() while the group is still running. stop() 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 stop() can wait for it again.