The Log provides simple solution for advanced logging.
composer require webiik/log
$log = new \Webiik\Log\Log();
$log->addLogger(function () {
return new \Webiik\Log\Logger\FileLogger();
});
$log->info('Hello {name}!', ['name' => 'Dolly!']);
$log->write();
addLogger(callable $factory): Logger
addLogger() creates new Logger and injects $factory into it. Adds created Logger to Log and returns it. To process logs you have to add some Logger(s) to Log. Log comes with 3 optional loggers: ErrorLogger, FileLogger and MailLogger.
$log->addLogger(function () {
return new \Webiik\Log\Logger\ErrorLogger();
});
Write Custom Logger
You can write your custom logger. Only thing you have to do is to implement Webiik\Log\Logger\LoggerInterface
.
// CustomLogger.php
declare(strict_types=1);
use Webiik\Log\Message;
class CustomLogger implements Webiik\Log\Logger\LoggerInterface
{
public function write(Message $message): void
{
// Process Message...
}
}
emergency(string $message, array $context = []): Message
alert(string $message, array $context = []): Message
critical(string $message, array $context = []): Message
error(string $message, array $context = []): Message
warning(string $message, array $context = []): Message
notice(string $message, array $context = []): Message
info(string $message, array $context = []): Message
debug(string $message, array $context = []): Message
log(string $level, string $message, array $context = []): Message
Adds Message to Log. Added Messages are not written until the method write() is called. The message may contain {placeholders} which will be replaced with values from the context array. It return Message.
$log->info('Hello {name}!', ['name' => 'Dolly!']);
write(): void
write() removes all added Messages and writes them using the associated loggers.
$log->write();
Message->setData(array $data): Message
setData() adds extra data to your Message.
$log->info('Hello Dolly!')->setData(['greeter' => 'Molly']);
Logger->setGroup(string $group): Logger
Message->setGroup(string $group): Message
setGroup() adds Logger to positive group. Every logger and log message can belong to one or more positive group. When logger belongs to some group(s) then it logs only messages belonging to same group(s).
// This logger logs only log messages belonging to 'error' group
$log->addLogger(function () {
return new \Webiik\Log\Logger\ErrorLogger();
})->setGroup('error');
// Add some log messages
$log->info('Some info.');
$log->warning('Some error.')->setGroup('error');
Logger->setNegativeGroup(string $group): Logger
Message->setGroup(string $group): Message
setNegativeGroup() adds Logger to negative group. Every logger can belong to one or more negative group. When logger belongs to some negative group(s) then it doesn't log messages belonging to same group(s).
// This logger doesn't log messages belonging to 'error' group
$log->addLogger(function () {
return new \Webiik\Log\Logger\FileLogger();
})->setNegativeGroup('error');
// Add some log messages
$log->info('Some info.');
$log->warning('Some error.')->setGroup('error');
Logger->setLevel(string $level): Logger
setLevel() sets Logger to write only Messages with certain PSR-3 log level.
// This logger logs messages from all groups but only with log level 'info'
$log->addLogger(function () {
return new \Webiik\Log\Logger\FileLogger();
})->setLevel('info');
setSilent(bool $silent): void
setSilent() configures Log to skip failed Loggers. In silent mode failed loggers don't stop code execution, instead of it these incidents are logged with other loggers. The default value is FALSE.
$log = setSilent(true);