-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathSimpleLogger.php
34 lines (29 loc) · 965 Bytes
/
SimpleLogger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
namespace Sabberworm\CSS;
use Psr\Log\AbstractLogger;
/**
* This class provides a simple logging facility.
*/
class SimpleLogger extends AbstractLogger
{
public function log($level, $message, array $context = []): void
{
echo self::interpolate($message, $context) . PHP_EOL;
}
/**
* Interpolates context values into the message placeholders.
*/
private function interpolate(string $message, array $context = []): string
{
// build a replacement array with braces around the context keys
$replace = [];
foreach ($context as $key => $val) {
// check that the value can be cast to string
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
$replace['{' . $key . '}'] = $val;
}
}
// interpolate replacement values into the message and return
return strtr($message, $replace);
}
}