@likec4/log 1.17.0 → 1.18.0

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.
@@ -0,0 +1,477 @@
1
+ const LogLevels = {
2
+ silent: Number.NEGATIVE_INFINITY,
3
+ fatal: 0,
4
+ error: 0,
5
+ warn: 1,
6
+ log: 2,
7
+ info: 3,
8
+ success: 3,
9
+ fail: 3,
10
+ ready: 3,
11
+ start: 3,
12
+ box: 3,
13
+ debug: 4,
14
+ trace: 5,
15
+ verbose: Number.POSITIVE_INFINITY
16
+ };
17
+ const LogTypes = {
18
+ // Silent
19
+ silent: {
20
+ level: -1
21
+ },
22
+ // Level 0
23
+ fatal: {
24
+ level: LogLevels.fatal
25
+ },
26
+ error: {
27
+ level: LogLevels.error
28
+ },
29
+ // Level 1
30
+ warn: {
31
+ level: LogLevels.warn
32
+ },
33
+ // Level 2
34
+ log: {
35
+ level: LogLevels.log
36
+ },
37
+ // Level 3
38
+ info: {
39
+ level: LogLevels.info
40
+ },
41
+ success: {
42
+ level: LogLevels.success
43
+ },
44
+ fail: {
45
+ level: LogLevels.fail
46
+ },
47
+ ready: {
48
+ level: LogLevels.info
49
+ },
50
+ start: {
51
+ level: LogLevels.info
52
+ },
53
+ box: {
54
+ level: LogLevels.info
55
+ },
56
+ // Level 4
57
+ debug: {
58
+ level: LogLevels.debug
59
+ },
60
+ // Level 5
61
+ trace: {
62
+ level: LogLevels.trace
63
+ },
64
+ // Verbose
65
+ verbose: {
66
+ level: LogLevels.verbose
67
+ }
68
+ };
69
+
70
+ function isObject(value) {
71
+ return value !== null && typeof value === "object";
72
+ }
73
+ function _defu(baseObject, defaults, namespace = ".", merger) {
74
+ if (!isObject(defaults)) {
75
+ return _defu(baseObject, {}, namespace);
76
+ }
77
+ const object = Object.assign({}, defaults);
78
+ for (const key in baseObject) {
79
+ if (key === "__proto__" || key === "constructor") {
80
+ continue;
81
+ }
82
+ const value = baseObject[key];
83
+ if (value === null || value === void 0) {
84
+ continue;
85
+ }
86
+ if (Array.isArray(value) && Array.isArray(object[key])) {
87
+ object[key] = [...value, ...object[key]];
88
+ } else if (isObject(value) && isObject(object[key])) {
89
+ object[key] = _defu(
90
+ value,
91
+ object[key],
92
+ (namespace ? `${namespace}.` : "") + key.toString());
93
+ } else {
94
+ object[key] = value;
95
+ }
96
+ }
97
+ return object;
98
+ }
99
+ function createDefu(merger) {
100
+ return (...arguments_) => (
101
+ // eslint-disable-next-line unicorn/no-array-reduce
102
+ arguments_.reduce((p, c) => _defu(p, c, ""), {})
103
+ );
104
+ }
105
+ const defu = createDefu();
106
+
107
+ function isPlainObject(obj) {
108
+ return Object.prototype.toString.call(obj) === "[object Object]";
109
+ }
110
+ function isLogObj(arg) {
111
+ if (!isPlainObject(arg)) {
112
+ return false;
113
+ }
114
+ if (!arg.message && !arg.args) {
115
+ return false;
116
+ }
117
+ if (arg.stack) {
118
+ return false;
119
+ }
120
+ return true;
121
+ }
122
+
123
+ let paused = false;
124
+ const queue = [];
125
+ class Consola {
126
+ constructor(options = {}) {
127
+ const types = options.types || LogTypes;
128
+ this.options = defu(
129
+ {
130
+ ...options,
131
+ defaults: { ...options.defaults },
132
+ level: _normalizeLogLevel(options.level, types),
133
+ reporters: [...options.reporters || []]
134
+ },
135
+ {
136
+ types: LogTypes,
137
+ throttle: 1e3,
138
+ throttleMin: 5,
139
+ formatOptions: {
140
+ date: true,
141
+ colors: false,
142
+ compact: true
143
+ }
144
+ }
145
+ );
146
+ for (const type in types) {
147
+ const defaults = {
148
+ type,
149
+ ...this.options.defaults,
150
+ ...types[type]
151
+ };
152
+ this[type] = this._wrapLogFn(defaults);
153
+ this[type].raw = this._wrapLogFn(
154
+ defaults,
155
+ true
156
+ );
157
+ }
158
+ if (this.options.mockFn) {
159
+ this.mockTypes();
160
+ }
161
+ this._lastLog = {};
162
+ }
163
+ get level() {
164
+ return this.options.level;
165
+ }
166
+ set level(level) {
167
+ this.options.level = _normalizeLogLevel(
168
+ level,
169
+ this.options.types,
170
+ this.options.level
171
+ );
172
+ }
173
+ prompt(message, opts) {
174
+ if (!this.options.prompt) {
175
+ throw new Error("prompt is not supported!");
176
+ }
177
+ return this.options.prompt(message, opts);
178
+ }
179
+ create(options) {
180
+ const instance = new Consola({
181
+ ...this.options,
182
+ ...options
183
+ });
184
+ if (this._mockFn) {
185
+ instance.mockTypes(this._mockFn);
186
+ }
187
+ return instance;
188
+ }
189
+ withDefaults(defaults) {
190
+ return this.create({
191
+ ...this.options,
192
+ defaults: {
193
+ ...this.options.defaults,
194
+ ...defaults
195
+ }
196
+ });
197
+ }
198
+ withTag(tag) {
199
+ return this.withDefaults({
200
+ tag: this.options.defaults.tag ? this.options.defaults.tag + ":" + tag : tag
201
+ });
202
+ }
203
+ addReporter(reporter) {
204
+ this.options.reporters.push(reporter);
205
+ return this;
206
+ }
207
+ removeReporter(reporter) {
208
+ if (reporter) {
209
+ const i = this.options.reporters.indexOf(reporter);
210
+ if (i >= 0) {
211
+ return this.options.reporters.splice(i, 1);
212
+ }
213
+ } else {
214
+ this.options.reporters.splice(0);
215
+ }
216
+ return this;
217
+ }
218
+ setReporters(reporters) {
219
+ this.options.reporters = Array.isArray(reporters) ? reporters : [reporters];
220
+ return this;
221
+ }
222
+ wrapAll() {
223
+ this.wrapConsole();
224
+ this.wrapStd();
225
+ }
226
+ restoreAll() {
227
+ this.restoreConsole();
228
+ this.restoreStd();
229
+ }
230
+ wrapConsole() {
231
+ for (const type in this.options.types) {
232
+ if (!console["__" + type]) {
233
+ console["__" + type] = console[type];
234
+ }
235
+ console[type] = this[type].raw;
236
+ }
237
+ }
238
+ restoreConsole() {
239
+ for (const type in this.options.types) {
240
+ if (console["__" + type]) {
241
+ console[type] = console["__" + type];
242
+ delete console["__" + type];
243
+ }
244
+ }
245
+ }
246
+ wrapStd() {
247
+ this._wrapStream(this.options.stdout, "log");
248
+ this._wrapStream(this.options.stderr, "log");
249
+ }
250
+ _wrapStream(stream, type) {
251
+ if (!stream) {
252
+ return;
253
+ }
254
+ if (!stream.__write) {
255
+ stream.__write = stream.write;
256
+ }
257
+ stream.write = (data) => {
258
+ this[type].raw(String(data).trim());
259
+ };
260
+ }
261
+ restoreStd() {
262
+ this._restoreStream(this.options.stdout);
263
+ this._restoreStream(this.options.stderr);
264
+ }
265
+ _restoreStream(stream) {
266
+ if (!stream) {
267
+ return;
268
+ }
269
+ if (stream.__write) {
270
+ stream.write = stream.__write;
271
+ delete stream.__write;
272
+ }
273
+ }
274
+ pauseLogs() {
275
+ paused = true;
276
+ }
277
+ resumeLogs() {
278
+ paused = false;
279
+ const _queue = queue.splice(0);
280
+ for (const item of _queue) {
281
+ item[0]._logFn(item[1], item[2]);
282
+ }
283
+ }
284
+ mockTypes(mockFn) {
285
+ const _mockFn = mockFn || this.options.mockFn;
286
+ this._mockFn = _mockFn;
287
+ if (typeof _mockFn !== "function") {
288
+ return;
289
+ }
290
+ for (const type in this.options.types) {
291
+ this[type] = _mockFn(type, this.options.types[type]) || this[type];
292
+ this[type].raw = this[type];
293
+ }
294
+ }
295
+ _wrapLogFn(defaults, isRaw) {
296
+ return (...args) => {
297
+ if (paused) {
298
+ queue.push([this, defaults, args, isRaw]);
299
+ return;
300
+ }
301
+ return this._logFn(defaults, args, isRaw);
302
+ };
303
+ }
304
+ _logFn(defaults, args, isRaw) {
305
+ if ((defaults.level || 0) > this.level) {
306
+ return false;
307
+ }
308
+ const logObj = {
309
+ date: /* @__PURE__ */ new Date(),
310
+ args: [],
311
+ ...defaults,
312
+ level: _normalizeLogLevel(defaults.level, this.options.types)
313
+ };
314
+ if (!isRaw && args.length === 1 && isLogObj(args[0])) {
315
+ Object.assign(logObj, args[0]);
316
+ } else {
317
+ logObj.args = [...args];
318
+ }
319
+ if (logObj.message) {
320
+ logObj.args.unshift(logObj.message);
321
+ delete logObj.message;
322
+ }
323
+ if (logObj.additional) {
324
+ if (!Array.isArray(logObj.additional)) {
325
+ logObj.additional = logObj.additional.split("\n");
326
+ }
327
+ logObj.args.push("\n" + logObj.additional.join("\n"));
328
+ delete logObj.additional;
329
+ }
330
+ logObj.type = typeof logObj.type === "string" ? logObj.type.toLowerCase() : "log";
331
+ logObj.tag = typeof logObj.tag === "string" ? logObj.tag : "";
332
+ const resolveLog = (newLog = false) => {
333
+ const repeated = (this._lastLog.count || 0) - this.options.throttleMin;
334
+ if (this._lastLog.object && repeated > 0) {
335
+ const args2 = [...this._lastLog.object.args];
336
+ if (repeated > 1) {
337
+ args2.push(`(repeated ${repeated} times)`);
338
+ }
339
+ this._log({ ...this._lastLog.object, args: args2 });
340
+ this._lastLog.count = 1;
341
+ }
342
+ if (newLog) {
343
+ this._lastLog.object = logObj;
344
+ this._log(logObj);
345
+ }
346
+ };
347
+ clearTimeout(this._lastLog.timeout);
348
+ const diffTime = this._lastLog.time && logObj.date ? logObj.date.getTime() - this._lastLog.time.getTime() : 0;
349
+ this._lastLog.time = logObj.date;
350
+ if (diffTime < this.options.throttle) {
351
+ try {
352
+ const serializedLog = JSON.stringify([
353
+ logObj.type,
354
+ logObj.tag,
355
+ logObj.args
356
+ ]);
357
+ const isSameLog = this._lastLog.serialized === serializedLog;
358
+ this._lastLog.serialized = serializedLog;
359
+ if (isSameLog) {
360
+ this._lastLog.count = (this._lastLog.count || 0) + 1;
361
+ if (this._lastLog.count > this.options.throttleMin) {
362
+ this._lastLog.timeout = setTimeout(
363
+ resolveLog,
364
+ this.options.throttle
365
+ );
366
+ return;
367
+ }
368
+ }
369
+ } catch {
370
+ }
371
+ }
372
+ resolveLog(true);
373
+ }
374
+ _log(logObj) {
375
+ for (const reporter of this.options.reporters) {
376
+ reporter.log(logObj, {
377
+ options: this.options
378
+ });
379
+ }
380
+ }
381
+ }
382
+ function _normalizeLogLevel(input, types = {}, defaultLevel = 3) {
383
+ if (input === void 0) {
384
+ return defaultLevel;
385
+ }
386
+ if (typeof input === "number") {
387
+ return input;
388
+ }
389
+ if (types[input] && types[input].level !== void 0) {
390
+ return types[input].level;
391
+ }
392
+ return defaultLevel;
393
+ }
394
+ Consola.prototype.add = Consola.prototype.addReporter;
395
+ Consola.prototype.remove = Consola.prototype.removeReporter;
396
+ Consola.prototype.clear = Consola.prototype.removeReporter;
397
+ Consola.prototype.withScope = Consola.prototype.withTag;
398
+ Consola.prototype.mock = Consola.prototype.mockTypes;
399
+ Consola.prototype.pause = Consola.prototype.pauseLogs;
400
+ Consola.prototype.resume = Consola.prototype.resumeLogs;
401
+ function createConsola$1(options = {}) {
402
+ return new Consola(options);
403
+ }
404
+
405
+ class BrowserReporter {
406
+ constructor(options) {
407
+ this.options = { ...options };
408
+ this.defaultColor = "#7f8c8d";
409
+ this.levelColorMap = {
410
+ 0: "#c0392b",
411
+ // Red
412
+ 1: "#f39c12",
413
+ // Yellow
414
+ 3: "#00BCD4"
415
+ // Cyan
416
+ };
417
+ this.typeColorMap = {
418
+ success: "#2ecc71"
419
+ // Green
420
+ };
421
+ }
422
+ _getLogFn(level) {
423
+ if (level < 1) {
424
+ return console.__error || console.error;
425
+ }
426
+ if (level === 1) {
427
+ return console.__warn || console.warn;
428
+ }
429
+ return console.__log || console.log;
430
+ }
431
+ log(logObj) {
432
+ const consoleLogFn = this._getLogFn(logObj.level);
433
+ const type = logObj.type === "log" ? "" : logObj.type;
434
+ const tag = logObj.tag || "";
435
+ const color = this.typeColorMap[logObj.type] || this.levelColorMap[logObj.level] || this.defaultColor;
436
+ const style = `
437
+ background: ${color};
438
+ border-radius: 0.5em;
439
+ color: white;
440
+ font-weight: bold;
441
+ padding: 2px 0.5em;
442
+ `;
443
+ const badge = `%c${[tag, type].filter(Boolean).join(":")}`;
444
+ if (typeof logObj.args[0] === "string") {
445
+ consoleLogFn(
446
+ `${badge}%c ${logObj.args[0]}`,
447
+ style,
448
+ // Empty string as style resets to default console style
449
+ "",
450
+ ...logObj.args.slice(1)
451
+ );
452
+ } else {
453
+ consoleLogFn(badge, style, ...logObj.args);
454
+ }
455
+ }
456
+ }
457
+
458
+ function createConsola(options = {}) {
459
+ const consola2 = createConsola$1({
460
+ reporters: options.reporters || [new BrowserReporter({})],
461
+ prompt(message, options2 = {}) {
462
+ if (options2.type === "confirm") {
463
+ return Promise.resolve(confirm(message));
464
+ }
465
+ return Promise.resolve(prompt(message));
466
+ },
467
+ ...options
468
+ });
469
+ return consola2;
470
+ }
471
+ createConsola();
472
+
473
+ const consola = createConsola({
474
+ level: LogLevels.debug
475
+ });
476
+
477
+ export { LogLevels, consola, consola as logger, consola as rootLogger };