Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
161 lines (144 sloc) 4.45 KB

Log

The Log provides simple solution for advanced logging.

Installation

composer require webiik/log

Example

$log = new \Webiik\Log\Log();
$log->addLogger(function () {
    return new \Webiik\Log\Logger\FileLogger();
});
$log->info('Hello {name}!', ['name' => 'Dolly!']);
$log->write();

Loggers

addLogger

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...
    }
}

Messages

add

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

write(): void

write() removes all added Messages and writes them using the associated loggers.

$log->write();

setData

Message->setData(array $data): Message

setData() adds extra data to your Message.

$log->info('Hello Dolly!')->setData(['greeter' => 'Molly']);

Groups

setGroup

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');

setNegativeGroup

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');

Levels

setLevel

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');

Silent mode

setSilent

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);

Resources

You can’t perform that action at this time.