forked from composer-unused/composer-unused
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DebugLogger.php
63 lines (53 loc) · 1.5 KB
/
DebugLogger.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
declare(strict_types=1);
namespace Icanhazstring\Composer\Unused\Log;
use DateTimeImmutable;
use Exception;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;
use Throwable;
class DebugLogger implements LoggerInterface
{
use LoggerTrait;
/** @var LogHandlerInterface */
private $handler;
public function __construct(LogHandlerInterface $handler)
{
$this->handler = $handler;
}
/**
* @param mixed $level
* @param string $message
* @param array<string, mixed> $context
*/
public function log($level, $message, array $context = []): void
{
try {
$this->handler->handle($this->buildRecord($level, $message, $context));
} catch (Exception $e) {
// Bad luck :D
}
}
/**
* @param mixed $level
* @param array<string, mixed> $context
* @return array<string, mixed>
* @throws Exception
*/
private function buildRecord($level, string $message, array $context = []): array
{
if (array_key_exists('error', $context)) {
/** @var Throwable $error */
$error = $context['error'];
unset($context['error']);
$context['file'] = $error->getFile();
$context['line'] = $error->getLine();
}
return [
'time' => (new DateTimeImmutable())->format('Y-m-d\TH:i:s.uP'),
'level' => $level,
'message' => $message,
'context' => $context
];
}
}