-
Notifications
You must be signed in to change notification settings - Fork 271
/
logger.ts
258 lines (235 loc) · 5.77 KB
/
logger.ts
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import {
PHPRequestErrorEvent,
UniversalPHP,
} from '@php-wasm/universal/src/lib/universal-php';
/**
* Log severity levels.
*/
export type LogSeverity = 'Debug' | 'Info' | 'Warn' | 'Error' | 'Fatal';
/**
* Log prefix.
*/
export type LogPrefix = 'Playground' | 'PHP-WASM';
/**
* A logger for Playground.
*/
export class Logger extends EventTarget {
public readonly fatalErrorEvent = 'playground-fatal-error';
/**
* Log messages
*/
private logs: string[] = [];
/**
* Whether the window events are connected.
*/
private windowConnected = false;
/**
* The length of the last PHP log.
*/
private lastPHPLogLength = 0;
/**
* The path to the error log file.
*/
private errorLogPath = '/wordpress/wp-content/debug.log';
constructor(errorLogPath?: string) {
super();
if (errorLogPath) {
this.errorLogPath = errorLogPath;
}
}
/**
* Read the WordPress debug.log file and return its content.
*
* @param UniversalPHP playground instance
* @returns string The content of the debug.log file
*/
private async getRequestPhpErrorLog(playground: UniversalPHP) {
if (!(await playground.fileExists(this.errorLogPath))) {
return '';
}
return await playground.readFileAsText(this.errorLogPath);
}
/**
* Log Windows errors.
*
* @param ErrorEvent event
*/
private logWindowError(event: ErrorEvent) {
this.log(
`${event.message} in ${event.filename} on line ${event.lineno}:${event.colno}`,
'Error'
);
}
/**
* Log unhandled promise rejections.
*
* @param PromiseRejectionEvent event
*/
private logUnhandledRejection(event: PromiseRejectionEvent) {
this.log(`${event.reason.stack}`, 'Error');
}
/**
* Register a listener for the window error events and log the data.
*/
public addWindowErrorListener() {
// Ensure that the window events are connected only once.
if (this.windowConnected) {
return;
}
if (typeof window === 'undefined') {
return;
}
window.addEventListener('error', this.logWindowError.bind(this));
window.addEventListener(
'unhandledrejection',
this.logUnhandledRejection.bind(this)
);
window.addEventListener(
'rejectionhandled',
this.logUnhandledRejection.bind(this)
);
this.windowConnected = true;
}
/**
* Register a listener for the request.end event and log the data.
* @param UniversalPHP playground instance
*/
public addPlaygroundRequestEndListener(playground: UniversalPHP) {
playground.addEventListener('request.end', async () => {
const log = await this.getRequestPhpErrorLog(playground);
if (log.length > this.lastPHPLogLength) {
this.logRaw(log.substring(this.lastPHPLogLength));
this.lastPHPLogLength = log.length;
}
});
playground.addEventListener('request.error', (event) => {
event = event as PHPRequestErrorEvent;
if (event.error) {
this.log(
`${event.error.message} ${event.error.stack}`,
'Fatal',
'PHP-WASM'
);
this.dispatchEvent(
new CustomEvent(this.fatalErrorEvent, {
detail: {
logs: this.getLogs(),
source: event.source,
},
})
);
}
});
}
/**
* Get UTC date in the PHP log format https://github.com/php/php-src/blob/master/main/main.c#L849
*
* @param date
* @returns string
*/
private formatLogDate(date: Date): string {
const formattedDate = new Intl.DateTimeFormat('en-GB', {
year: 'numeric',
month: 'short',
day: '2-digit',
timeZone: 'UTC',
})
.format(date)
.replace(/ /g, '-');
const formattedTime = new Intl.DateTimeFormat('en-GB', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false,
timeZone: 'UTC',
timeZoneName: 'short',
}).format(date);
return formattedDate + ' ' + formattedTime;
}
/**
* Format log message and severity and log it.
* @param string message
* @param LogSeverity severity
* @param string prefix
*/
public formatMessage(
message: string,
severity: LogSeverity,
prefix: string
): string {
const now = this.formatLogDate(new Date());
return `[${now}] ${prefix} ${severity}: ${message}`;
}
/**
* Log message with severity and timestamp.
* @param string message
* @param LogSeverity severity
* @param string prefix
*/
public log(
message: string,
severity?: LogSeverity,
prefix?: LogPrefix
): void {
if (severity === undefined) {
severity = 'Info';
}
const log = this.formatMessage(
message,
severity,
prefix ?? 'Playground'
);
this.logRaw(log);
}
/**
* Log message without severity and timestamp.
* @param string log
*/
public logRaw(log: string): void {
this.logs.push(log);
console.debug(log);
}
/**
* Get all logs.
* @returns string[]
*/
public getLogs(): string[] {
return this.logs;
}
}
/**
* The logger instance.
*/
export const logger: Logger = new Logger();
/**
* Collect errors from JavaScript window events like error and log them.
* @param loggerInstance The logger instance
*/
export function collectWindowErrors(loggerInstance: Logger) {
loggerInstance.addWindowErrorListener();
}
/**
* Collect PHP logs from the error_log file and log them.
* @param UniversalPHP playground instance
* @param loggerInstance The logger instance
*/
export function collectPhpLogs(
loggerInstance: Logger,
playground: UniversalPHP
) {
loggerInstance.addPlaygroundRequestEndListener(playground);
}
/**
* Add a listener for the fatal Playground errors.
* These errors include Playground errors like Asyncify errors. PHP errors won't trigger this event.
* The callback function will receive an Event object with logs in the detail property.
*
* @param loggerInstance The logger instance
* @param callback The callback function
*/
export function addFatalErrorListener(
loggerInstance: Logger,
callback: EventListenerOrEventListenerObject
) {
loggerInstance.addEventListener(loggerInstance.fatalErrorEvent, callback);
}