<?php
use Psr\Log\AbstractLogger;
use Psr\Log\LogLevel;
// abstract from composer package psr/log
class Logger extends AbstractLogger
{
public function log($level, $message, array $context = array())
{
$msg = (new DateTime())->format(DateTime::ISO8601);
$msg .= sprintf(' [%s] %s', $level, $message) . PHP_EOL;
if (array() !== $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);
}
// write log (usually into a file)
echo $msg;
}
}
$log = new Logger();
$log->error('an error happened in {file}', array('file' => 'foo.php'));
$log->notice('{reason} happened, but I work anyway', array('reason' => 'stuff'));
$log->warning('There is something strange in {method}. Better take a look', array('method' => 'bar()'));
Result
2024-04-18T11:00:15+0200 [error] an error happened in foo.php
2024-04-18T11:00:15+0200 [notice] stuff happened, but I work anyway
2024-04-18T11:00:15+0200 [warning] There is something strange in bar(). Better take a look