1. Go to this page and download the library: Download units/logger library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
units / logger example snippets
use Units\Logger\ConsoleLogger;
use Units\Logger\Logger;
use Units\Logger\LogLevel;
$logger = Logger::create([
'adapter' => new ConsoleLogger([
'context' => 'suppliers',
'level' => 'info',
'timestamp' => true,
]),
]);
$logger->info('user {id} logged in', ['id' => 42]);
$logger->warn('supplier {id} went quiet', ['id' => 'aurimas']);
$logger->error('job failed', new RuntimeException('boom'));
$logger->log(LogLevel::Warn, 'careful');
echo $logger->whoami(); // Logger v1.0.0 via Units\Logger\ConsoleLogger
namespace Units\Logger;
interface ILogger
{
public function debug(string $message, mixed ...$args): void;
public function info(string $message, mixed ...$args): void;
public function warn(string $message, mixed ...$args): void;
public function error(string $message, mixed ...$args): void;
public function log(LogLevel $level, string $message, mixed ...$args): void;
public function child(string $context): ILogger;
}
final class NatsLogger implements ILogger
{
public function __construct(private readonly NatsClient $nats) {}
public function info(string $message, mixed ...$args): void
{
$this->nats->publish($this->subject, json_encode([$message, $args]));
}
// ... debug, warn, error, log, child
}
$logger = Logger::create(['adapter' => new NatsLogger($nats)]);
$logger = Logger::create(['adapter' => new ConsoleLogger(['level' => 'error'])]);
$logger->info('never printed'); // reaches the adapter; the adapter drops it
$logger->error('always printed');
use Units\Logger\LogLevel;
LogLevel::Info->weight(); // 1 - higher is more severe
LogLevel::shouldLog(LogLevel::Warn, LogLevel::Info); // true - warn survives an info threshold
LogLevel::normalize('ERROR'); // LogLevel::Error
LogLevel::normalize('verbose'); // LogLevel::Info - unknown input is not fatal