@warlock.js/logger 4.0.39 → 4.0.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/index.js +689 -2
- package/cjs/index.js.map +1 -1
- package/esm/index.js +673 -2
- package/esm/index.js.map +1 -1
- package/package.json +1 -1
package/esm/index.js
CHANGED
|
@@ -1,3 +1,674 @@
|
|
|
1
|
-
import {colors
|
|
2
|
-
|
|
1
|
+
import { colors } from '@mongez/copper';
|
|
2
|
+
import { ensureDirectoryAsync, fileExistsAsync, getJsonFileAsync, putJsonFileAsync } from '@mongez/fs';
|
|
3
|
+
import dayjs from 'dayjs';
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
import { EOL } from 'os';
|
|
6
|
+
import path from 'path';
|
|
7
|
+
import { Random } from '@mongez/reinforcements';
|
|
8
|
+
|
|
9
|
+
// ../../warlock.js/logger/src/channels/console-log.ts
|
|
10
|
+
|
|
11
|
+
// ../../warlock.js/logger/src/log-channel.ts
|
|
12
|
+
var LogChannel = class {
|
|
13
|
+
/**
|
|
14
|
+
* Channel name
|
|
15
|
+
*/
|
|
16
|
+
name;
|
|
17
|
+
/**
|
|
18
|
+
* Channel description
|
|
19
|
+
*/
|
|
20
|
+
description;
|
|
21
|
+
/**
|
|
22
|
+
* Determine if channel is logging in terminal
|
|
23
|
+
*/
|
|
24
|
+
terminal = false;
|
|
25
|
+
/**
|
|
26
|
+
* Default Configurations
|
|
27
|
+
*/
|
|
28
|
+
defaultConfigurations = {};
|
|
29
|
+
/**
|
|
30
|
+
* Channel configurations
|
|
31
|
+
*/
|
|
32
|
+
channelConfigurations = {};
|
|
33
|
+
//
|
|
34
|
+
/**
|
|
35
|
+
* Determine whether the channel is fully initialized
|
|
36
|
+
*/
|
|
37
|
+
isInitialized = false;
|
|
38
|
+
/**
|
|
39
|
+
* Constructor
|
|
40
|
+
*/
|
|
41
|
+
constructor(configurations) {
|
|
42
|
+
if (configurations) {
|
|
43
|
+
this.setConfigurations(configurations);
|
|
44
|
+
}
|
|
45
|
+
setTimeout(async () => {
|
|
46
|
+
if (this.init) {
|
|
47
|
+
await this.init();
|
|
48
|
+
}
|
|
49
|
+
this.isInitialized = true;
|
|
50
|
+
}, 0);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Get config value
|
|
54
|
+
*/
|
|
55
|
+
config(key) {
|
|
56
|
+
return this.channelConfigurations[key] ?? (this.defaultConfigurations ?? {})[key];
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Set configurations
|
|
60
|
+
*/
|
|
61
|
+
setConfigurations(configurations) {
|
|
62
|
+
this.channelConfigurations = {
|
|
63
|
+
...this.channelConfigurations,
|
|
64
|
+
...configurations
|
|
65
|
+
};
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Determine if the message should be logged
|
|
70
|
+
*/
|
|
71
|
+
shouldBeLogged(data) {
|
|
72
|
+
const allowedLevels = this.config("levels");
|
|
73
|
+
if (allowedLevels?.length && !allowedLevels.includes(data.type))
|
|
74
|
+
return false;
|
|
75
|
+
const filter = this.config("filter");
|
|
76
|
+
if (filter) {
|
|
77
|
+
return filter(data);
|
|
78
|
+
}
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Get date and time formats
|
|
83
|
+
*/
|
|
84
|
+
getDateAndTimeFormat() {
|
|
85
|
+
const dateFormat = this.config("dateFormat");
|
|
86
|
+
const date = dateFormat?.date ?? "DD-MM-YYYY";
|
|
87
|
+
const time = dateFormat?.time ?? "HH:mm:ss";
|
|
88
|
+
return { date, time };
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* get basic configurations with the given ones
|
|
92
|
+
*/
|
|
93
|
+
withBasicConfigurations(configurations) {
|
|
94
|
+
return {
|
|
95
|
+
filter: () => true,
|
|
96
|
+
...configurations
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// ../../warlock.js/logger/src/channels/console-log.ts
|
|
102
|
+
var ConsoleLog = class extends LogChannel {
|
|
103
|
+
/**
|
|
104
|
+
* {@inheritdoc}
|
|
105
|
+
*/
|
|
106
|
+
name = "console";
|
|
107
|
+
/**
|
|
108
|
+
* Determine if channel is logging in terminal
|
|
109
|
+
*/
|
|
110
|
+
terminal = true;
|
|
111
|
+
/**
|
|
112
|
+
* {@inheritdoc}
|
|
113
|
+
*/
|
|
114
|
+
log(data) {
|
|
115
|
+
const { module, action, message, type: level } = data;
|
|
116
|
+
if (!this.shouldBeLogged(data)) return;
|
|
117
|
+
const date = (/* @__PURE__ */ new Date()).toISOString();
|
|
118
|
+
switch (level) {
|
|
119
|
+
case "debug":
|
|
120
|
+
console.log(
|
|
121
|
+
colors.magentaBright("\u2699"),
|
|
122
|
+
colors.yellow(`(${date})`),
|
|
123
|
+
colors.cyan(`[${module}]`),
|
|
124
|
+
colors.magenta(`[${action}]`),
|
|
125
|
+
colors.magentaBright(message)
|
|
126
|
+
);
|
|
127
|
+
break;
|
|
128
|
+
case "info":
|
|
129
|
+
console.log(
|
|
130
|
+
colors.blueBright("\u2139"),
|
|
131
|
+
colors.yellow(`(${date})`),
|
|
132
|
+
colors.cyan(`[${module}]`),
|
|
133
|
+
colors.magenta(`[${action}]`),
|
|
134
|
+
colors.blueBright(message)
|
|
135
|
+
);
|
|
136
|
+
break;
|
|
137
|
+
case "warn":
|
|
138
|
+
console.log(
|
|
139
|
+
colors.yellow("\u26A0"),
|
|
140
|
+
colors.yellow(`(${date})`),
|
|
141
|
+
colors.cyan(`[${module}]`),
|
|
142
|
+
colors.magenta(`[${action}]`),
|
|
143
|
+
colors.yellowBright(message)
|
|
144
|
+
);
|
|
145
|
+
break;
|
|
146
|
+
case "error":
|
|
147
|
+
console.log(
|
|
148
|
+
colors.red("\u2717"),
|
|
149
|
+
colors.yellow(`(${date})`),
|
|
150
|
+
colors.cyan(`[${module}]`),
|
|
151
|
+
colors.magenta(`[${action}]`),
|
|
152
|
+
colors.redBright(message)
|
|
153
|
+
);
|
|
154
|
+
break;
|
|
155
|
+
case "success":
|
|
156
|
+
console.log(
|
|
157
|
+
colors.green("\u2713"),
|
|
158
|
+
colors.yellow(`(${date})`),
|
|
159
|
+
colors.cyan(`[${module}]`),
|
|
160
|
+
colors.magenta(`[${action}]`),
|
|
161
|
+
colors.greenBright(message)
|
|
162
|
+
);
|
|
163
|
+
break;
|
|
164
|
+
default:
|
|
165
|
+
console.log(
|
|
166
|
+
"[log]",
|
|
167
|
+
colors.yellow(`(${date})`),
|
|
168
|
+
colors.cyan(`[${module}]`),
|
|
169
|
+
colors.magenta(`[${action}]`),
|
|
170
|
+
message
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
if (typeof message === "object") {
|
|
174
|
+
console.log(message);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
var FileLog = class extends LogChannel {
|
|
179
|
+
/**
|
|
180
|
+
* {@inheritdoc}
|
|
181
|
+
*/
|
|
182
|
+
name = "file";
|
|
183
|
+
/**
|
|
184
|
+
* Messages buffer
|
|
185
|
+
*/
|
|
186
|
+
messages = [];
|
|
187
|
+
/**
|
|
188
|
+
* Grouped messages
|
|
189
|
+
*/
|
|
190
|
+
groupedMessages = {};
|
|
191
|
+
/**
|
|
192
|
+
* Default channel configurations
|
|
193
|
+
*/
|
|
194
|
+
defaultConfigurations = {
|
|
195
|
+
storagePath: process.cwd() + "/storage/logs",
|
|
196
|
+
rotate: true,
|
|
197
|
+
name: "app",
|
|
198
|
+
extension: "log",
|
|
199
|
+
chunk: "single",
|
|
200
|
+
maxMessagesToWrite: 100,
|
|
201
|
+
filter: () => true,
|
|
202
|
+
maxFileSize: 10 * 1024 * 1024,
|
|
203
|
+
// 10MB
|
|
204
|
+
get rotateFileName() {
|
|
205
|
+
return dayjs().format("DD-MM-YYYY");
|
|
206
|
+
},
|
|
207
|
+
dateFormat: {
|
|
208
|
+
date: "DD-MM-YYYY",
|
|
209
|
+
time: "HH:mm:ss"
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
/**
|
|
213
|
+
* Last write time
|
|
214
|
+
*/
|
|
215
|
+
lastWriteTime = Date.now();
|
|
216
|
+
/**
|
|
217
|
+
* A flag to determine if the file is being written
|
|
218
|
+
*/
|
|
219
|
+
isWriting = false;
|
|
220
|
+
/**
|
|
221
|
+
* Check file size for file rotation
|
|
222
|
+
*/
|
|
223
|
+
async checkAndRotateFile(filePath = this.filePath) {
|
|
224
|
+
if (!this.config("rotate")) return;
|
|
225
|
+
try {
|
|
226
|
+
const stats = await fs.promises.stat(filePath);
|
|
227
|
+
if (stats.size >= this.config("maxFileSize")) {
|
|
228
|
+
await this.rotateLogFile();
|
|
229
|
+
}
|
|
230
|
+
} catch (error) {
|
|
231
|
+
if (error.code === "ENOENT") {
|
|
232
|
+
console.log("Log file does not exist, will be created on first write.");
|
|
233
|
+
} else {
|
|
234
|
+
console.error("Error checking log file:", error);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Rotate log file
|
|
240
|
+
*/
|
|
241
|
+
async rotateLogFile() {
|
|
242
|
+
const fileName = `${this.fileName}-${this.config(
|
|
243
|
+
"rotateFileName"
|
|
244
|
+
)}-${Date.now()}`;
|
|
245
|
+
const extension = this.extension;
|
|
246
|
+
const rotatedFilePath = path.join(
|
|
247
|
+
this.storagePath,
|
|
248
|
+
`${fileName}.${extension}`
|
|
249
|
+
);
|
|
250
|
+
await fs.promises.rename(this.filePath, rotatedFilePath).catch((error) => {
|
|
251
|
+
console.error("Error rotating file:", error);
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Flush messages
|
|
256
|
+
*/
|
|
257
|
+
initMessageFlush() {
|
|
258
|
+
setInterval(() => {
|
|
259
|
+
if (this.messages.length > 0 && (this.messages.length >= this.maxMessagesToWrite || Date.now() - this.lastWriteTime > 5e3)) {
|
|
260
|
+
this.writeMessagesToFile();
|
|
261
|
+
}
|
|
262
|
+
}, 5e3);
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Get file path
|
|
266
|
+
*/
|
|
267
|
+
get filePath() {
|
|
268
|
+
const fileName = this.fileName;
|
|
269
|
+
const extension = this.extension;
|
|
270
|
+
return path.join(this.storagePath, `${fileName}.${extension}`);
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Get max messages
|
|
274
|
+
*/
|
|
275
|
+
get maxMessagesToWrite() {
|
|
276
|
+
return this.config("maxMessagesToWrite");
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Get file name
|
|
280
|
+
*/
|
|
281
|
+
get fileName() {
|
|
282
|
+
const debugLevel = this.config("chunk");
|
|
283
|
+
switch (debugLevel) {
|
|
284
|
+
case "single":
|
|
285
|
+
default:
|
|
286
|
+
return this.config("name");
|
|
287
|
+
case "daily":
|
|
288
|
+
return dayjs().format("DD-MM-YYYY");
|
|
289
|
+
case "hourly":
|
|
290
|
+
return dayjs().format("DD-MM-YYYY-HH");
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Get file extension
|
|
295
|
+
*/
|
|
296
|
+
get extension() {
|
|
297
|
+
return this.config("extension");
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Get content
|
|
301
|
+
*/
|
|
302
|
+
get content() {
|
|
303
|
+
return this.messages.map((message) => message.content).join(EOL) + EOL;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Get storage path
|
|
307
|
+
*/
|
|
308
|
+
get storagePath() {
|
|
309
|
+
return this.config("storagePath");
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* {@inheritdoc}
|
|
313
|
+
*/
|
|
314
|
+
async init() {
|
|
315
|
+
const logsDirectory = this.storagePath;
|
|
316
|
+
await ensureDirectoryAsync(logsDirectory);
|
|
317
|
+
this.initMessageFlush();
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* {@inheritdoc}
|
|
321
|
+
*/
|
|
322
|
+
async log(data) {
|
|
323
|
+
const { module, action, message, type: level } = data;
|
|
324
|
+
if (!this.shouldBeLogged(data)) return;
|
|
325
|
+
const { date: dateFormat, time } = this.getDateAndTimeFormat();
|
|
326
|
+
const date = dayjs().format(dateFormat + " " + time);
|
|
327
|
+
let content = `[${date}] [${level}] [${module}][${action}]: `;
|
|
328
|
+
let stack;
|
|
329
|
+
if (message instanceof Error) {
|
|
330
|
+
content += message.message + EOL;
|
|
331
|
+
content += `[trace]` + EOL;
|
|
332
|
+
content += message.stack;
|
|
333
|
+
stack = message.stack;
|
|
334
|
+
} else {
|
|
335
|
+
content += message;
|
|
336
|
+
}
|
|
337
|
+
this.messages.push({
|
|
338
|
+
content,
|
|
339
|
+
level,
|
|
340
|
+
date,
|
|
341
|
+
module,
|
|
342
|
+
action,
|
|
343
|
+
stack
|
|
344
|
+
});
|
|
345
|
+
await this.checkIfMessagesShouldBeWritten();
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Check if messages should be written
|
|
349
|
+
*/
|
|
350
|
+
async checkIfMessagesShouldBeWritten() {
|
|
351
|
+
if (this.messages.length >= this.maxMessagesToWrite || Date.now() - this.lastWriteTime > 5e3) {
|
|
352
|
+
await this.writeMessagesToFile();
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Should be called after messages are saved
|
|
357
|
+
*/
|
|
358
|
+
onSave() {
|
|
359
|
+
this.messages = [];
|
|
360
|
+
this.groupedMessages = {};
|
|
361
|
+
this.isWriting = false;
|
|
362
|
+
this.lastWriteTime = Date.now();
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Check if messages should be grouped
|
|
366
|
+
*/
|
|
367
|
+
get messagedShouldBeGrouped() {
|
|
368
|
+
return Number(this.config("groupBy")?.length) > 0;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Write messages to the file
|
|
372
|
+
*/
|
|
373
|
+
async writeMessagesToFile() {
|
|
374
|
+
if (this.messages.length === 0 || this.isWriting || !this.isInitialized)
|
|
375
|
+
return;
|
|
376
|
+
this.isWriting = true;
|
|
377
|
+
if (this.messagedShouldBeGrouped) {
|
|
378
|
+
return await this.writeGroupedMessagesToFile();
|
|
379
|
+
}
|
|
380
|
+
await this.checkAndRotateFile();
|
|
381
|
+
try {
|
|
382
|
+
await this.write(this.filePath, this.content);
|
|
383
|
+
this.onSave();
|
|
384
|
+
} catch (error) {
|
|
385
|
+
console.error("Failed to write log:", error);
|
|
386
|
+
this.isWriting = false;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Write grouped messages to the file
|
|
391
|
+
*/
|
|
392
|
+
async writeGroupedMessagesToFile() {
|
|
393
|
+
this.prepareGroupedMessages();
|
|
394
|
+
for (const key in this.groupedMessages) {
|
|
395
|
+
const directoryPath = path.join(this.storagePath, key);
|
|
396
|
+
await ensureDirectoryAsync(directoryPath);
|
|
397
|
+
const filePath = path.join(
|
|
398
|
+
directoryPath,
|
|
399
|
+
`${this.fileName}.${this.extension}`
|
|
400
|
+
);
|
|
401
|
+
await this.checkAndRotateFile(filePath);
|
|
402
|
+
const content = this.groupedMessages[key].map((message) => message.content).join(EOL) + EOL;
|
|
403
|
+
try {
|
|
404
|
+
await this.write(filePath, content);
|
|
405
|
+
} catch (error) {
|
|
406
|
+
console.error("Failed to write log:", error);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
this.onSave();
|
|
410
|
+
this.isWriting = false;
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Prepare grouped messages
|
|
414
|
+
*/
|
|
415
|
+
prepareGroupedMessages() {
|
|
416
|
+
this.messages.forEach((message) => {
|
|
417
|
+
const key = this.config("groupBy").map((groupKey) => encodeURIComponent(message[groupKey])).join("/");
|
|
418
|
+
this.groupedMessages[key] = this.groupedMessages[key] || [];
|
|
419
|
+
this.groupedMessages[key].push(message);
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Start writing to the file
|
|
424
|
+
*/
|
|
425
|
+
async write(filePath, content) {
|
|
426
|
+
return new Promise((resolve, reject) => {
|
|
427
|
+
const writer = fs.createWriteStream(filePath, { flags: "a" });
|
|
428
|
+
writer.write(content, (error) => {
|
|
429
|
+
writer.end();
|
|
430
|
+
if (error) {
|
|
431
|
+
reject(error);
|
|
432
|
+
} else {
|
|
433
|
+
resolve(true);
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
var JSONFileLog = class extends FileLog {
|
|
440
|
+
/**
|
|
441
|
+
* {@inheritdoc}
|
|
442
|
+
*/
|
|
443
|
+
name = "fileJson";
|
|
444
|
+
/**
|
|
445
|
+
* Get file extension
|
|
446
|
+
*/
|
|
447
|
+
get extension() {
|
|
448
|
+
return "json";
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Get initial file contents
|
|
452
|
+
*/
|
|
453
|
+
get initialFileContents() {
|
|
454
|
+
return {
|
|
455
|
+
messages: this.messages
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* {@inheritdoc}
|
|
460
|
+
*/
|
|
461
|
+
async log(data) {
|
|
462
|
+
let stack;
|
|
463
|
+
if (data.message instanceof Error) {
|
|
464
|
+
stack = data.message.stack?.split("\n");
|
|
465
|
+
data.message = data.message.message;
|
|
466
|
+
}
|
|
467
|
+
const { module, action, message, type: level } = data;
|
|
468
|
+
if (!this.shouldBeLogged(data)) return;
|
|
469
|
+
const { date: dateFormat, time } = this.getDateAndTimeFormat();
|
|
470
|
+
const date = dayjs().format(dateFormat + " " + time);
|
|
471
|
+
this.messages.push({
|
|
472
|
+
content: message,
|
|
473
|
+
level,
|
|
474
|
+
date,
|
|
475
|
+
module,
|
|
476
|
+
action,
|
|
477
|
+
stack
|
|
478
|
+
});
|
|
479
|
+
await this.checkIfMessagesShouldBeWritten();
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* Write messages to the file
|
|
483
|
+
*/
|
|
484
|
+
async writeMessagesToFile() {
|
|
485
|
+
if (this.messages.length === 0 || this.isWriting) return;
|
|
486
|
+
this.isWriting = true;
|
|
487
|
+
if (this.messagedShouldBeGrouped) {
|
|
488
|
+
return await this.writeGroupedMessagesToFile();
|
|
489
|
+
}
|
|
490
|
+
await this.checkAndRotateFile();
|
|
491
|
+
let fileContents;
|
|
492
|
+
if (await fileExistsAsync(this.filePath)) {
|
|
493
|
+
try {
|
|
494
|
+
fileContents = await getJsonFileAsync(this.filePath);
|
|
495
|
+
} catch (error) {
|
|
496
|
+
console.error("Error reading log file, reinitializing:", error);
|
|
497
|
+
fileContents = { messages: [] };
|
|
498
|
+
}
|
|
499
|
+
} else {
|
|
500
|
+
fileContents = { messages: [] };
|
|
501
|
+
}
|
|
502
|
+
fileContents.messages.push(...this.messages);
|
|
503
|
+
try {
|
|
504
|
+
await putJsonFileAsync(this.filePath, fileContents, { spaces: 2 });
|
|
505
|
+
this.onSave();
|
|
506
|
+
} catch (error) {
|
|
507
|
+
console.error("Failed to write log:", error);
|
|
508
|
+
this.isWriting = false;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Write grouped messages to the file
|
|
513
|
+
*/
|
|
514
|
+
async writeGroupedMessagesToFile() {
|
|
515
|
+
this.prepareGroupedMessages();
|
|
516
|
+
for (const key in this.groupedMessages) {
|
|
517
|
+
const directoryPath = path.join(this.storagePath, key);
|
|
518
|
+
await ensureDirectoryAsync(directoryPath);
|
|
519
|
+
const filePath = path.join(
|
|
520
|
+
directoryPath,
|
|
521
|
+
`${this.fileName}.${this.extension}`
|
|
522
|
+
);
|
|
523
|
+
await this.checkAndRotateFile(filePath);
|
|
524
|
+
const content = this.groupedMessages[key];
|
|
525
|
+
try {
|
|
526
|
+
await putJsonFileAsync(filePath, content, { spaces: 2 });
|
|
527
|
+
} catch (error) {
|
|
528
|
+
console.error("Failed to write log:", error);
|
|
529
|
+
this.isWriting = false;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
this.onSave();
|
|
533
|
+
}
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
// ../../warlock.js/logger/src/utils/clear-message.ts
|
|
537
|
+
function clearMessage(message) {
|
|
538
|
+
if (typeof message !== "string") return message;
|
|
539
|
+
return message.replace(/\u001b[^m]*?m/g, "");
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// ../../warlock.js/logger/src/logger.ts
|
|
543
|
+
var Logger = class {
|
|
544
|
+
/**
|
|
545
|
+
* Current channel
|
|
546
|
+
*/
|
|
547
|
+
channels = [];
|
|
548
|
+
id = "logger-" + Random.string(32);
|
|
549
|
+
/**
|
|
550
|
+
* Add a new channel
|
|
551
|
+
*/
|
|
552
|
+
addChannel(channel) {
|
|
553
|
+
this.channels.push(channel);
|
|
554
|
+
return this;
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Set base configurations
|
|
558
|
+
*/
|
|
559
|
+
configure(config) {
|
|
560
|
+
this.channels = config.channels;
|
|
561
|
+
return this;
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Set channels
|
|
565
|
+
*/
|
|
566
|
+
setChannels(channels) {
|
|
567
|
+
this.channels = channels;
|
|
568
|
+
return this;
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Normalize log data to a single object
|
|
572
|
+
*/
|
|
573
|
+
normalizeLogData(dataOrModule, action, message = "", level) {
|
|
574
|
+
if (typeof dataOrModule === "object") {
|
|
575
|
+
return {
|
|
576
|
+
type: level || dataOrModule.type || "info",
|
|
577
|
+
module: dataOrModule.module,
|
|
578
|
+
action: dataOrModule.action,
|
|
579
|
+
message: dataOrModule.message,
|
|
580
|
+
...dataOrModule.context ? { context: dataOrModule.context } : {}
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
return {
|
|
584
|
+
type: level || "info",
|
|
585
|
+
module: dataOrModule,
|
|
586
|
+
action,
|
|
587
|
+
message
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Make log
|
|
592
|
+
*/
|
|
593
|
+
async log(data) {
|
|
594
|
+
for (const channel of this.channels) {
|
|
595
|
+
if (channel.terminal === false) {
|
|
596
|
+
data.message = clearMessage(data.message);
|
|
597
|
+
}
|
|
598
|
+
channel.log(data);
|
|
599
|
+
}
|
|
600
|
+
return this;
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Make debug log
|
|
604
|
+
*/
|
|
605
|
+
debug(dataOrModule, action, message = "") {
|
|
606
|
+
const data = this.normalizeLogData(dataOrModule, action, message, "debug");
|
|
607
|
+
return this.log(data);
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Make info log
|
|
611
|
+
*/
|
|
612
|
+
info(dataOrModule, action, message = "") {
|
|
613
|
+
const data = this.normalizeLogData(dataOrModule, action, message, "info");
|
|
614
|
+
return this.log(data);
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* Make warn log
|
|
618
|
+
*/
|
|
619
|
+
warn(dataOrModule, action, message = "") {
|
|
620
|
+
const data = this.normalizeLogData(dataOrModule, action, message, "warn");
|
|
621
|
+
return this.log(data);
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* Make error log
|
|
625
|
+
*/
|
|
626
|
+
error(dataOrModule, action, message = "") {
|
|
627
|
+
const data = this.normalizeLogData(dataOrModule, action, message, "error");
|
|
628
|
+
return this.log(data);
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Make success log
|
|
632
|
+
*/
|
|
633
|
+
success(dataOrModule, action, message = "") {
|
|
634
|
+
const data = this.normalizeLogData(
|
|
635
|
+
dataOrModule,
|
|
636
|
+
action,
|
|
637
|
+
message,
|
|
638
|
+
"success"
|
|
639
|
+
);
|
|
640
|
+
return this.log(data);
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Get channel by name
|
|
644
|
+
*/
|
|
645
|
+
channel(name) {
|
|
646
|
+
return this.channels.find((channel) => channel.name === name);
|
|
647
|
+
}
|
|
648
|
+
};
|
|
649
|
+
var logger = new Logger();
|
|
650
|
+
var log = (data) => {
|
|
651
|
+
return logger.log(data);
|
|
652
|
+
};
|
|
653
|
+
log.info = logger.info.bind(logger);
|
|
654
|
+
log.debug = logger.debug.bind(logger);
|
|
655
|
+
log.warn = logger.warn.bind(logger);
|
|
656
|
+
log.error = logger.error.bind(logger);
|
|
657
|
+
log.success = logger.success.bind(logger);
|
|
658
|
+
log.channel = logger.channel.bind(logger);
|
|
659
|
+
|
|
660
|
+
// ../../warlock.js/logger/src/utils/capture-unhandled-errors.ts
|
|
661
|
+
function captureAnyUnhandledRejection() {
|
|
662
|
+
process.on("unhandledRejection", (reason, promise) => {
|
|
663
|
+
log.error("app", "unhandledRejection", reason);
|
|
664
|
+
console.log(promise);
|
|
665
|
+
});
|
|
666
|
+
process.on("uncaughtException", (error) => {
|
|
667
|
+
log.error("app", "uncaughtException", error);
|
|
668
|
+
console.log(error);
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
export { ConsoleLog, FileLog, JSONFileLog, LogChannel, Logger, captureAnyUnhandledRejection, clearMessage, log, logger };
|
|
673
|
+
//# sourceMappingURL=index.js.map
|
|
3
674
|
//# sourceMappingURL=index.js.map
|