@likec4/log 1.9.0 → 1.10.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.
package/dist/node.d.mts CHANGED
@@ -1,6 +1,3 @@
1
- import { formatWithOptions } from 'node:util';
2
- import { sep } from 'node:path';
3
-
4
1
  type SelectOption = {
5
2
  label: string;
6
3
  value: string;
@@ -32,7 +29,9 @@ type inferPromptReturnType<T extends PromptOptions> = T extends TextOptions ? st
32
29
  declare function prompt<_ = any, __ = any, T extends PromptOptions = TextOptions>(message: string, opts?: PromptOptions): Promise<inferPromptReturnType<T>>;
33
30
 
34
31
  type LogLevel = 0 | 1 | 2 | 3 | 4 | 5 | (number & {});
32
+ declare const LogLevels: Record<LogType, number>;
35
33
  type LogType = "silent" | "fatal" | "error" | "warn" | "log" | "info" | "success" | "fail" | "ready" | "start" | "box" | "debug" | "trace" | "verbose";
34
+ declare const LogTypes: Record<LogType, Partial<LogObject>>;
36
35
 
37
36
  interface ConsolaOptions {
38
37
  reporters: ConsolaReporter[];
@@ -80,7 +79,7 @@ interface ConsolaReporter {
80
79
  }) => void;
81
80
  }
82
81
 
