@likec4/log 1.8.1 → 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.
@@ -0,0 +1,126 @@
1
+ type SelectOption = {
2
+ label: string;
3
+ value: string;
4
+ hint?: string;
5
+ };
6
+ type TextOptions = {
7
+ type?: "text";
8
+ default?: string;
9
+ placeholder?: string;
10
+ initial?: string;
11
+ };
12
+ type ConfirmOptions = {
13
+ type: "confirm";
14
+ initial?: boolean;
15
+ };
16
+ type SelectOptions = {
17
+ type: "select";
18
+ initial?: string;
19
+ options: (string | SelectOption)[];
20
+ };
21
+ type MultiSelectOptions = {
22
+ type: "multiselect";
23
+ initial?: string;
24
+ options: string[] | SelectOption[];
25
+ required?: boolean;
26
+ };
27
+ type PromptOptions = TextOptions | ConfirmOptions | SelectOptions | MultiSelectOptions;
28
+ type inferPromptReturnType<T extends PromptOptions> = T extends TextOptions ? string : T extends ConfirmOptions ? boolean : T extends SelectOptions ? T["options"][number] : T extends MultiSelectOptions ? T["options"] : unknown;
29
+ declare function prompt<_ = any, __ = any, T extends PromptOptions = TextOptions>(message: string, opts?: PromptOptions): Promise<inferPromptReturnType<T>>;
30
+
31
+ type LogLevel = 0 | 1 | 2 | 3 | 4 | 5 | (number & {});
32
+ declare const LogLevels: Record<LogType, number>;
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>>;
35
+
36
+ interface ConsolaOptions {
37
+ reporters: ConsolaReporter[];
38
+ types: Record<LogType, InputLogObject>;
39
+ level: LogLevel;
40
+ defaults: InputLogObject;
41
+ throttle: number;
42
+ throttleMin: number;
43
+ stdout?: NodeJS.WriteStream;
44
+ stderr?: NodeJS.WriteStream;
45
+ mockFn?: (type: LogType, defaults: InputLogObject) => (...args: any) => void;
46
+ prompt?: typeof prompt | undefined;
47
+ formatOptions: FormatOptions;
48
+ }
49
+ /**
50
+ * @see https://nodejs.org/api/util.html#util_util_inspect_object_showhidden_depth_colors
51
+ */
52
+ interface FormatOptions {
53
+ columns?: number;
54
+ date?: boolean;
55
+ colors?: boolean;
56
+ compact?: boolean | number;
57
+ [key: string]: unknown;
58
+ }
59
+ interface InputLogObject {
60
+ level?: LogLevel;
61
+ tag?: string;
62
+ type?: LogType;
63
+ message?: string;
64
+ additional?: string | string[];
65
+ args?: any[];
66
+ date?: Date;
67
+ }
68
+ interface LogObject extends InputLogObject {
69
+ level: LogLevel;
70
+ type: LogType;
71
+ tag: string;
72
+ args: any[];
73
+ date: Date;
74
+ [key: string]: unknown;
75
+ }
76
+ interface ConsolaReporter {
77
+ log: (logObj: LogObject, ctx: {
78
+ options: ConsolaOptions;
79
+ }) => void;
80
+ }
81
+
82
+ declare class Consola {
83
+ options: ConsolaOptions;
84
+ _lastLog: {
85
+ serialized?: string;
86
+ object?: LogObject;
87
+ count?: number;
88
+ time?: Date;
89
+ timeout?: ReturnType<typeof setTimeout>;
90
+ };
91
+ _mockFn?: ConsolaOptions["mockFn"];
92
+ constructor(options?: Partial<ConsolaOptions>);
93
+ get level(): LogLevel;
94
+ set level(level: LogLevel);
95
+ prompt<T extends PromptOptions>(message: string, opts?: T): Promise<T extends TextOptions ? string : T extends ConfirmOptions ? boolean : T extends SelectOptions ? T["options"][number] : T extends MultiSelectOptions ? T["options"] : unknown>;
96
+ create(options: Partial<ConsolaOptions>): ConsolaInstance;
97
+ withDefaults(defaults: InputLogObject): ConsolaInstance;
98
+ withTag(tag: string): ConsolaInstance;
99
+ addReporter(reporter: ConsolaReporter): this;
100
+ removeReporter(reporter: ConsolaReporter): ConsolaReporter[] | this;
101
+ setReporters(reporters: ConsolaReporter[]): this;
102
+ wrapAll(): void;
103
+ restoreAll(): void;
104
+ wrapConsole(): void;
105
+ restoreConsole(): void;
106
+ wrapStd(): void;
107
+ _wrapStream(stream: NodeJS.WriteStream | undefined, type: LogType): void;
108
+ restoreStd(): void;
109
+ _restoreStream(stream?: NodeJS.WriteStream): void;
110
+ pauseLogs(): void;
111
+ resumeLogs(): void;
112
+ mockTypes(mockFn?: ConsolaOptions["mockFn"]): void;
113
+ _wrapLogFn(defaults: InputLogObject, isRaw?: boolean): (...args: any[]) => false | undefined;
114
+ _logFn(defaults: InputLogObject, args: any[], isRaw?: boolean): false | undefined;
115
+ _log(logObj: LogObject): void;
116
+ }
117
+ interface LogFn {
118
+ (message: InputLogObject | any, ...args: any[]): void;
119
+ raw: (...args: any[]) => void;
120
+ }
121
+ type ConsolaInstance = Consola & Record<LogType, LogFn>;
122
+ declare function createConsola(options?: Partial<ConsolaOptions>): ConsolaInstance;
123
+
124
+ declare const logger: ConsolaInstance;
125
+
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 };
package/dist/index.mjs ADDED
@@ -0,0 +1,414 @@
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, merger);
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 (merger && merger(object, key, value, namespace)) {
87
+ continue;
88
+ }
89
+ if (Array.isArray(value) && Array.isArray(object[key])) {
90
+ object[key] = [...value, ...object[key]];
91
+ } else if (isObject(value) && isObject(object[key])) {
92
+ object[key] = _defu(
93
+ value,
94
+ object[key],
95
+ (namespace ? `${namespace}.` : "") + key.toString(),
96
+ merger
97
+ );
98
+ } else {
99
+ object[key] = value;
100
+ }
101
+ }
102
+ return object;
103
+ }
104
+ function createDefu(merger) {
105
+ return (...arguments_) => (
106
+ // eslint-disable-next-line unicorn/no-array-reduce
107
+ arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
108
+ );
109
+ }
110
+ const defu = createDefu();
111
+
112
+ function isPlainObject(obj) {
113
+ return Object.prototype.toString.call(obj) === "[object Object]";
114
+ }
115
+ function isLogObj(arg) {
116
+ if (!isPlainObject(arg)) {
117
+ return false;
118
+ }
119
+ if (!arg.message && !arg.args) {
120
+ return false;
121
+ }
122
+ if (arg.stack) {
123
+ return false;
124
+ }
125
+ return true;
126
+ }
127
+
128
+ let paused = false;
129
+ const queue = [];
130
+ class Consola {
131
+ constructor(options = {}) {
132
+ const types = options.types || LogTypes;
133
+ this.options = defu(
134
+ {
135
+ ...options,
136
+ defaults: { ...options.defaults },
137
+ level: _normalizeLogLevel(options.level, types),
138
+ reporters: [...options.reporters || []]
139
+ },
140
+ {
141
+ types: LogTypes,
142
+ throttle: 1e3,
143
+ throttleMin: 5,
144
+ formatOptions: {
145
+ date: true,
146
+ colors: false,
147
+ compact: true
148
+ }
149
+ }
150
+ );
151
+ for (const type in types) {
152
+ const defaults = {
153
+ type,
154
+ ...this.options.defaults,
155
+ ...types[type]
156
+ };
157
+ this[type] = this._wrapLogFn(defaults);
158
+ this[type].raw = this._wrapLogFn(
159
+ defaults,
160
+ true
161
+ );
162
+ }
163
+ if (this.options.mockFn) {
164
+ this.mockTypes();
165
+ }
166
+ this._lastLog = {};
167
+ }
168
+ get level() {
169
+ return this.options.level;
170
+ }
171
+ set level(level) {
172
+ this.options.level = _normalizeLogLevel(
173
+ level,
174
+ this.options.types,
175
+ this.options.level
176
+ );
177
+ }
178
+ prompt(message, opts) {
179
+ if (!this.options.prompt) {
180
+ throw new Error("prompt is not supported!");
181
+ }
182
+ return this.options.prompt(message, opts);
183
+ }
184
+ create(options) {
185
+ const instance = new Consola({
186
+ ...this.options,
187
+ ...options
188
+ });
189
+ if (this._mockFn) {
190
+ instance.mockTypes(this._mockFn);
191
+ }
192
+ return instance;
193
+ }
194
+ withDefaults(defaults) {
195
+ return this.create({
196
+ ...this.options,
197
+ defaults: {
198
+ ...this.options.defaults,
199
+ ...defaults
200
+ }
201
+ });
202
+ }
203
+ withTag(tag) {
204
+ return this.withDefaults({
205
+ tag: this.options.defaults.tag ? this.options.defaults.tag + ":" + tag : tag
206
+ });
207
+ }
208
+ addReporter(reporter) {
209
+ this.options.reporters.push(reporter);
210
+ return this;
211
+ }
212
+ removeReporter(reporter) {
213
+ if (reporter) {
214
+ const i = this.options.reporters.indexOf(reporter);
215
+ if (i >= 0) {
216
+ return this.options.reporters.splice(i, 1);
217
+ }
218
+ } else {
219
+ this.options.reporters.splice(0);
220
+ }
221
+ return this;
222
+ }
223
+ setReporters(reporters) {
224
+ this.options.reporters = Array.isArray(reporters) ? reporters : [reporters];
225
+ return this;
226
+ }
227
+ wrapAll() {
228
+ this.wrapConsole();
229
+ this.wrapStd();
230
+ }
231
+ restoreAll() {
232
+ this.restoreConsole();
233
+ this.restoreStd();
234
+ }
235
+ wrapConsole() {
236
+ for (const type in this.options.types) {
237
+ if (!console["__" + type]) {
238
+ console["__" + type] = console[type];
239
+ }
240
+ console[type] = this[type].raw;
241
+ }
242
+ }
243
+ restoreConsole() {
244
+ for (const type in this.options.types) {
245
+ if (console["__" + type]) {
246
+ console[type] = console["__" + type];
247
+ delete console["__" + type];
248
+ }
249
+ }
250
+ }
251
+ wrapStd() {
252
+ this._wrapStream(this.options.stdout, "log");
253
+ this._wrapStream(this.options.stderr, "log");
254
+ }
255
+ _wrapStream(stream, type) {
256
+ if (!stream) {
257
+ return;
258
+ }
259
+ if (!stream.__write) {
260
+ stream.__write = stream.write;
261
+ }
262
+ stream.write = (data) => {
263
+ this[type].raw(String(data).trim());
264
+ };
265
+ }
266
+ restoreStd() {
267
+ this._restoreStream(this.options.stdout);
268
+ this._restoreStream(this.options.stderr);
269
+ }
270
+ _restoreStream(stream) {
271
+ if (!stream) {
272
+ return;
273
+ }
274
+ if (stream.__write) {
275
+ stream.write = stream.__write;
276
+ delete stream.__write;
277
+ }
278
+ }
279
+ pauseLogs() {
280
+ paused = true;
281
+ }
282
+ resumeLogs() {
283
+ paused = false;
284
+ const _queue = queue.splice(0);
285
+ for (const item of _queue) {
286
+ item[0]._logFn(item[1], item[2]);
287
+ }
288
+ }
289
+ mockTypes(mockFn) {
290
+ const _mockFn = mockFn || this.options.mockFn;
291
+ this._mockFn = _mockFn;
292
+ if (typeof _mockFn !== "function") {
293
+ return;
294
+ }
295
+ for (const type in this.options.types) {
296
+ this[type] = _mockFn(type, this.options.types[type]) || this[type];
297
+ this[type].raw = this[type];
298
+ }
299
+ }
300
+ _wrapLogFn(defaults, isRaw) {
301
+ return (...args) => {
302
+ if (paused) {
303
+ queue.push([this, defaults, args, isRaw]);
304
+ return;
305
+ }
306
+ return this._logFn(defaults, args, isRaw);
307
+ };
308
+ }
309
+ _logFn(defaults, args, isRaw) {
310
+ if ((defaults.level || 0) > this.level) {
311
+ return false;
312
+ }
313
+ const logObj = {
314
+ date: /* @__PURE__ */ new Date(),
315
+ args: [],
316
+ ...defaults,
317
+ level: _normalizeLogLevel(defaults.level, this.options.types)
318
+ };
319
+ if (!isRaw && args.length === 1 && isLogObj(args[0])) {
320
+ Object.assign(logObj, args[0]);
321
+ } else {
322
+ logObj.args = [...args];
323
+ }
324
+ if (logObj.message) {
325
+ logObj.args.unshift(logObj.message);
326
+ delete logObj.message;
327
+ }
328
+ if (logObj.additional) {
329
+ if (!Array.isArray(logObj.additional)) {
330
+ logObj.additional = logObj.additional.split("\n");
331
+ }
332
+ logObj.args.push("\n" + logObj.additional.join("\n"));
333
+ delete logObj.additional;
334
+ }
335
+ logObj.type = typeof logObj.type === "string" ? logObj.type.toLowerCase() : "log";
336
+ logObj.tag = typeof logObj.tag === "string" ? logObj.tag : "";
337
+ const resolveLog = (newLog = false) => {
338
+ const repeated = (this._lastLog.count || 0) - this.options.throttleMin;
339
+ if (this._lastLog.object && repeated > 0) {
340
+ const args2 = [...this._lastLog.object.args];
341
+ if (repeated > 1) {
342
+ args2.push(`(repeated ${repeated} times)`);
343
+ }
344
+ this._log({ ...this._lastLog.object, args: args2 });
345
+ this._lastLog.count = 1;
346
+ }
347
+ if (newLog) {
348
+ this._lastLog.object = logObj;
349
+ this._log(logObj);
350
+ }
351
+ };
352
+ clearTimeout(this._lastLog.timeout);
353
+ const diffTime = this._lastLog.time && logObj.date ? logObj.date.getTime() - this._lastLog.time.getTime() : 0;
354
+ this._lastLog.time = logObj.date;
355
+ if (diffTime < this.options.throttle) {
356
+ try {
357
+ const serializedLog = JSON.stringify([
358
+ logObj.type,
359
+ logObj.tag,
360
+ logObj.args
361
+ ]);
362
+ const isSameLog = this._lastLog.serialized === serializedLog;
363
+ this._lastLog.serialized = serializedLog;
364
+ if (isSameLog) {
365
+ this._lastLog.count = (this._lastLog.count || 0) + 1;
366
+ if (this._lastLog.count > this.options.throttleMin) {
367
+ this._lastLog.timeout = setTimeout(
368
+ resolveLog,
369
+ this.options.throttle
370
+ );
371
+ return;
372
+ }
373
+ }
374
+ } catch {
375
+ }
376
+ }
377
+ resolveLog(true);
378
+ }
379
+ _log(logObj) {
380
+ for (const reporter of this.options.reporters) {
381
+ reporter.log(logObj, {
382
+ options: this.options
383
+ });
384
+ }
385
+ }
386
+ }
387
+ function _normalizeLogLevel(input, types = {}, defaultLevel = 3) {
388
+ if (input === void 0) {
389
+ return defaultLevel;
390
+ }
391
+ if (typeof input === "number") {
392
+ return input;
393
+ }
394
+ if (types[input] && types[input].level !== void 0) {
395
+ return types[input].level;
396
+ }
397
+ return defaultLevel;
398
+ }
399
+ Consola.prototype.add = Consola.prototype.addReporter;
400
+ Consola.prototype.remove = Consola.prototype.removeReporter;
401
+ Consola.prototype.clear = Consola.prototype.removeReporter;
402
+ Consola.prototype.withScope = Consola.prototype.withTag;
403
+ Consola.prototype.mock = Consola.prototype.mockTypes;
404
+ Consola.prototype.pause = Consola.prototype.pauseLogs;
405
+ Consola.prototype.resume = Consola.prototype.resumeLogs;
406
+ function createConsola(options = {}) {
407
+ return new Consola(options);
408
+ }
409
+
410
+ const logger = createConsola({
411
+ level: LogLevels.debug
412
+ });
413
+
414
+ export { LogLevels, logger as consola, logger, logger as rootLogger };