@visulima/pail 2.1.15 → 2.1.17

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.
Files changed (82) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/index.browser.cjs +19 -1
  3. package/dist/index.browser.mjs +14 -1
  4. package/dist/index.server.cjs +442 -5
  5. package/dist/index.server.mjs +437 -5
  6. package/dist/packem_shared/CallerProcessor-CyJFdYGN.mjs +58 -0
  7. package/dist/packem_shared/CallerProcessor-DYrXv6Fp.cjs +60 -0
  8. package/dist/packem_shared/JsonFileReporter-BHaRQMnS.cjs +135 -0
  9. package/dist/packem_shared/JsonFileReporter-CdHau53i.mjs +133 -0
  10. package/dist/packem_shared/JsonReporter-C05lFJhj.mjs +20 -0
  11. package/dist/packem_shared/JsonReporter-DCd8dbfQ.mjs +32 -0
  12. package/dist/packem_shared/JsonReporter-DDubAhfJ.cjs +22 -0
  13. package/dist/packem_shared/JsonReporter-HXYHZuWz.cjs +34 -0
  14. package/dist/packem_shared/MessageFormatterProcessor-CFyi7GWt.mjs +53 -0
  15. package/dist/packem_shared/MessageFormatterProcessor-D_txQ9nO.cjs +55 -0
  16. package/dist/packem_shared/PrettyReporter-Bx3QLZrW.mjs +162 -0
  17. package/dist/packem_shared/PrettyReporter-LWDDriJ1.cjs +200 -0
  18. package/dist/packem_shared/PrettyReporter-XC0Ix5SW.mjs +192 -0
  19. package/dist/packem_shared/PrettyReporter-mFCjMb0i.cjs +168 -0
  20. package/dist/packem_shared/RedactProcessor-CfNkF2lC.mjs +26 -0
  21. package/dist/packem_shared/RedactProcessor-DWE5iqAQ.cjs +26 -0
  22. package/dist/packem_shared/SimpleReporter-CuNgsGVB.mjs +178 -0
  23. package/dist/packem_shared/SimpleReporter-DabZGkIs.cjs +186 -0
  24. package/dist/packem_shared/abstract-json-reporter-CG0EXqip.mjs +55 -0
  25. package/dist/packem_shared/abstract-json-reporter-D4MiU6B-.cjs +57 -0
  26. package/dist/packem_shared/abstract-pretty-reporter-BR_6_JVa.cjs +49 -0
  27. package/dist/packem_shared/abstract-pretty-reporter-izJgxoMv.mjs +46 -0
  28. package/dist/packem_shared/constants-BYYZ5WNW.mjs +114 -0
  29. package/dist/packem_shared/constants-DHfYGxxG.cjs +118 -0
  30. package/dist/packem_shared/format-label-CQCvMzlF.cjs +622 -0
  31. package/dist/packem_shared/format-label-DYMZf3Bg.mjs +610 -0
  32. package/dist/packem_shared/get-longest-label-CM8kLFWH.mjs +11 -0
  33. package/dist/packem_shared/get-longest-label-CQ7lRhgD.cjs +13 -0
  34. package/dist/packem_shared/pail.browser-B3Bi2uPA.cjs +387 -0
  35. package/dist/packem_shared/pail.browser-DZLrCtc5.mjs +384 -0
  36. package/dist/packem_shared/write-console-log-Cu0-IDN-.cjs +18 -0
  37. package/dist/packem_shared/write-console-log-Tptfzg9D.mjs +16 -0
  38. package/dist/packem_shared/write-stream-BBAF33Zm.mjs +8 -0
  39. package/dist/packem_shared/write-stream-CkNf2Ju8.cjs +10 -0
  40. package/dist/processor.browser.cjs +11 -1
  41. package/dist/processor.browser.mjs +2 -1
  42. package/dist/processor.server.cjs +13 -1
  43. package/dist/processor.server.mjs +3 -1
  44. package/dist/reporter.browser.cjs +11 -1
  45. package/dist/reporter.browser.mjs +2 -1
  46. package/dist/reporter.server.cjs +15 -1
  47. package/dist/reporter.server.mjs +4 -1
  48. package/package.json +15 -15
  49. package/dist/packem_shared/CallerProcessor-BBHkvRgS.mjs +0 -1
  50. package/dist/packem_shared/CallerProcessor-CLoY9R76.cjs +0 -1
  51. package/dist/packem_shared/JsonFileReporter-CLEMnuGI.cjs +0 -2
  52. package/dist/packem_shared/JsonFileReporter-lr0s7r9B.mjs +0 -4
  53. package/dist/packem_shared/JsonReporter-CiQkPCv4.cjs +0 -2
  54. package/dist/packem_shared/JsonReporter-CjvkrXUp.cjs +0 -1
  55. package/dist/packem_shared/JsonReporter-DInAbLuf.mjs +0 -1
  56. package/dist/packem_shared/JsonReporter-J50eZjl2.mjs +0 -2
  57. package/dist/packem_shared/MessageFormatterProcessor-CCRIUIdT.cjs +0 -1
  58. package/dist/packem_shared/MessageFormatterProcessor-D_XajTuK.mjs +0 -1
  59. package/dist/packem_shared/PrettyReporter-1SleqVaI.mjs +0 -13
  60. package/dist/packem_shared/PrettyReporter-C_AJrNOj.mjs +0 -5
  61. package/dist/packem_shared/PrettyReporter-CldK9UpN.cjs +0 -13
  62. package/dist/packem_shared/PrettyReporter-CuJWFD0k.cjs +0 -5
  63. package/dist/packem_shared/RedactProcessor-BVZtatWJ.cjs +0 -1
  64. package/dist/packem_shared/RedactProcessor-CxKTN42V.mjs +0 -3
  65. package/dist/packem_shared/SimpleReporter-Cy_0ZeXQ.cjs +0 -11
  66. package/dist/packem_shared/SimpleReporter-DzUWU5Gu.mjs +0 -11
  67. package/dist/packem_shared/abstract-json-reporter-29d4dwOH.mjs +0 -1
  68. package/dist/packem_shared/abstract-json-reporter-Z6bVrpPK.cjs +0 -1
  69. package/dist/packem_shared/abstract-pretty-reporter-CZb4ET-0.cjs +0 -1
  70. package/dist/packem_shared/abstract-pretty-reporter-DgXRHEj-.mjs +0 -1
  71. package/dist/packem_shared/constants-BFrKdBPm.cjs +0 -1
  72. package/dist/packem_shared/constants-BMS51p6Y.mjs +0 -1
  73. package/dist/packem_shared/format-label-B0BaeOry.cjs +0 -8
  74. package/dist/packem_shared/format-label-ECls2QEw.mjs +0 -8
  75. package/dist/packem_shared/get-longest-label-4ISUsE2q.mjs +0 -1
  76. package/dist/packem_shared/get-longest-label-DXpzhu5N.cjs +0 -1
  77. package/dist/packem_shared/pail.browser-D7k9K5zt.mjs +0 -1
  78. package/dist/packem_shared/pail.browser-tTEE0Aky.cjs +0 -1
  79. package/dist/packem_shared/write-console-log-3G15muTS.cjs +0 -1
  80. package/dist/packem_shared/write-console-log-BP95fgQZ.mjs +0 -1
  81. package/dist/packem_shared/write-stream-Bgxqp0HI.cjs +0 -1
  82. package/dist/packem_shared/write-stream-C17GXSxA.mjs +0 -1
