Skip to content
thesis-phpPublic

About

PSR-3 logger with a configurable clock, bounded buffering, and asynchronous JSON and colored console output.

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

Thesis Logger

PHP Release License PHPStan Coverage Mutation score

This read-only repository mirrors packages/logger. Open issues and pull requests in the thesis-php/thesis monorepo.

Report a bug · Request a feature · Changelog · Contributing

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.

About

PSR-3 logger with a configurable clock, bounded buffering, and asynchronous JSON and colored console output.

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages