NullObject - 3
The snippet can be accessed without any authentication.
Authored by
Krzysztof Dyszczyk
Edited
nullLogger.php 1.43 KiB
interface LoggerInterface
{
/**
* System is unusable.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function emergency($message, array $context = array());
/**
* Detailed debug information.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function debug($message, array $context = array());
/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param mixed[] $context
*
* @return void
*
* @throws \Psr\Log\InvalidArgumentException
*/
public function log($level, $message, array $context = array());
}
class NullLogger implements LoggerInterface
{
/**
* @inheritDoc
*/
public function emergency($message, array $context = array())
{
$this->log(LogLevel::EMERGENCY, $message, $context);
}
/**
* @inheritDoc
*/
public function debug($message, array $context = array())
{
$this->log(LogLevel::DEBUG, $message, $context);
}
/**
* @inheritDoc
*/
public function log($level, $message, array $context = array())
{
// noop
}
}
function get_logger(): LoggerInterface {
try {
// some magic code to initialize and return a logger
} catch (Logger_Cannot_Initialize $e) {
return new NullLogger();
}
}
Please register or sign in to comment