<?php
use Psr\Log\{AbstractLogger, LoggerInterface, LoggerAwareInterface};
$logger = new class() extends AbstractLogger {
public function log($level, $message, array $context = array())
{
$msg = sprintf('[%s] %s', $level, $message);
if ($context) {
$search = array_keys($context);
array_walk($search, function(&$value, $key) {
$value = sprintf('{%s}', $value);
});
$replace = array_values($context);
$msg = str_replace($search, $replace, $msg);
}
echo $msg . PHP_EOL;
}
};
$object = new class() implements LoggerAwareInterface {
private $logger;
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function run()
{
if (null !== $this->logger) {
$this->logger->error('an error happened in {file}', array('file' => 'foo.php'));
$this->logger->notice('{reason} happened, but I work anyway', array('reason' => 'stuff'));
}
return 0;
}
};
$object->setLogger($logger);
$object->run();