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