1. Go to this page and download the library: Download lfbn/logger-trait 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/ */
lfbn / logger-trait example snippets
// In the class you want, add the use of the Trait.
use LoggerTrait;
(...)
// After that, you can use it.
$this->logError('Some message...', ['some context']);
class MyClass {
protected function initLogger(): bool
{
$this->logger = new \Monolog\Logger('Overriding default logger: '.$this->getLoggerName());
try {
$handler = new StreamHandler(
$this->getLoggerStream(),
$this->getLoggerMinimumLevel()
);
$this->logger->pushHandler($handler);
} catch (Exception $e) {
$this->logger = new NullLogger();
return false;
}
return true;
}
}
class Test {
use \Lfbn\LoggerTrait\LoggerTrait;
public function test(): void
{
$this->logDebug('Hello TEST!');
}
}
$logger = new \Monolog\Logger('test4-my-own-logger');
try {
$handler = new StreamHandler(
'php://stdout',
'debug'
);
$logger->pushHandler($handler);
} catch (Exception $e) {
$logger = new NullLogger();
}
$myClass = (new Test());
$myClass->setLogger($logger);
$myClass->test();