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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
<?php
declare(strict_types=1);
interface Logger
{
public const DEBUG = 10;
public const INFO = 20;
public const WARNING = 30;
public const ERROR = 40;
public const LEVEL_NAMES = [
self::DEBUG => 'DEBUG',
self::INFO => 'INFO',
self::WARNING => 'WARNING',
self::ERROR => 'ERROR',
];
public function debug(string $message, array $context = []);
public function info(string $message, array $context = []): void;
public function warning(string $message, array $context = []): void;
public function error(string $message, array $context = []): void;
}
final class SimpleLogger implements Logger
{
private string $name;
private array $handlers;
/**
* @param callable[] $handlers
*/
public function __construct(
string $name,
array $handlers = []
) {
$this->name = $name;
$this->handlers = $handlers;
}
public function addHandler(callable $fn)
{
$this->handlers[] = $fn;
}
public function debug(string $message, array $context = [])
{
$this->log(self::DEBUG, $message, $context);
}
public function info(string $message, array $context = []): void
{
$this->log(self::INFO, $message, $context);
}
public function warning(string $message, array $context = []): void
{
$this->log(self::WARNING, $message, $context);
}
public function error(string $message, array $context = []): void
{
$this->log(self::ERROR, $message, $context);
}
private function log(int $level, string $message, array $context = []): void
{
$ignoredMessages = [
'Format name invalid',
'Unknown format given',
'Unable to find channel',
];
foreach ($ignoredMessages as $ignoredMessage) {
if (str_starts_with($message, $ignoredMessage)) {
return;
}
}
foreach ($this->handlers as $handler) {
$handler([
'name' => $this->name,
'created_at' => now(),
'level' => $level,
'level_name' => self::LEVEL_NAMES[$level],
'message' => $message,
'context' => $context,
]);
}
}
}
final class StreamHandler
{
private string $stream;
private int $level;
public function __construct(string $stream, int $level = Logger::DEBUG)
{
$this->stream = $stream;
$this->level = $level;
}
public function __invoke(array $record)
{
if ($record['level'] < $this->level) {
return;
}
if (isset($record['context']['e'])) {
/** @var \Throwable $e */
$e = $record['context']['e'];
unset($record['context']['e']);
$record['context']['type'] = get_class($e);
$record['context']['code'] = $e->getCode();
$record['context']['message'] = sanitize_root($e->getMessage());
$record['context']['file'] = sanitize_root($e->getFile());
$record['context']['line'] = $e->getLine();
$record['context']['url'] = get_current_url();
$record['context']['trace'] = trace_to_call_points(trace_from_exception($e));
}
$context = '';
if ($record['context']) {
try {
$context = Json::encode($record['context']);
} catch (\JsonException $e) {
$record['context']['message'] = null;
$context = Json::encode($record['context']);
}
}
$text = sprintf(
"[%s] %s.%s %s %s\n",
$record['created_at']->format('Y-m-d H:i:s'),
$record['name'],
$record['level_name'],
$record['message'],
$context
);
$bytes = file_put_contents($this->stream, $text, FILE_APPEND);
}
}
final class ErrorLogHandler
{
private int $level;
public function __construct(int $level = Logger::DEBUG)
{
$this->level = $level;
}
public function __invoke(array $record)
{
if ($record['level'] < $this->level) {
return;
}
if (isset($record['context']['e'])) {
/** @var \Throwable $e */
$e = $record['context']['e'];
unset($record['context']['e']);
$record['context']['type'] = get_class($e);
$record['context']['code'] = $e->getCode();
$record['context']['message'] = sanitize_root($e->getMessage());
$record['context']['file'] = sanitize_root($e->getFile());
$record['context']['line'] = $e->getLine();
$record['context']['url'] = get_current_url();
$record['context']['trace'] = trace_to_call_points(trace_from_exception($e));
}
$context = '';
if ($record['context']) {
try {
$context = Json::encode($record['context']);
} catch (\JsonException $e) {
$record['context']['message'] = null;
$context = Json::encode($record['context']);
}
}
// Intentionally omitting newline
$text = sprintf(
'[%s] %s.%s %s %s',
$record['created_at']->format('Y-m-d H:i:s'),
$record['name'],
$record['level_name'],
$record['message'],
$context
);
error_log($text);
}
}
final class NullLogger implements Logger
{
public function debug(string $message, array $context = [])
{
}
public function info(string $message, array $context = []): void
{
}
public function warning(string $message, array $context = []): void
{
}
public function error(string $message, array $context = []): void
{
}
}
|