83
- declare class Consola$1 {
82
+ declare class Consola {
84
83
  options: ConsolaOptions;
85
84
  _lastLog: {
86
85
  serialized?: string;
@@ -119,492 +118,9 @@ interface LogFn {
119
118
  (message: InputLogObject | any, ...args: any[]): void;
120
119
  raw: (...args: any[]) => void;
121
120
  }
122
- type ConsolaInstance = Consola$1 & Record<LogType, LogFn>;
123
-
124
- const LogLevels = {
125
- silent: Number.NEGATIVE_INFINITY,
126
- fatal: 0,
127
- error: 0,
128
- warn: 1,
129
- log: 2,
130
- info: 3,
131
- success: 3,
132
- fail: 3,
133
- ready: 3,
134
- start: 3,
135
- box: 3,
136
- debug: 4,
137
- trace: 5,
138
- verbose: Number.POSITIVE_INFINITY
139
- };
140
- const LogTypes = {
141
- // Silent
142
- silent: {
143
- level: -1
144
- },
145
- // Level 0
146
- fatal: {
147
- level: LogLevels.fatal
148
- },
149
- error: {
150
- level: LogLevels.error
151
- },
152
- // Level 1
153
- warn: {
154
- level: LogLevels.warn
155
- },
156
- // Level 2
157
- log: {
158
- level: LogLevels.log
159
- },
160
- // Level 3
161
- info: {
162
- level: LogLevels.info
163
- },
164
- success: {
165
- level: LogLevels.success
166
- },
167
- fail: {
168
- level: LogLevels.fail
169
- },
170
- ready: {
171
- level: LogLevels.info
172
- },
173
- start: {
174
- level: LogLevels.info
175
- },
176
- box: {
177
- level: LogLevels.info
178
- },
179
- // Level 4
180
- debug: {
181
- level: LogLevels.debug
182
- },
183
- // Level 5
184
- trace: {
185
- level: LogLevels.trace
186
- },
187
- // Verbose
188
- verbose: {
189
- level: LogLevels.verbose
190
- }
191
- };
192
-
193
- function isObject(value) {
194
- return value !== null && typeof value === "object";
195
- }
196
- function _defu(baseObject, defaults, namespace = ".", merger) {
197
- if (!isObject(defaults)) {
198
- return _defu(baseObject, {}, namespace, merger);
199
- }
200
- const object = Object.assign({}, defaults);
201
- for (const key in baseObject) {
202
- if (key === "__proto__" || key === "constructor") {
203
- continue;
204
- }
205
- const value = baseObject[key];
206
- if (value === null || value === void 0) {
207
- continue;
208
- }
209
- if (merger && merger(object, key, value, namespace)) {
210
- continue;
211
- }
212
- if (Array.isArray(value) && Array.isArray(object[key])) {
213
- object[key] = [...value, ...object[key]];
214
- } else if (isObject(value) && isObject(object[key])) {
215
- object[key] = _defu(
216
- value,
217
- object[key],
218
- (namespace ? `${namespace}.` : "") + key.toString(),
219
- merger
220
- );
221
- } else {
222
- object[key] = value;
223
- }
224
- }
225
- return object;
226
- }
227
- function createDefu(merger) {
228
- return (...arguments_) => (
229
- // eslint-disable-next-line unicorn/no-array-reduce
230
- arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
231
- );
232
- }
233
- const defu = createDefu();
234
-
235
- function isPlainObject(obj) {
236
- return Object.prototype.toString.call(obj) === "[object Object]";
237
- }
238
- function isLogObj(arg) {
239
- if (!isPlainObject(arg)) {
240
- return false;
241
- }
242
- if (!arg.message && !arg.args) {
243
- return false;
244
- }
245
- if (arg.stack) {
246
- return false;
247
- }
248
- return true;
249
- }
250
-
251
- let paused = false;
252
- const queue = [];
253
- class Consola {
254
- constructor(options = {}) {
255
- const types = options.types || LogTypes;
256
- this.options = defu(
257
- {
258
- ...options,
259
- defaults: { ...options.defaults },
260
- level: _normalizeLogLevel(options.level, types),
261
- reporters: [...options.reporters || []]
262
- },
263
- {
264
- types: LogTypes,
265
- throttle: 1e3,
266
- throttleMin: 5,
267
- formatOptions: {
268
- date: true,
269
- colors: false,
270
- compact: true
271
- }
272
- }
273
- );
274
- for (const type in types) {
275
- const defaults = {
276
- type,
277
- ...this.options.defaults,
278
- ...types[type]
279
- };
280
- this[type] = this._wrapLogFn(defaults);
281
- this[type].raw = this._wrapLogFn(
282
- defaults,
283
- true
284
- );
285
- }
286
- if (this.options.mockFn) {
287
- this.mockTypes();
288
- }
289
- this._lastLog = {};
290
- }
291
- get level() {
292
- return this.options.level;
293
- }
294
- set level(level) {
295
- this.options.level = _normalizeLogLevel(
296
- level,
297
- this.options.types,
298
- this.options.level
299
- );
300
- }
301
- prompt(message, opts) {
302
- if (!this.options.prompt) {
303
- throw new Error("prompt is not supported!");
304
- }
305
- return this.options.prompt(message, opts);
306
- }
307
- create(options) {
308
- const instance = new Consola({
309
- ...this.options,
310
- ...options
311
- });
312
- if (this._mockFn) {
313
- instance.mockTypes(this._mockFn);
314
- }
315
- return instance;
316
- }
317
- withDefaults(defaults) {
318
- return this.create({
319
- ...this.options,
320
- defaults: {
321
- ...this.options.defaults,
322
- ...defaults
323
- }
324
- });
325
- }
326
- withTag(tag) {
327
- return this.withDefaults({
328
- tag: this.options.defaults.tag ? this.options.defaults.tag + ":" + tag : tag
329
- });
330
- }
331
- addReporter(reporter) {
332
- this.options.reporters.push(reporter);
333
- return this;
334
- }
335
- removeReporter(reporter) {
336
- if (reporter) {
337
- const i = this.options.reporters.indexOf(reporter);
338
- if (i >= 0) {
339
- return this.options.reporters.splice(i, 1);
340
- }
341
- } else {
342
- this.options.reporters.splice(0);
343
- }
344
- return this;
345
- }
346
- setReporters(reporters) {
347
- this.options.reporters = Array.isArray(reporters) ? reporters : [reporters];
348
- return this;
349
- }
350
- wrapAll() {
351
- this.wrapConsole();
352
- this.wrapStd();
353
- }
354
- restoreAll() {
355
- this.restoreConsole();
356
- this.restoreStd();
357
- }
358
- wrapConsole() {
359
- for (const type in this.options.types) {
360
- if (!console["__" + type]) {
361
- console["__" + type] = console[type];
362
- }
363
- console[type] = this[type].raw;
364
- }
365
- }
366
- restoreConsole() {
367
- for (const type in this.options.types) {
368
- if (console["__" + type]) {
369
- console[type] = console["__" + type];
370
- delete console["__" + type];
371
- }
372
- }
373
- }
374
- wrapStd() {
375
- this._wrapStream(this.options.stdout, "log");
376
- this._wrapStream(this.options.stderr, "log");
377
- }
378
- _wrapStream(stream, type) {
379
- if (!stream) {
380
- return;
381
- }
382
- if (!stream.__write) {
383
- stream.__write = stream.write;
384
- }
385
- stream.write = (data) => {
386
- this[type].raw(String(data).trim());
387
- };
388
- }
389
- restoreStd() {
390
- this._restoreStream(this.options.stdout);
391
- this._restoreStream(this.options.stderr);
392
- }
393
- _restoreStream(stream) {
394
- if (!stream) {
395
- return;
396
- }
397
- if (stream.__write) {
398
- stream.write = stream.__write;
399
- delete stream.__write;
400
- }
401
- }
402
- pauseLogs() {
403
- paused = true;
404
- }
405
- resumeLogs() {
406
- paused = false;
407
- const _queue = queue.splice(0);
408
- for (const item of _queue) {
409
- item[0]._logFn(item[1], item[2]);
410
- }
411
- }
412
- mockTypes(mockFn) {
413
- const _mockFn = mockFn || this.options.mockFn;
414
- this._mockFn = _mockFn;
415
- if (typeof _mockFn !== "function") {
416
- return;
417
- }
418
- for (const type in this.options.types) {
419
- this[type] = _mockFn(type, this.options.types[type]) || this[type];
420
- this[type].raw = this[type];
421
- }
422
- }
423
- _wrapLogFn(defaults, isRaw) {
424
- return (...args) => {
425
- if (paused) {
426
- queue.push([this, defaults, args, isRaw]);
427
- return;
428
- }
429
- return this._logFn(defaults, args, isRaw);
430
- };
431
- }
432
- _logFn(defaults, args, isRaw) {
433
- if ((defaults.level || 0) > this.level) {
434
- return false;
435
- }
436
- const logObj = {
437
- date: /* @__PURE__ */ new Date(),
438
- args: [],
439
- ...defaults,
440
- level: _normalizeLogLevel(defaults.level, this.options.types)
441
- };
442
- if (!isRaw && args.length === 1 && isLogObj(args[0])) {
443
- Object.assign(logObj, args[0]);
444
- } else {
445
- logObj.args = [...args];
446
- }
447
- if (logObj.message) {
448
- logObj.args.unshift(logObj.message);
449
- delete logObj.message;
450
- }
451
- if (logObj.additional) {
452
- if (!Array.isArray(logObj.additional)) {
453
- logObj.additional = logObj.additional.split("\n");
454
- }
455
- logObj.args.push("\n" + logObj.additional.join("\n"));
456
- delete logObj.additional;
457
- }
458
- logObj.type = typeof logObj.type === "string" ? logObj.type.toLowerCase() : "log";
459
- logObj.tag = typeof logObj.tag === "string" ? logObj.tag : "";
460
- const resolveLog = (newLog = false) => {
461
- const repeated = (this._lastLog.count || 0) - this.options.throttleMin;
462
- if (this._lastLog.object && repeated > 0) {
463
- const args2 = [...this._lastLog.object.args];
464
- if (repeated > 1) {
465
- args2.push(`(repeated ${repeated} times)`);
466
- }
467
- this._log({ ...this._lastLog.object, args: args2 });
468
- this._lastLog.count = 1;
469
- }
470
- if (newLog) {
471
- this._lastLog.object = logObj;
472
- this._log(logObj);
473
- }
474
- };
475
- clearTimeout(this._lastLog.timeout);
476
- const diffTime = this._lastLog.time && logObj.date ? logObj.date.getTime() - this._lastLog.time.getTime() : 0;
477
- this._lastLog.time = logObj.date;
478
- if (diffTime < this.options.throttle) {
479
- try {
480
- const serializedLog = JSON.stringify([
481
- logObj.type,
482
- logObj.tag,
483
- logObj.args
484
- ]);
485
- const isSameLog = this._lastLog.serialized === serializedLog;
486
- this._lastLog.serialized = serializedLog;
487
- if (isSameLog) {
488
- this._lastLog.count = (this._lastLog.count || 0) + 1;
489
- if (this._lastLog.count > this.options.throttleMin) {
490
- this._lastLog.timeout = setTimeout(
491
- resolveLog,
492
- this.options.throttle
493
- );
494
- return;
495
- }
496
- }
497
- } catch {
498
- }
499
- }
500
- resolveLog(true);
501
- }
502
- _log(logObj) {
503
- for (const reporter of this.options.reporters) {
504
- reporter.log(logObj, {
505
- options: this.options
506
- });
507
- }
508
- }
509
- }
510
- function _normalizeLogLevel(input, types = {}, defaultLevel = 3) {
511
- if (input === void 0) {
512
- return defaultLevel;
513
- }
514
- if (typeof input === "number") {
515
- return input;
516
- }
517
- if (types[input] && types[input].level !== void 0) {
518
- return types[input].level;
519
- }
520
- return defaultLevel;
521
- }
522
- Consola.prototype.add = Consola.prototype.addReporter;
523
- Consola.prototype.remove = Consola.prototype.removeReporter;
524
- Consola.prototype.clear = Consola.prototype.removeReporter;
525
- Consola.prototype.withScope = Consola.prototype.withTag;
526
- Consola.prototype.mock = Consola.prototype.mockTypes;
527
- Consola.prototype.pause = Consola.prototype.pauseLogs;
528
- Consola.prototype.resume = Consola.prototype.resumeLogs;
529
- function createConsola$1(options = {}) {
530
- return new Consola(options);
531
- }
532
-
533
- function parseStack(stack) {
534
- const cwd = process.cwd() + sep;
535
- const lines = stack.split("\n").splice(1).map((l) => l.trim().replace("file://", "").replace(cwd, ""));
536
- return lines;
537
- }
538
-
539
- function writeStream(data, stream) {
540
- const write = stream.__write || stream.write;
541
- return write.call(stream, data);
542
- }
543
-
544
- const bracket = (x) => x ? `[${x}]` : "";
545
- class BasicReporter {
546
- formatStack(stack, opts) {
547
- return " " + parseStack(stack).join("\n ");
548
- }
549
- formatArgs(args, opts) {
550
- const _args = args.map((arg) => {
551
- if (arg && typeof arg.stack === "string") {
552
- return arg.message + "\n" + this.formatStack(arg.stack, opts);
553
- }
554
- return arg;
555
- });
556
- return formatWithOptions(opts, ..._args);
557
- }
558
- formatDate(date, opts) {
559
- return opts.date ? date.toLocaleTimeString() : "";
560
- }
561
- filterAndJoin(arr) {
562
- return arr.filter(Boolean).join(" ");
563
- }
564
- formatLogObj(logObj, opts) {
565
- const message = this.formatArgs(logObj.args, opts);
566
- if (logObj.type === "box") {
567
- return "\n" + [
568
- bracket(logObj.tag),
569
- logObj.title && logObj.title,
570
- ...message.split("\n")
571
- ].filter(Boolean).map((l) => " > " + l).join("\n") + "\n";
572
- }
573
- return this.filterAndJoin([
574
- bracket(logObj.type),
575
- bracket(logObj.tag),
576
- message
577
- ]);
578
- }
579
- log(logObj, ctx) {
580
- const line = this.formatLogObj(logObj, {
581
- columns: ctx.options.stdout.columns || 0,
582
- ...ctx.options.formatOptions
583
- });
584
- return writeStream(
585
- line + "\n",
586
- logObj.level < 2 ? ctx.options.stderr || process.stderr : ctx.options.stdout || process.stdout
587
- );
588
- }
589
- }
590
-
591
- function createConsola(options = {}) {
592
- let level = LogLevels.info;
593
- if (process.env.CONSOLA_LEVEL) {
594
- level = Number.parseInt(process.env.CONSOLA_LEVEL) ?? level;
595
- }
596
- const consola2 = createConsola$1({
597
- level,
598
- defaults: { level },
599
- stdout: process.stdout,
600
- stderr: process.stderr,
601
- reporters: options.reporters || [new BasicReporter()],
602
- ...options
603
- });
604
- return consola2;
605
- }
606
- createConsola();
121
+ type ConsolaInstance = Consola & Record<LogType, LogFn>;
122
+ declare function createConsola(options?: Partial<ConsolaOptions>): ConsolaInstance;
607
123
 
608
124
  declare const logger: ConsolaInstance;
609
125
 
610
- export { Consola, LogLevels, LogTypes, logger as consola, createConsola, logger, logger as rootLogger };
126
+ export { Consola, type ConsolaInstance, type ConsolaOptions, type ConsolaReporter, type FormatOptions, type InputLogObject, type LogLevel, LogLevels, type LogObject, type LogType, LogTypes, logger as consola, createConsola, logger, logger as rootLogger };