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