-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathConsoleLogger.php
45 lines (40 loc) · 1.22 KB
/
ConsoleLogger.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
35
36
37
38
39
40
41
42
43
44
45
<?php
namespace Core\Logger;
use Closure;
use Core\Utils\CoreHelper;
use Psr\Log\AbstractLogger;
use Psr\Log\InvalidArgumentException;
class ConsoleLogger extends AbstractLogger
{
/**
* A callable function that takes in a format and any number of parameters to satisfy that format.
* For example: "printf", will be called like printf('%s %s', 'a', 'b')
*
* @var callable
*/
private $printer;
public function __construct(callable $printer)
{
$this->printer = $printer;
}
/**
* @inheritDoc
*/
public function log($level, $message, array $context = []): void
{
if (!in_array($level, LoggerConstants::ALLOWED_LOG_LEVELS, true)) {
throw new InvalidArgumentException(
"Invalid LogLevel: $level. See Psr\Log\LogLevel.php for possible values of log levels."
);
}
Closure::fromCallable($this->printer)("%s: %s\n", $level, str_replace(
array_map(function ($key) {
return '{' . $key . '}';
}, array_keys($context)),
array_map(function ($value) {
return CoreHelper::serialize($value);
}, $context),
$message
));
}
}