1. Go to this page and download the library: Download phoole/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/ */
phoole / logger example snippets
use Psr\Log\LogLevel;
use Phoole\Logger\Logger;
use Phoole\Logger\Entry\MemoryInfo;
use Phoole\Logger\Handler\SyslogHandler;
use Phoole\Logger\Handler\TerminalHandler;
// with channel id
$logger = new Logger('MyApp');
// log every warning to syslog
$logger->addHandler(
LogLevel::WARNING,
new SyslogHandler()
);
// log to terminal for MemoryInfo entry
$logger->addHandler(
LogLevel::INFO,
new TerminalHandler(),
MemoryInfo::class // handle this log object only
);
// log a text message
$logger->warning('a warning message');
// log memory usage
$logger->info(new MemoryInfo());
// with predefined template and processor
$logger->warning(new MemoryInfo());
// use new template
$logger->warning(new MemoryInfo('Peak memory usage is {memory_peak}M'));
// using LogEntry
$logger->info('test only');
use Phoole\Logger\Entry\LogEntry;
class MyMessage extends LogEntry
{
// message template
protected $message = 'your {template}';
}
// add handler
$logger->addHandler(
'warning', // level
function(LogEntry $entry) { // a handler
echo (string) $entry;
},
MyMessage::class // handle this type of message only
);
// output: 'your wow'
$logger->error(new MyMessage(), ['template' => 'wow']);
use Phoole\Logger\Processor\ProcessorAbstract;
// closure
$processor1 = function(LogEntry $entry) {
};
// invokable object
$processor2 = new class() {
public function __invoke(LogEntry $entry)
{
}
}
// extends
class Processor3 extends ProcessorAbstract
{
protected function updateContext(array $context): array
{
$context['bingo'] = 'wow';
return $context;
}
}
class MyMessage extends LogEntry
{
// message template
protected $message = 'your {template}';
// define processors for this class
protected static function classProcessors(): array
{
return [
function(LogEntry $entry) {
$context = $entry->getContext();
$context['template'] = 'wow';
$entry->setContext($context);
},
new myProcessor(),
];
}
}
use Phoole\Logger\Handler\SyslogHandler;
// will also add 'Processor1' and 'Processor2' to 'MyMessage' class
$logger->addHandler(
'info',
new SyslogHandler(),
MyMessage::addProcessor(
new Processor1(),
new Processor2(),
...
)
);
use Phoole\Logger\Handler\HandlerAbstract;
$handler1 = function(LogEntry $entry) {
echo (string) $entry;
}
$handler2 = new class() {
public function __invoke(LogEntry $entry)
{
}
}
class Handler3 extends HandlerAbstract
{
protected function write(LogEntryInterface $entry)
{
echo $this->>getFormatter()->format($entry);
}
}
$logger->addHandler(
LogLevel::WARNING,
new TerminalHandler(),
LogEntryInterface::class // this is the default anyway
);
use Phoole\Logger\Handler\TerminalHandler;
use Phoole\Logger\Formatter\AnsiFormatter;
// use ANSI Color formatter
$handler = new TerminalHandler(new AnsiFormatter());
// add handler handles 'ConsoleMessage' ONLY
$logger->addHandler('debug', $handler, ConsoleMessage::class);
// log to console
$logger->info(new ConsoleMessage('exited with error.'));
// this will goes handlers handling 'LogEntry'
$logger->info('exited with error');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.