@@ -0,0 +1,387 @@
1
+ 'use strict';
2
+
3
+ const safeStableStringify = require('safe-stable-stringify');
4
+ const constants = require('./constants-DHfYGxxG.cjs');
5
+ const writeConsoleLog = require('./write-console-log-Cu0-IDN-.cjs');
6
+ const getLongestLabel = require('./get-longest-label-CQ7lRhgD.cjs');
7
+
8
+ var __defProp$3 = Object.defineProperty;
9
+ var __name$3 = (target, value) => __defProp$3(target, "name", { value, configurable: true });
10
+ class RawReporter {
11
+ static {
12
+ __name$3(this, "RawReporter");
13
+ }
14
+ // eslint-disable-next-line class-methods-use-this
15
+ log(meta) {
16
+ const { context = [], message, type } = meta;
17
+ const consoleLogFunction = writeConsoleLog.writeConsoleLogBasedOnLevel(type.level);
18
+ consoleLogFunction(message, ...context);
19
+ }
20
+ }
21
+
22
+ var __defProp$2 = Object.defineProperty;
23
+ var __name$2 = (target, value) => __defProp$2(target, "name", { value, configurable: true });
24
+ const arrayify = /* @__PURE__ */ __name$2((x) => {
25
+ if (x === undefined) {
26
+ return [];
27
+ }
28
+ return Array.isArray(x) ? x : [x];
29
+ }, "arrayify");
30
+
31
+ var __defProp$1 = Object.defineProperty;
32
+ var __name$1 = (target, value) => __defProp$1(target, "name", { value, configurable: true });
33
+ const mergeTypes = /* @__PURE__ */ __name$1((standard, custom) => {
34
+ const types = { ...standard };
35
+ Object.keys(custom).forEach((type) => {
36
+ types[type] = { ...types[type], ...custom[type] };
37
+ });
38
+ return types;
39
+ }, "mergeTypes");
40
+
41
+ var __defProp = Object.defineProperty;
42
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
43
+ class PailBrowserImpl {
44
+ static {
45
+ __name(this, "PailBrowserImpl");
46
+ }
47
+ timersMap;
48
+ countMap;
49
+ seqTimers;
50
+ lastLog;
51
+ logLevels;
52
+ disabled;
53
+ scopeName;
54
+ types;
55
+ longestLabel;
56
+ processors;
57
+ generalLogLevel;
58
+ reporters;
59
+ throttle;
60
+ throttleMin;
61
+ stringify;
62
+ groups;
63
+ startTimerMessage;
64
+ endTimerMessage;
65
+ rawReporter;
66
+ constructor(options) {
67
+ this.throttle = options.throttle ?? 1e3;
68
+ this.throttleMin = options.throttleMin ?? 5;
69
+ this.stringify = safeStableStringify.configure({
70
+ strict: true
71
+ });
72
+ this.startTimerMessage = options.messages?.timerStart ?? "Initialized timer...";
73
+ this.endTimerMessage = options.messages?.timerEnd ?? "Timer run for:";
74
+ this.types = mergeTypes(constants.LOG_TYPES, options.types ?? {});
75
+ this.longestLabel = getLongestLabel.getLongestLabel(this.types);
76
+ this.logLevels = { ...constants.EXTENDED_RFC_5424_LOG_LEVELS, ...options.logLevels };
77
+ this.generalLogLevel = this._normalizeLogLevel(options.logLevel);
78
+ this.reporters = /* @__PURE__ */ new Set();
79
+ this.processors = /* @__PURE__ */ new Set();
80
+ this.disabled = options.disabled ?? false;
81
+ this.scopeName = arrayify(options.scope).filter(Boolean);
82
+ this.timersMap = /* @__PURE__ */ new Map();
83
+ this.countMap = /* @__PURE__ */ new Map();
84
+ this.groups = [];
85
+ this.seqTimers = /* @__PURE__ */ new Set();
86
+ for (const type in this.types) {
87
+ this[type] = this._logger.bind(this, type, false);
88
+ }
89
+ this.lastLog = {};
90
+ if (Array.isArray(options.reporters)) {
91
+ this.registerReporters(options.reporters);
92
+ }
93
+ this.rawReporter = this.extendReporter(options.rawReporter ?? new RawReporter());
94
+ if (Array.isArray(options.processors)) {
95
+ this.registerProcessors(options.processors);
96
+ }
97
+ }
98
+ wrapConsole() {
99
+ for (const type in this.types) {
100
+ if (!console["__" + type]) {
101
+ console["__" + type] = console[type];
102
+ }
103
+ console[type] = this[type];
104
+ }
105
+ }
106
+ restoreConsole() {
107
+ for (const type in this.types) {
108
+ if (console["__" + type]) {
109
+ console[type] = console["__" + type];
110
+ delete console["__" + type];
111
+ }
112
+ }
113
+ }
114
+ wrapException() {
115
+ {
116
+ process.on("uncaughtException", (error) => {
117
+ this.error(error);
118
+ });
119
+ process.on("unhandledRejection", (error) => {
120
+ this.error(error);
121
+ });
122
+ }
123
+ }
124
+ /**
125
+ * Disables logging
126
+ */
127
+ disable() {
128
+ this.disabled = true;
129
+ }
130
+ /**
131
+ * Enables logging
132
+ */
133
+ enable() {
134
+ this.disabled = false;
135
+ }
136
+ isEnabled() {
137
+ return !this.disabled;
138
+ }
139
+ scope(...name) {
140
+ if (name.length === 0) {
141
+ throw new Error("No scope name was defined.");
142
+ }
143
+ this.scopeName = name.flat();
144
+ return this;
145
+ }
146
+ unscope() {
147
+ this.scopeName = [];
148
+ }
149
+ time(label = "default") {
150
+ if (this.seqTimers.has(label)) {
151
+ this._logger("warn", false, {
152
+ message: "Timer '" + label + "' already exists",
153
+ prefix: label
154
+ });
155
+ } else {
156
+ this.seqTimers.add(label);
157
+ this.timersMap.set(label, Date.now());
158
+ this._logger("start", false, {
159
+ message: this.startTimerMessage,
160
+ prefix: label
161
+ });
162
+ }
163
+ }
164
+ timeLog(label, ...data) {
165
+ if (!label && this.seqTimers.size > 0) {
166
+ label = [...this.seqTimers].pop();
167
+ }
168
+ if (label && this.timersMap.has(label)) {
169
+ const span = Date.now() - this.timersMap.get(label);
170
+ this._logger("info", false, {
171
+ context: data,
172
+ message: span < 1e3 ? span + " ms" : (span / 1e3).toFixed(2) + " s",
173
+ prefix: label
174
+ });
175
+ } else {
176
+ this._logger("warn", false, {
177
+ context: data,
178
+ message: "Timer not found",
179
+ prefix: label
180
+ });
181
+ }
182
+ }
183
+ timeEnd(label) {
184
+ if (!label && this.seqTimers.size > 0) {
185
+ label = [...this.seqTimers].pop();
186
+ }
187
+ if (label && this.timersMap.has(label)) {
188
+ const span = Date.now() - this.timersMap.get(label);
189
+ this.timersMap.delete(label);
190
+ this._logger("stop", false, {
191
+ message: this.endTimerMessage + " " + (span < 1e3 ? span + " ms" : (span / 1e3).toFixed(2) + " s"),
192
+ prefix: label
193
+ });
194
+ } else {
195
+ this._logger("warn", false, {
196
+ message: "Timer not found",
197
+ prefix: label
198
+ });
199
+ }
200
+ }
201
+ group(label = "console.group") {
202
+ if (typeof window === "undefined") {
203
+ this.groups.push(label);
204
+ } else {
205
+ console.group(label);
206
+ }
207
+ }
208
+ groupEnd() {
209
+ if (typeof window === "undefined") {
210
+ this.groups.pop();
211
+ } else {
212
+ console.groupEnd();
213
+ }
214
+ }
215
+ count(label = "default") {
216
+ const current = this.countMap.get(label) ?? 0;
217
+ this.countMap.set(label, current + 1);
218
+ this._logger("log", false, {
219
+ message: label + ": " + (current + 1),
220
+ prefix: label
221
+ });
222
+ }
223
+ countReset(label = "default") {
224
+ if (this.countMap.has(label)) {
225
+ this.countMap.delete(label);
226
+ } else {
227
+ this._logger("warn", false, {
228
+ message: "Count for " + label + " does not exist",
229
+ prefix: label
230
+ });
231
+ }
232
+ }
233
+ // eslint-disable-next-line class-methods-use-this
234
+ clear() {
235
+ console.clear();
236
+ }
237
+ raw(message, ...arguments_) {
238
+ if (this.disabled) {
239
+ return;
240
+ }
241
+ this._logger("log", true, {
242
+ context: arguments_,
243
+ message
244
+ });
245
+ }
246
+ extendReporter(reporter) {
247
+ if (typeof reporter.setLoggerTypes === "function") {
248
+ reporter.setLoggerTypes(this.types);
249
+ }
250
+ if (typeof reporter.setStringify === "function") {
251
+ reporter.setStringify(this.stringify);
252
+ }
253
+ return reporter;
254
+ }
255
+ registerReporters(reporters) {
256
+ for (const reporter of reporters) {
257
+ this.reporters.add(this.extendReporter(reporter));
258
+ }
259
+ }
260
+ _report(meta, raw) {
261
+ if (raw) {
262
+ this.rawReporter.log(Object.freeze(meta));
263
+ } else {
264
+ for (const reporter of this.reporters) {
265
+ reporter.log(Object.freeze(meta));
266
+ }
267
+ }
268
+ }
269
+ registerProcessors(processors) {
270
+ for (const processor of processors) {
271
+ if (typeof processor.setStringify === "function") {
272
+ processor.setStringify(this.stringify);
273
+ }
274
+ this.processors.add(processor);
275
+ }
276
+ }
277
+ // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
278
+ _normalizeLogLevel(level) {
279
+ return level && this.logLevels[level] ? level : "debug";
280
+ }
281
+ // eslint-disable-next-line sonarjs/cognitive-complexity,@typescript-eslint/no-explicit-any
282
+ _buildMeta(typeName, type, ...arguments_) {
283
+ const meta = {
284
+ badge: undefined,
285
+ context: undefined,
286
+ error: undefined,
287
+ label: undefined,
288
+ message: constants.EMPTY_SYMBOL,
289
+ prefix: undefined,
290
+ repeated: undefined,
291
+ scope: undefined,
292
+ suffix: undefined
293
+ };
294
+ meta.type = {
295
+ level: type.logLevel,
296
+ name: typeName
297
+ };
298
+ meta.groups = this.groups;
299
+ meta.scope = this.scopeName;
300
+ meta.date = /* @__PURE__ */ new Date();
301
+ if (arguments_.length === 1 && typeof arguments_[0] === "object" && arguments_[0] !== null) {
302
+ if (arguments_[0] instanceof Error) {
303
+ meta.error = arguments_[0];
304
+ } else if ("message" in arguments_[0]) {
305
+ const { context, message, prefix, suffix } = arguments_[0];
306
+ if (context) {
307
+ meta.context = context;
308
+ }
309
+ if (prefix) {
310
+ meta.prefix = prefix;
311
+ }
312
+ if (suffix) {
313
+ meta.suffix = suffix;
314
+ }
315
+ meta.message = message;
316
+ } else {
317
+ meta.message = arguments_[0];
318
+ }
319
+ } else if (arguments_.length > 1 && typeof arguments_[0] === "string") {
320
+ meta.message = arguments_[0];
321
+ meta.context = arguments_.slice(1);
322
+ } else {
323
+ meta.message = arguments_[0];
324
+ }
325
+ if (type.logLevel === "trace") {
326
+ meta.traceError = new Error("Trace");
327
+ }
328
+ if (type.badge) {
329
+ meta.badge = type.badge;
330
+ }
331
+ if (type.label) {
332
+ meta.label = type.label;
333
+ }
334
+ return meta;
335
+ }
336
+ // eslint-disable-next-line sonarjs/cognitive-complexity,@typescript-eslint/no-explicit-any
337
+ _logger(type, raw, ...messageObject) {
338
+ if (this.disabled) {
339
+ return;
340
+ }
341
+ const logLevel = this._normalizeLogLevel(this.types[type].logLevel);
342
+ if (this.logLevels[logLevel] >= this.logLevels[this.generalLogLevel]) {
343
+ let meta = this._buildMeta(type, this.types[type], ...messageObject);
344
+ const resolveLog = /* @__PURE__ */ __name((newLog = false) => {
345
+ const repeated = (this.lastLog.count || 0) - this.throttleMin;
346
+ if (this.lastLog.object && repeated > 0) {
347
+ const lastMeta = { ...this.lastLog.object };
348
+ if (repeated > 1) {
349
+ lastMeta.repeated = repeated;
350
+ }
351
+ this._report(lastMeta, raw);
352
+ this.lastLog.count = 1;
353
+ }
354
+ if (newLog) {
355
+ for (const processor of this.processors) {
356
+ meta = { ...processor.process(meta) };
357
+ }
358
+ this.lastLog.object = meta;
359
+ this._report(meta, raw);
360
+ }
361
+ }, "resolveLog");
362
+ clearTimeout(this.lastLog.timeout);
363
+ const diffTime = this.lastLog.time && meta.date ? new Date(meta.date).getTime() - this.lastLog.time.getTime() : 0;
364
+ this.lastLog.time = new Date(meta.date);
365
+ if (diffTime < this.throttle) {
366
+ try {
367
+ const serializedLog = JSON.stringify([meta.label, meta.scope, meta.type, meta.message, meta.prefix, meta.suffix, meta.context]);
368
+ const isSameLog = this.lastLog.serialized === serializedLog;
369
+ this.lastLog.serialized = serializedLog;
370
+ if (isSameLog) {
371
+ this.lastLog.count = (this.lastLog.count || 0) + 1;
372
+ if (this.lastLog.count > this.throttleMin) {
373
+ this.lastLog.timeout = setTimeout(resolveLog, this.throttle);
374
+ return;
375
+ }
376
+ }
377
+ } catch {
378
+ }
379
+ }
380
+ resolveLog(true);
381
+ }
382
+ }
383
+ }
384
+ const PailBrowser = PailBrowserImpl;
385
+
386
+ exports.PailBrowser = PailBrowser;
387
+ exports.PailBrowserImpl = PailBrowserImpl;