This read-only repository mirrors
packages/logger. Open issues and pull requests in the thesis-php/thesis monorepo.
Logger implements PSR-3. Stream output runs on the calling fiber and propagates formatting and write errors.
The default minimum level is info. Pass minLevel to change it.
composer require thesis/loggerRequires 64-bit PHP 8.6+.
use Thesis\Logger;
use function Thesis\Io\stderr;
$logger = new Logger(stderr());
$logger->info('Worker {worker} is ready', ['worker' => 3]);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;
use Thesis\Logger\StreamHandler;
$logger = new Logger(new StreamHandler(stderr(), new Console()));Implement Formatter::format(Record $record) to provide your own format. The stream handler calls it with the
DateTimeImmutable captured at the log call, the interpolated message and the normalized context.
Return the formatted record. StreamHandler appends \n and writes the result to the stream.
During capture, formatting and output, a stream handler ignores calls back into the same handler, including calls from tasks started in that context. Independent tasks can still log through it.
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.