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