phasync
Swerve / setLog

Method

Swerve::setLog

Replace the logger that Swerve::log returns.

Description#

public static Swerve::setLog(
    LoggerInterface $log
): void

bin/swerve calls it when a worker starts. An application that embeds swerve without its command line can call it to have log() return its own logger.

Parameters#

$log LoggerInterface
Returned by every later call of Swerve::log()

Examples#

Example 1Log through a PSR-3 logger of your own
use Psr\Log\AbstractLogger;
use Swerve\Swerve;

// Any PSR-3 logger; this one prints each line
Swerve::setLog(new class () extends AbstractLogger {
    public function log($level, string|Stringable $message, array $context = []): void
    {
        echo "[$level] $message ", json_encode($context), "\n";
    }
});

Swerve::log()->info('started');
Swerve::log()->warning('disk almost full: {free} MB', ['free' => 512]);
Output
[info] started []
[warning] disk almost full: {free} MB {"free":512}
Example 2
Swerve::setLog(new Monolog\Logger('app'));

See also#

  • Swerve::logThe log of this process: swerve's own PSR-3 logger, in its format (the time, this worker's slot).