Slim, simple and full compatible PSR-3 Logger library for PHP.
Install via the composer utility.
composer require "phoole/logger"
or add the following lines to your composer.json
{
"require": {
"phoole/logger": "1.*"
}
}Create the logger instance with a channel id,
usePsr\Log\LogLevel;
usePhoole\Logger\Logger;
usePhoole\Logger\Entry\MemoryInfo;
usePhoole\Logger\Handler\SyslogHandler;
usePhoole\Logger\Handler\TerminalHandler;
// with channel id$logger = newLogger('MyApp');
// log every warning to syslog$logger->addHandler(
LogLevel::WARNING,
newSyslogHandler()
);
// log to terminal for MemoryInfo entry$logger->addHandler(
LogLevel::INFO,
newTerminalHandler(),
MemoryInfo::class // handle this log object only
);
// log a text message$logger->warning('a warning message');
// log memory usage$logger->info(newMemoryInfo());A log entry is a message in the form of an object. It solves the problem of 'WHAT TO BE SENT OUT'. It has a message template, and some processors to process its context.
For example,
Entry\MemoryInfois a predefined log entry with a message template of{memory_used}M memory used , peak usage is {memory_peak}Mand oneProcessor\MemoryProcessorprocessor.// with predefined template and processor$logger->warning(newMemoryInfo()); // use new template$logger->warning(newMemoryInfo('Peak memory usage is {memory_peak}M'));
Entry\LogEntryis the log entry prototype used whenever text message is to be logged// using LogEntry$logger->info('test only');
To define your own log entry,
usePhoole\Logger\Entry\LogEntry; class MyMessage extends LogEntry { // message templateprotected$message = 'your {template}'; } // add handler$logger->addHandler( 'warning', // levelfunction(LogEntry$entry) { // a handlerecho (string) $entry; }, MyMessage::class // handle this type of message only ); // output: 'your wow'$logger->error(newMyMessage(), ['template' => 'wow']);
Processors are associated with log entry classes. They solve the problem of 'WHAT EXTRA INFO TO SENT OUT'. They will inject information into entries' context. Processors are
callable(LogEntryInterface $entry),usePhoole\Logger\Processor\ProcessorAbstract; // closure$processor1 = function(LogEntry$entry) { }; // invokable object$processor2 = newclass() { publicfunction__invoke(LogEntry$entry) { } } // extends class Processor3 extends ProcessorAbstract { protectedfunctionupdateContext(array$context): array { $context['bingo'] = 'wow'; return$context; } }
Processors are attached to log entries either in the entry class definition as follows,
class MyMessage extends LogEntry { // message templateprotected$message = 'your {template}'; // define processors for this classprotectedstaticfunctionclassProcessors(): array { return [ function(LogEntry$entry) { $context = $entry->getContext(); $context['template'] = 'wow'; $entry->setContext($context); }, newmyProcessor(), ]; } }
or during the handler attachment
usePhoole\Logger\Handler\SyslogHandler; // will also add 'Processor1' and 'Processor2' to 'MyMessage' class$logger->addHandler( 'info', newSyslogHandler(), MyMessage::addProcessor( newProcessor1(), newProcessor2(), ... ) );
Handlers solve the problem of 'WHERE TO SEND MESSAGE'. They take a log entry object and send it to somewhere.
Handlers takes the form of
callable(LogEntryInterface $entry)as follows,usePhoole\Logger\Handler\HandlerAbstract; $handler1 = function(LogEntry$entry) { echo (string) $entry; } $handler2 = newclass() { publicfunction__invoke(LogEntry$entry) { } } class Handler3 extends HandlerAbstract { protectedfunctionwrite(LogEntryInterface$entry) { echo$this->>getFormatter()->format($entry); } }
Handlers are added to the
$loggerwith specific log level and type of log message they are going to handle (default isLogEntryInterface).$logger->addHandler( LogLevel::WARNING, newTerminalHandler(), LogEntryInterface::class // this is the default anyway );
Formatters solve the problem of 'HOW MESSAGE WILL BE PRESENTED''. Each handler of the type
Handler\HandlerAbstractmay have formatter specified during its initiation.usePhoole\Logger\Handler\TerminalHandler; usePhoole\Logger\Formatter\AnsiFormatter; // use ANSI Color formatter$handler = newTerminalHandler(newAnsiFormatter()); // add handler handles 'ConsoleMessage' ONLY$logger->addHandler('debug', $handler, ConsoleMessage::class); // log to console$logger->info(newConsoleMessage('exited with error.')); // this will goes handlers handling 'LogEntry'$logger->info('exited with error');
See PSR-3 for standard related APIs.
__construct(string $channel)Create the logger with a channel id.
addHandler(string $level, callable $handler, string $entryClass, int $priority = 50): $thisAdd one handler to specified channel with the priority.
Phoole\Logger\Entry\LogEntryrelatedstatic function addProcessor(callable ...$callables): stringThis method will returns called class name.
$ composer testPHP >= 7.2.0
phoole/base 1.*
