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