1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\PSR3; |
4
|
|
|
|
5
|
|
|
use Psr\Log\InvalidArgumentException; |
6
|
|
|
use Psr\Log\LogLevel; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Logging levels PSR-3 LogLevel enum. |
10
|
|
|
* |
11
|
|
|
* @var array $levels Logging levels |
12
|
|
|
*/ |
13
|
|
|
const LOG_LEVELS = [ |
14
|
|
|
LogLevel::DEBUG => 'DEBUG', |
15
|
|
|
LogLevel::INFO => 'INFO', |
16
|
|
|
LogLevel::NOTICE => 'NOTICE', |
17
|
|
|
LogLevel::WARNING => 'WARNING', |
18
|
|
|
LogLevel::ERROR => 'ERROR', |
19
|
|
|
LogLevel::CRITICAL => 'CRITICAL', |
20
|
|
|
LogLevel::ALERT => 'ALERT', |
21
|
|
|
LogLevel::EMERGENCY => 'EMERGENCY', |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Functions in this file are a continuation of the code from this |
26
|
|
|
* https://github.com/Seldaek/monolog/blob/6e6586257d9fb231bf039563632e626cdef594e5/src/Monolog/Processor/PsrLogMessageProcessor.php file. |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $message |
31
|
|
|
* @param array $context |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
function processPlaceHolders(string $message, array $context): string |
35
|
|
|
{ |
36
|
3 |
|
if (false === \strpos($message, '{')) { |
37
|
1 |
|
return $message; |
38
|
|
|
} |
39
|
|
|
|
40
|
2 |
|
$replacements = []; |
41
|
2 |
|
foreach ($context as $key => $value) { |
42
|
2 |
|
$replacements['{'.$key.'}'] = formatValue($value); |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
return \strtr($message, $replacements); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
function formatValue($value): string |
49
|
|
|
{ |
50
|
11 |
|
if (\is_null($value) || \is_scalar($value) || (\is_object($value) && \method_exists($value, '__toString'))) { |
51
|
8 |
|
return (string)$value; |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
if (\is_object($value)) { |
55
|
2 |
|
return '[object '.\get_class($value).']'; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
return '['.\gettype($value).']'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
function normalizeContext(array $context): array |
62
|
|
|
{ |
63
|
6 |
|
foreach ($context as $index => $value) { |
64
|
5 |
|
if ($value instanceof \JsonSerializable) { |
65
|
1 |
|
$value = $value->jsonSerialize(); |
66
|
|
|
} |
67
|
|
|
|
68
|
5 |
|
if ($value instanceof \DateTimeInterface) { |
69
|
|
|
$value = \json_decode(\json_encode($value), true); |
70
|
|
|
} |
71
|
|
|
|
72
|
5 |
|
if (\is_array($value)) { |
73
|
4 |
|
$context[$index] = normalizeContext($value); |
74
|
4 |
|
continue; |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
if (\is_resource($value)) { |
78
|
2 |
|
$context[$index] = \sprintf('[resource] (%s)', \get_resource_type($value)); |
79
|
2 |
|
continue; |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
$context[$index] = formatValue($value); |
83
|
|
|
} |
84
|
|
|
|
85
|
6 |
|
return $context; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
function checkCorrectLogLevel(string $level): bool |
89
|
|
|
{ |
90
|
11 |
|
$level = \strtolower($level); |
91
|
11 |
|
$levels = LOG_LEVELS; |
92
|
11 |
|
if (!isset($levels[$level])) { |
93
|
3 |
|
throw new InvalidArgumentException( |
94
|
3 |
|
'Level "' . $level . '" is not defined, use one of: '.\implode(', ', \array_keys(LOG_LEVELS)) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
8 |
|
return true; |
99
|
|
|
} |
100
|
|
|
|