nightingale 14.2.1 → 16.0.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.
Files changed (108) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +204 -57
  3. package/dist/definitions/config.d.ts +1 -1
  4. package/dist/definitions/debug/debug.d.ts +5 -0
  5. package/dist/definitions/debug/debug.d.ts.map +1 -0
  6. package/dist/definitions/debug/debug.test.d.ts +2 -0
  7. package/dist/definitions/debug/debug.test.d.ts.map +1 -0
  8. package/dist/definitions/formatter-utils/formatObject.d.ts +9 -0
  9. package/dist/definitions/formatter-utils/formatObject.d.ts.map +1 -0
  10. package/dist/definitions/formatter-utils/formatObject.test.d.ts +2 -0
  11. package/dist/definitions/formatter-utils/formatObject.test.d.ts.map +1 -0
  12. package/dist/definitions/formatter-utils/formatRecordToString.d.ts +4 -0
  13. package/dist/definitions/formatter-utils/formatRecordToString.d.ts.map +1 -0
  14. package/dist/definitions/formatter-utils/index.d.ts +15 -0
  15. package/dist/definitions/formatter-utils/index.d.ts.map +1 -0
  16. package/dist/definitions/formatter-utils/index.test.d.ts +2 -0
  17. package/dist/definitions/formatter-utils/index.test.d.ts.map +1 -0
  18. package/dist/definitions/formatter-utils/levelToStyles.d.ts +3 -0
  19. package/dist/definitions/formatter-utils/levelToStyles.d.ts.map +1 -0
  20. package/dist/definitions/formatter-utils/levelToSymbol.d.ts +3 -0
  21. package/dist/definitions/formatter-utils/levelToSymbol.d.ts.map +1 -0
  22. package/dist/definitions/formatter-utils/styleToHexColor.d.ts +7 -0
  23. package/dist/definitions/formatter-utils/styleToHexColor.d.ts.map +1 -0
  24. package/dist/definitions/formatter-utils/styleToHtmlStyle.d.ts +109 -0
  25. package/dist/definitions/formatter-utils/styleToHtmlStyle.d.ts.map +1 -0
  26. package/dist/definitions/formatters/ANSIFormatter.d.ts +6 -0
  27. package/dist/definitions/formatters/ANSIFormatter.d.ts.map +1 -0
  28. package/dist/definitions/formatters/ANSIFormatter.test.d.ts +2 -0
  29. package/dist/definitions/formatters/ANSIFormatter.test.d.ts.map +1 -0
  30. package/dist/definitions/formatters/BrowserConsoleFormatter.d.ts +10 -0
  31. package/dist/definitions/formatters/BrowserConsoleFormatter.d.ts.map +1 -0
  32. package/dist/definitions/formatters/BrowserConsoleFormatter.test.d.ts +2 -0
  33. package/dist/definitions/formatters/BrowserConsoleFormatter.test.d.ts.map +1 -0
  34. package/dist/definitions/formatters/HTMLFormatter.d.ts +5 -0
  35. package/dist/definitions/formatters/HTMLFormatter.d.ts.map +1 -0
  36. package/dist/definitions/formatters/HTMLFormatter.test.d.ts +2 -0
  37. package/dist/definitions/formatters/HTMLFormatter.test.d.ts.map +1 -0
  38. package/dist/definitions/formatters/JSONFormatter.d.ts +4 -0
  39. package/dist/definitions/formatters/JSONFormatter.d.ts.map +1 -0
  40. package/dist/definitions/formatters/JSONFormatter.test.d.ts +2 -0
  41. package/dist/definitions/formatters/JSONFormatter.test.d.ts.map +1 -0
  42. package/dist/definitions/formatters/MarkdownFormatter.d.ts +5 -0
  43. package/dist/definitions/formatters/MarkdownFormatter.d.ts.map +1 -0
  44. package/dist/definitions/formatters/MarkdownFormatter.test.d.ts +2 -0
  45. package/dist/definitions/formatters/MarkdownFormatter.test.d.ts.map +1 -0
  46. package/dist/definitions/formatters/RawFormatter.d.ts +5 -0
  47. package/dist/definitions/formatters/RawFormatter.d.ts.map +1 -0
  48. package/dist/definitions/formatters/RawFormatter.test.d.ts +2 -0
  49. package/dist/definitions/formatters/RawFormatter.test.d.ts.map +1 -0
  50. package/dist/definitions/handlers/BrowserConsoleHandler.d.ts +14 -0
  51. package/dist/definitions/handlers/BrowserConsoleHandler.d.ts.map +1 -0
  52. package/dist/definitions/handlers/ConsoleCLIHandler.d.ts +12 -0
  53. package/dist/definitions/handlers/ConsoleCLIHandler.d.ts.map +1 -0
  54. package/dist/definitions/handlers/ConsoleHandler.d.ts +14 -0
  55. package/dist/definitions/handlers/ConsoleHandler.d.ts.map +1 -0
  56. package/dist/definitions/handlers/StringHandler.d.ts +9 -0
  57. package/dist/definitions/handlers/StringHandler.d.ts.map +1 -0
  58. package/dist/definitions/index.d.ts +18 -4
  59. package/dist/definitions/index.d.ts.map +1 -1
  60. package/dist/definitions/loggers/LoggerCLI.d.ts +23 -0
  61. package/dist/definitions/loggers/LoggerCLI.d.ts.map +1 -0
  62. package/dist/definitions/outputs/cliConsoleOutput.d.ts +3 -0
  63. package/dist/definitions/outputs/cliConsoleOutput.d.ts.map +1 -0
  64. package/dist/definitions/outputs/consoleOutput.d.ts +3 -0
  65. package/dist/definitions/outputs/consoleOutput.d.ts.map +1 -0
  66. package/dist/index-browser.es.js +936 -46
  67. package/dist/index-browser.es.js.map +1 -1
  68. package/dist/index-node20.mjs +1023 -0
  69. package/dist/index-node20.mjs.map +1 -0
  70. package/package.json +31 -37
  71. package/src/config.ts +12 -12
  72. package/src/debug/debug.test.ts +50 -0
  73. package/src/debug/debug.ts +100 -0
  74. package/src/formatter-utils/formatObject.test.ts +153 -0
  75. package/src/formatter-utils/formatObject.ts +462 -0
  76. package/src/formatter-utils/formatRecordToString.ts +67 -0
  77. package/src/formatter-utils/index.test.ts +33 -0
  78. package/src/formatter-utils/index.ts +20 -0
  79. package/src/formatter-utils/levelToStyles.ts +14 -0
  80. package/src/formatter-utils/levelToSymbol.ts +14 -0
  81. package/src/formatter-utils/styleToHexColor.ts +9 -0
  82. package/src/formatter-utils/styleToHtmlStyle.ts +69 -0
  83. package/src/formatters/ANSIFormatter.test.ts +27 -0
  84. package/src/formatters/ANSIFormatter.ts +68 -0
  85. package/src/formatters/BrowserConsoleFormatter.test.ts +59 -0
  86. package/src/formatters/BrowserConsoleFormatter.ts +45 -0
  87. package/src/formatters/HTMLFormatter.test.ts +23 -0
  88. package/src/formatters/HTMLFormatter.ts +28 -0
  89. package/src/formatters/JSONFormatter.test.ts +62 -0
  90. package/src/formatters/JSONFormatter.ts +62 -0
  91. package/src/formatters/MarkdownFormatter.test.ts +19 -0
  92. package/src/formatters/MarkdownFormatter.ts +31 -0
  93. package/src/formatters/RawFormatter.test.ts +21 -0
  94. package/src/formatters/RawFormatter.ts +13 -0
  95. package/src/handlers/BrowserConsoleHandler.ts +78 -0
  96. package/src/handlers/ConsoleCLIHandler.ts +41 -0
  97. package/src/handlers/ConsoleHandler.ts +55 -0
  98. package/src/handlers/StringHandler.ts +21 -0
  99. package/src/index.test.ts +29 -29
  100. package/src/index.ts +24 -10
  101. package/src/loggers/LoggerCLI.ts +91 -0
  102. package/src/outputs/cliConsoleOutput.ts +17 -0
  103. package/src/outputs/consoleOutput.ts +18 -0
  104. package/dist/index-browsermodern.es.js +0 -120
  105. package/dist/index-browsermodern.es.js.map +0 -1
  106. package/dist/index-node18.mjs +0 -120
  107. package/dist/index-node18.mjs.map +0 -1
  108. package/src/.eslintrc.json +0 -30
@@ -1,10 +1,12 @@
1
- import { Logger } from 'nightingale-logger';
1
+ import { Logger, Level as Level$1 } from 'nightingale-logger';
2
2
  export { Logger } from 'nightingale-logger';
3
+ import { Level } from 'nightingale-levels';
3
4
  export { Level, Level as levels } from 'nightingale-levels';
5
+ import ansi from 'ansi-styles';
4
6
 
5
- var globalOrWindow = typeof global !== 'undefined' ? global : window;
6
- if (process.env.NODE_ENV !== 'production' && globalOrWindow.__NIGHTINGALE_GLOBAL_HANDLERS) {
7
- throw new Error('nightingale: update all to ^5.0.0');
7
+ const globalOrWindow = typeof global !== "undefined" ? global : window;
8
+ if (process.env.NODE_ENV !== "production" && globalOrWindow.__NIGHTINGALE_GLOBAL_HANDLERS) {
9
+ throw new Error("nightingale: update all to ^5.0.0");
8
10
  }
9
11
  if (!globalOrWindow.__NIGHTINGALE_CONFIG) {
10
12
  globalOrWindow.__NIGHTINGALE_CONFIG = [];
@@ -20,28 +22,28 @@ function clearCache() {
20
22
  function handleConfig(config) {
21
23
  if (config.keys) {
22
24
  if (config.pattern) {
23
- throw new Error('Cannot have key and pattern for the same config');
25
+ throw new Error("Cannot have key and pattern for the same config");
24
26
  }
25
27
  if (config.key) {
26
- throw new Error('Cannot have key and keys for the same config');
28
+ throw new Error("Cannot have key and keys for the same config");
27
29
  }
28
30
  } else if (config.key) {
29
31
  if (config.pattern) {
30
- throw new Error('Cannot have key and pattern for the same config');
32
+ throw new Error("Cannot have key and pattern for the same config");
31
33
  }
32
34
  config.keys = [config.key];
33
35
  delete config.key;
34
36
  }
35
37
  if (config.handler) {
36
38
  if (config.handlers) {
37
- throw new Error('Cannot have handler and handlers for the same config');
39
+ throw new Error("Cannot have handler and handlers for the same config");
38
40
  }
39
41
  config.handlers = [config.handler];
40
42
  delete config.handler;
41
43
  }
42
44
  if (config.processor) {
43
45
  if (config.processors) {
44
- throw new Error('Cannot have processors and processors for the same config');
46
+ throw new Error("Cannot have processors and processors for the same config");
45
47
  }
46
48
  config.processors = [config.processor];
47
49
  delete config.processor;
@@ -51,80 +53,968 @@ function handleConfig(config) {
51
53
  function configure(config) {
52
54
  if (globalOrWindow.__NIGHTINGALE_CONFIG.length > 0) {
53
55
  // eslint-disable-next-line no-console
54
- console.log('nightingale: warning: config overridden');
56
+ console.log("nightingale: warning: config overridden");
55
57
  }
56
58
  clearCache();
57
59
  globalOrWindow.__NIGHTINGALE_CONFIG = config.map(handleConfig);
58
60
  }
59
- function addConfig(config, unshift) {
60
- if (unshift === void 0) {
61
- unshift = false;
62
- }
61
+ function addConfig(config, unshift = false) {
63
62
  config = handleConfig(config);
64
- globalOrWindow.__NIGHTINGALE_CONFIG[unshift ? 'unshift' : 'push'](config);
63
+ globalOrWindow.__NIGHTINGALE_CONFIG[unshift ? "unshift" : "push"](config);
65
64
  clearCache();
66
65
  }
67
- var configIsForKey = function configIsForKey(key) {
68
- return function (config) {
69
- if (config.keys) return config.keys.includes(key);
70
- if (config.pattern) return config.pattern.test(key);
71
- return true;
72
- };
66
+ const configIsForKey = key => config => {
67
+ if (config.keys) return config.keys.includes(key);
68
+ if (config.pattern) return config.pattern.test(key);
69
+ return true;
73
70
  };
74
- globalOrWindow.__NIGHTINGALE_GET_CONFIG_FOR_LOGGER = function (key) {
75
- var globalCache = globalOrWindow.__NIGHTINGALE_LOGGER_MAP_CACHE;
76
- var existingCache = globalCache.get(key);
71
+ globalOrWindow.__NIGHTINGALE_GET_CONFIG_FOR_LOGGER = key => {
72
+ const globalCache = globalOrWindow.__NIGHTINGALE_LOGGER_MAP_CACHE;
73
+ const existingCache = globalCache.get(key);
77
74
  if (existingCache) {
78
75
  return existingCache;
79
76
  }
80
- var loggerConfig = {
77
+ const loggerConfig = {
81
78
  handlers: [],
82
79
  processors: []
83
80
  };
84
- globalOrWindow.__NIGHTINGALE_CONFIG.filter(configIsForKey(key)).some(function (config) {
85
- var _loggerConfig$handler, _loggerConfig$process;
86
- if (config.handlers) (_loggerConfig$handler = loggerConfig.handlers).push.apply(_loggerConfig$handler, config.handlers);
87
- if (config.processors) (_loggerConfig$process = loggerConfig.processors).push.apply(_loggerConfig$process, config.processors);
81
+ globalOrWindow.__NIGHTINGALE_CONFIG.filter(configIsForKey(key)).some(config => {
82
+ if (config.handlers) loggerConfig.handlers.push(...config.handlers);
83
+ if (config.processors) loggerConfig.processors.push(...config.processors);
88
84
  return config.stop;
89
85
  });
90
86
  globalCache.set(key, loggerConfig);
91
87
  return loggerConfig;
92
88
  };
93
89
  if (globalOrWindow.__NIGHTINGALE_GET_CONFIG_FOR_LOGGER_RECORD) {
94
- globalOrWindow.__NIGHTINGALE_GET_CONFIG_FOR_LOGGER_RECORD = function (key, level) {
95
- var _globalOrWindow$__NIG = globalOrWindow.__NIGHTINGALE_GET_CONFIG_FOR_LOGGER(key),
96
- handlers = _globalOrWindow$__NIG.handlers,
97
- processors = _globalOrWindow$__NIG.processors;
90
+ globalOrWindow.__NIGHTINGALE_GET_CONFIG_FOR_LOGGER_RECORD = (key, level) => {
91
+ const {
92
+ handlers,
93
+ processors
94
+ } = globalOrWindow.__NIGHTINGALE_GET_CONFIG_FOR_LOGGER(key);
98
95
  return {
99
- handlers: handlers.filter(function (handler) {
100
- return level >= handler.minLevel && (!handler.isHandling || handler.isHandling(level, key));
101
- }),
102
- processors: processors
96
+ handlers: handlers.filter(handler => level >= handler.minLevel && (!handler.isHandling || handler.isHandling(level, key))),
97
+ processors
103
98
  };
104
99
  };
105
100
  }
106
101
 
102
+ const levelToStyles = {
103
+ [Level.TRACE]: ["gray"],
104
+ [Level.DEBUG]: ["gray"],
105
+ // [Level.INFO]: ['gray'],
106
+ [Level.WARN]: ["yellow"],
107
+ [Level.ERROR]: ["red", "bold"],
108
+ [Level.CRITICAL]: ["red", "bold"],
109
+ [Level.FATAL]: ["bgRed", "white"],
110
+ [Level.EMERGENCY]: ["bgRed", "white"]
111
+ };
112
+
113
+ const levelToSymbol = {
114
+ [Level.TRACE]: "•",
115
+ [Level.DEBUG]: "•",
116
+ [Level.INFO]: "→",
117
+ [Level.WARN]: "⚠",
118
+ [Level.ERROR]: "✖",
119
+ [Level.CRITICAL]: "!",
120
+ [Level.FATAL]: "‼",
121
+ [Level.EMERGENCY]: "‼"
122
+ };
123
+
124
+ const styleToHexColor = {
125
+ orange: "ff5f00",
126
+ grayLight: "808080",
127
+ "gray-light": "808080"
128
+ };
129
+
130
+ const styleToHtmlStyleThemeLight = {
131
+ // text style
132
+ bold: {
133
+ open: "font-weight: bold",
134
+ close: "font-weight: normal"
135
+ },
136
+ italic: {
137
+ open: "font-style: italic",
138
+ close: "font-style: normal"
139
+ },
140
+ underline: {
141
+ open: "text-decoration: underline",
142
+ close: "text-decoration: none"
143
+ },
144
+ inverse: {
145
+ open: "unicode-bidi: bidi-override; direction: rtl",
146
+ close: "unicode-bidi: normal; direction: ltr"
147
+ },
148
+ strikethrough: {
149
+ open: "text-decoration: line-through",
150
+ close: "text-decoration: none"
151
+ },
152
+ black: {
153
+ open: "color: black",
154
+ close: "color: currentcolor"
155
+ },
156
+ red: {
157
+ open: "color: #ff0020",
158
+ close: "color: currentcolor"
159
+ },
160
+ green: {
161
+ open: "color: #00b317",
162
+ close: "color: currentcolor"
163
+ },
164
+ yellow: {
165
+ open: "color: #ffcc00",
166
+ close: "color: currentcolor"
167
+ },
168
+ blue: {
169
+ open: "color: #00a0ff",
170
+ close: "color: currentcolor"
171
+ },
172
+ magenta: {
173
+ open: "color: #ff00a0",
174
+ close: "color: currentcolor"
175
+ },
176
+ cyan: {
177
+ open: "color: #00cfd8",
178
+ close: "color: currentcolor"
179
+ },
180
+ white: {
181
+ open: "color: white",
182
+ close: "color: currentcolor"
183
+ },
184
+ gray: {
185
+ open: "color: gray",
186
+ close: "color: currentcolor"
187
+ },
188
+ bgBlack: {
189
+ open: "background: black",
190
+ close: "background: initial"
191
+ },
192
+ bgRed: {
193
+ open: "background: #ff0020",
194
+ close: "background: initial"
195
+ },
196
+ bgGreen: {
197
+ open: "background: #00b317",
198
+ close: "background: initial"
199
+ },
200
+ bgYellow: {
201
+ open: "background: #ffcc00",
202
+ close: "background: initial"
203
+ },
204
+ bgBlue: {
205
+ open: "background: #00a0ff",
206
+ close: "background: initial"
207
+ },
208
+ bgMagenta: {
209
+ open: "background: #ff00a0",
210
+ close: "background: initial"
211
+ },
212
+ bgCyan: {
213
+ open: "background: #00cfd8",
214
+ close: "background: initial"
215
+ },
216
+ bgWhite: {
217
+ open: "background: white",
218
+ close: "background: initial"
219
+ },
220
+ orange: {
221
+ open: `color: #${styleToHexColor.orange}`,
222
+ close: "color: currentcolor"
223
+ },
224
+ grayLight: {
225
+ open: `color: #${styleToHexColor.grayLight}`,
226
+ close: "color: currentcolor"
227
+ },
228
+ "gray-light": {
229
+ open: `color: #${styleToHexColor.grayLight}`,
230
+ close: "color: currentcolor"
231
+ }
232
+ };
233
+ const styleToHtmlStyleThemeDark = {
234
+ ...styleToHtmlStyleThemeLight,
235
+ black: styleToHtmlStyleThemeLight.white,
236
+ bgBlack: styleToHtmlStyleThemeLight.bgWhite,
237
+ white: styleToHtmlStyleThemeLight.black,
238
+ bgWhite: styleToHtmlStyleThemeLight.bgBlack,
239
+ gray: {
240
+ open: "color: lightgray",
241
+ close: "color: currentcolor"
242
+ }
243
+ };
244
+
245
+ /* eslint-disable @typescript-eslint/no-use-before-define */
246
+
247
+ const noStyleFn = (styles, value) => value;
248
+ function tryStringify(arg) {
249
+ try {
250
+ return JSON.stringify(arg).replace(/\\n/g, "\n");
251
+ } catch {
252
+ return "[Circular]";
253
+ }
254
+ }
255
+ const sameRawFormattedValue = value => ({
256
+ stringValue: value,
257
+ formattedValue: value
258
+ });
259
+ function internalFormatValue(value, styleFn, styles, {
260
+ padding,
261
+ depth,
262
+ maxDepth,
263
+ objects
264
+ }) {
265
+ const typeofValue = typeof value;
266
+ if (!styles) {
267
+ if (value == null) {
268
+ styles = ["cyan"];
269
+ } else {
270
+ switch (typeofValue) {
271
+ case "undefined":
272
+ styles = ["cyan"];
273
+ break;
274
+ case "boolean":
275
+ styles = ["green"];
276
+ break;
277
+ case "number":
278
+ styles = ["yellow"];
279
+ break;
280
+ case "bigint":
281
+ styles = ["red"];
282
+ break;
283
+ case "string":
284
+ styles = ["orange"];
285
+ break;
286
+ case "symbol":
287
+ styles = ["magenta"];
288
+ break;
289
+ }
290
+ }
291
+ }
292
+ let stringValue;
293
+ if (value === null) {
294
+ stringValue = "null";
295
+ } else if (value === undefined) {
296
+ stringValue = "undefined";
297
+ } else if (typeofValue === "boolean") {
298
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
299
+ stringValue = value.toString();
300
+ } else if (value.constructor === Object) {
301
+ if (depth >= maxDepth) {
302
+ stringValue = "{Object...}";
303
+ } else {
304
+ return internalFormatObject(value, styleFn, undefined, {
305
+ padding,
306
+ depth: depth + 1,
307
+ maxDepth,
308
+ objects
309
+ });
310
+ }
311
+ } else if (Array.isArray(value)) {
312
+ if (depth >= maxDepth) {
313
+ stringValue = "[Array...]";
314
+ } else {
315
+ return internalFormatArray(value, styleFn, {
316
+ padding,
317
+ depth: depth + 1,
318
+ maxDepth,
319
+ objects
320
+ });
321
+ }
322
+ } else if (value instanceof Error) {
323
+ const stack = value.stack;
324
+ stringValue = stack?.startsWith(value.message) || stack?.startsWith(`${value.name}: ${value.message}`) ? stack : `${value.message}\n${stack || ""}`;
325
+ } else if (value instanceof Map) {
326
+ const name = value.constructor.name;
327
+ if (depth >= maxDepth) {
328
+ stringValue = `{${name}...}`;
329
+ } else {
330
+ return internalFormatMap(name, value, styleFn, {
331
+ padding,
332
+ depth: depth + 1,
333
+ maxDepth,
334
+ objects
335
+ });
336
+ }
337
+ } else if (typeofValue === "bigint") {
338
+ stringValue = value.toString();
339
+ } else if (typeofValue === "symbol") {
340
+ stringValue = value.toString();
341
+ } else if (value instanceof Set) {
342
+ const name = value.constructor.name;
343
+ if (depth >= maxDepth) {
344
+ stringValue = `{${name}...}`;
345
+ } else {
346
+ return internalFormatSet(name, value, styleFn, {
347
+ padding,
348
+ depth: depth + 1,
349
+ maxDepth,
350
+ objects
351
+ });
352
+ }
353
+ } else if (value instanceof WeakMap) {
354
+ stringValue = "{WeakMap...}";
355
+ } else if (value instanceof WeakSet) {
356
+ stringValue = "{WeakSet...}";
357
+ } else {
358
+ stringValue = tryStringify(value);
359
+ }
360
+ const formattedValue = styleFn(styles, stringValue);
361
+ return {
362
+ stringValue,
363
+ formattedValue
364
+ };
365
+ }
366
+ const separator = ",";
367
+ const internalFormatKey = (key, styleFn) => {
368
+ return {
369
+ stringKey: `${key}: `,
370
+ formattedKey: `${styleFn(["gray-light", "bold"], `${key}:`)} `
371
+ };
372
+ };
373
+ const internalNoKey = () => {
374
+ return {
375
+ stringKey: "",
376
+ formattedKey: ""
377
+ };
378
+ };
379
+ const internalFormatMapKey = (key, styleFn, internalFormatParams) => {
380
+ const {
381
+ stringValue,
382
+ formattedValue
383
+ } = internalFormatValue(key, noStyleFn, undefined, internalFormatParams);
384
+ return {
385
+ stringKey: `${stringValue} => `,
386
+ formattedKey: `${styleFn(["gray-light", "bold"], `${formattedValue}:`)} `
387
+ };
388
+ };
389
+ const internalFormatIterator = (values, styleFn, objectStyles, {
390
+ padding,
391
+ depth,
392
+ maxDepth,
393
+ objects
394
+ }, {
395
+ prefix,
396
+ suffix,
397
+ prefixSuffixSpace = " ",
398
+ formatKey
399
+ }) => {
400
+ let breakLine = false;
401
+ const formattedSeparator = () => styleFn(["gray"], separator);
402
+ const valuesMaxIndex = values.length - 1;
403
+ const formattedValues = values.map(({
404
+ key,
405
+ value
406
+ }, index) => {
407
+ const internalFormatParams = {
408
+ padding,
409
+ depth: depth + 1,
410
+ maxDepth,
411
+ objects
412
+ };
413
+
414
+ // key must be formatted before value (browser-formatter needs order)
415
+ const {
416
+ stringKey,
417
+ formattedKey
418
+ } = formatKey(key, styleFn, internalFormatParams);
419
+ let {
420
+ stringValue,
421
+ formattedValue
422
+ } = internalFormatValue(value, styleFn, key && objectStyles ? objectStyles[key] : undefined, internalFormatParams);
423
+ if (stringValue && (stringValue.length > 80 || stringValue.includes("\n"))) {
424
+ breakLine = true;
425
+ stringValue = stringValue.replace(/\n/g, `\n${padding}`);
426
+ formattedValue = formattedValue.replace(/\n/g, `\n${padding}`);
427
+ }
428
+ return {
429
+ stringValue: stringKey + stringValue + (index === valuesMaxIndex ? "" : separator),
430
+ formattedValue: formattedKey + formattedValue + (index === valuesMaxIndex ? "" : formattedSeparator())
431
+ // note: we need to format the separator for each values for browser-formatter
432
+ };
433
+ });
434
+ return {
435
+ stringValue: prefix + formattedValues.map(breakLine ? v => `\n${padding}${v.stringValue}` : fv => fv.stringValue).join(breakLine ? "\n" : " ") + suffix,
436
+ formattedValue: `${prefix}${breakLine ? "" : prefixSuffixSpace}${formattedValues.map(breakLine ? v => `\n${padding}${v.formattedValue}` : v => v.formattedValue).join(breakLine ? "" : " ")}${breakLine ? ",\n" : prefixSuffixSpace}${suffix}`
437
+ };
438
+ };
439
+ function internalFormatObject(object, styleFn, objectStyles, {
440
+ padding,
441
+ depth,
442
+ maxDepth,
443
+ objects
444
+ }) {
445
+ if (objects.has(object)) {
446
+ return sameRawFormattedValue("{Circular Object}");
447
+ }
448
+ const keys = Object.keys(object);
449
+ if (keys.length === 0) {
450
+ return sameRawFormattedValue("{}");
451
+ }
452
+ objects.add(object);
453
+ const result = internalFormatIterator(keys.map(key => ({
454
+ key,
455
+ value: object[key]
456
+ })), styleFn, objectStyles, {
457
+ padding,
458
+ depth,
459
+ maxDepth,
460
+ objects
461
+ }, {
462
+ prefix: "{",
463
+ suffix: "}",
464
+ formatKey: internalFormatKey
465
+ });
466
+ objects.delete(object);
467
+ return result;
468
+ }
469
+ function internalFormatMap(name, map, styleFn, {
470
+ padding,
471
+ depth,
472
+ maxDepth,
473
+ objects
474
+ }) {
475
+ if (objects.has(map)) {
476
+ return sameRawFormattedValue(`{Circular ${name}}`);
477
+ }
478
+ const keys = [...map.keys()];
479
+ if (keys.length === 0) {
480
+ return sameRawFormattedValue(`${name} {}`);
481
+ }
482
+ objects.add(map);
483
+ const result = internalFormatIterator(keys.map(key => ({
484
+ key,
485
+ value: map.get(key)
486
+ })), styleFn, undefined, {
487
+ padding,
488
+ depth,
489
+ maxDepth,
490
+ objects
491
+ }, {
492
+ prefix: `${name} {`,
493
+ suffix: "}",
494
+ formatKey: internalFormatMapKey
495
+ });
496
+ objects.delete(map);
497
+ return result;
498
+ }
499
+ function internalFormatArray(array, styleFn, {
500
+ padding,
501
+ depth,
502
+ maxDepth,
503
+ objects
504
+ }) {
505
+ if (objects.has(array)) {
506
+ return sameRawFormattedValue("{Circular Array}");
507
+ }
508
+ if (array.length === 0) {
509
+ return sameRawFormattedValue("[]");
510
+ }
511
+ objects.add(array);
512
+ const result = internalFormatIterator(array.map(value => ({
513
+ key: undefined,
514
+ value
515
+ })), styleFn, undefined, {
516
+ padding,
517
+ depth,
518
+ maxDepth,
519
+ objects
520
+ }, {
521
+ prefix: "[",
522
+ suffix: "]",
523
+ prefixSuffixSpace: "",
524
+ formatKey: internalNoKey
525
+ });
526
+ objects.delete(array);
527
+ return result;
528
+ }
529
+ function internalFormatSet(name, set, styleFn, {
530
+ padding,
531
+ depth,
532
+ maxDepth,
533
+ objects
534
+ }) {
535
+ if (objects.has(set)) {
536
+ return sameRawFormattedValue(`{Circular ${name}}`);
537
+ }
538
+ const values = [...set.values()];
539
+ if (values.length === 0) {
540
+ return sameRawFormattedValue(`${name} []`);
541
+ }
542
+ objects.add(set);
543
+ const result = internalFormatIterator(values.map(value => ({
544
+ key: undefined,
545
+ value
546
+ })), styleFn, undefined, {
547
+ padding,
548
+ depth,
549
+ maxDepth,
550
+ objects
551
+ }, {
552
+ prefix: `${name} [`,
553
+ suffix: "]",
554
+ formatKey: internalNoKey
555
+ });
556
+ objects.delete(set);
557
+ return result;
558
+ }
559
+ function formatObject(object, styleFn = noStyleFn, objectStyles, {
560
+ padding = " ",
561
+ maxDepth = 10
562
+ } = {}) {
563
+ const {
564
+ formattedValue: result
565
+ } = internalFormatObject(object, styleFn, objectStyles, {
566
+ padding,
567
+ maxDepth,
568
+ depth: 0,
569
+ objects: new Set()
570
+ });
571
+ if (result === "{}") {
572
+ return "";
573
+ }
574
+ return result;
575
+ }
576
+
577
+ function formatRecordToString(record, style) {
578
+ const parts = [];
579
+ if (record.displayName) {
580
+ parts.push(style(["gray-light"], record.displayName));
581
+ } else if (record.key) {
582
+ parts.push(style(["gray-light"], record.key));
583
+ }
584
+ if (record.datetime) {
585
+ parts.push(style(["gray", "bold"], record.datetime.toTimeString().split(" ", 2)[0]));
586
+ /* new Date().toFormat('HH24:MI:SS') */
587
+ }
588
+ let message = record.symbol || levelToSymbol[record.level] || "";
589
+ const styles = record.styles || levelToStyles[record.level];
590
+ if (record.message) {
591
+ if (message) {
592
+ message += ` ${record.message}`;
593
+ } else {
594
+ message = record.message;
595
+ }
596
+ }
597
+ if (message) {
598
+ if (styles) {
599
+ message = style(styles, message);
600
+ }
601
+ parts.push(message);
602
+ }
603
+ const formatRecordObject = (key, object, objectStyles) => {
604
+ if (!object) {
605
+ return;
606
+ }
607
+ const stringObject = formatObject(object, style, objectStyles);
608
+ if (!stringObject) {
609
+ return;
610
+ }
611
+ parts.push(stringObject);
612
+ };
613
+ formatRecordObject("metadata", record.metadata, record.metadataStyles);
614
+ formatRecordObject("extra", record.extra, undefined);
615
+ formatRecordObject("context", record.context, undefined);
616
+ return parts.join(" ");
617
+ }
618
+
619
+ /* eslint-disable complexity */
620
+ const specialRegexpChars = /[$()+.?[\\\]^{|}]/;
621
+ const createTestFunctionFromRegexp = regexp => string => regexp.test(string);
622
+ const createTestFunctionFromRegexpString = value => {
623
+ if (!value.endsWith("/")) throw new Error("Invalid RegExp DEBUG value");
624
+ return createTestFunctionFromRegexp(new RegExp(value.slice(1, -1)));
625
+ };
626
+ const createTestFunctionFromValue = value => {
627
+ if (value.endsWith(":*")) {
628
+ value = value.slice(0, -2);
629
+ return string => string.startsWith(value);
630
+ }
631
+ return string => string === value;
632
+ };
633
+ function createFindDebugLevel(debugValue) {
634
+ let isWildcard = false;
635
+ const debugValues = [];
636
+ const skips = [];
637
+ if (!Array.isArray(debugValue)) {
638
+ if (debugValue instanceof RegExp) {
639
+ debugValues.push(createTestFunctionFromRegexp(debugValue));
640
+ debugValue = undefined;
641
+ } else if (debugValue) {
642
+ debugValue = debugValue.trim();
643
+ if (debugValue.startsWith("/")) {
644
+ debugValues.push(createTestFunctionFromRegexpString(debugValue));
645
+ debugValue = undefined;
646
+ } else {
647
+ debugValue = debugValue.split(/[\s,]+/);
648
+ }
649
+ }
650
+ }
651
+ if (debugValue) {
652
+ debugValue.forEach(value => {
653
+ if (specialRegexpChars.test(value)) {
654
+ throw new Error(`Invalid debug value: "${value}" (contains special chars)`);
655
+ }
656
+ if (!value) return;
657
+ if (value === "*") {
658
+ isWildcard = true;
659
+ return;
660
+ }
661
+ if (value.startsWith("-")) {
662
+ skips.push(createTestFunctionFromValue(value.slice(1)));
663
+ } else if (!isWildcard) {
664
+ debugValues.push(createTestFunctionFromValue(value));
665
+ }
666
+ });
667
+ }
668
+ if (isWildcard) {
669
+ if (skips.length === 0) {
670
+ return () => Level.ALL;
671
+ } else {
672
+ return (minLevel, key) => skips.some(skip => skip(key)) ? minLevel : Level.ALL;
673
+ }
674
+ }
675
+ if (debugValues.length === 0) {
676
+ return minLevel => minLevel;
677
+ }
678
+ return (minLevel, key) => {
679
+ if (minLevel === Level.ALL || !key) {
680
+ return minLevel;
681
+ }
682
+ if (debugValues.some(dv => dv(key))) {
683
+ return skips.some(skip => skip(key)) ? minLevel : Level.ALL;
684
+ }
685
+ return minLevel;
686
+ };
687
+ }
688
+
689
+ function style$4(styles, value) {
690
+ return value;
691
+ }
692
+ const RawFormatter = {
693
+ format(record) {
694
+ return formatRecordToString(record, style$4);
695
+ }
696
+ };
697
+
698
+ function style$3(styles, string) {
699
+ if (!styles || styles.length === 0 || !string) {
700
+ return string;
701
+ }
702
+
703
+ // eslint-disable-next-line unicorn/no-array-reduce
704
+ return styles.reduce((part, styleName) => {
705
+ switch (styleName) {
706
+ case "bold":
707
+ return `*${part}*`;
708
+ case "italic":
709
+ return `_${part}_`;
710
+ case "strikethrough":
711
+ return `~${part}~`;
712
+
713
+ // no default
714
+ }
715
+ return part;
716
+ }, string);
717
+ }
718
+ const MarkdownFormatter = {
719
+ format(record) {
720
+ return formatRecordToString(record, style$3);
721
+ }
722
+ };
723
+
724
+ function map2object(map) {
725
+ const object = {};
726
+ map.forEach((value, key) => {
727
+ if (typeof key === "object") {
728
+ // ignore key
729
+ return;
730
+ }
731
+
732
+ // eslint-disable-next-line @typescript-eslint/no-base-to-string
733
+ object[String(key)] = value;
734
+ });
735
+ return object;
736
+ }
737
+ function stringify(value, space) {
738
+ return JSON.stringify(value, (key, objectValue) => {
739
+ if (objectValue instanceof Map) {
740
+ return map2object(objectValue);
741
+ }
742
+ if (objectValue instanceof Error) {
743
+ return {
744
+ message: objectValue.message,
745
+ stack: objectValue.stack
746
+ };
747
+ }
748
+ return objectValue;
749
+ }, space);
750
+ }
751
+ const JSONFormatter = {
752
+ format(record) {
753
+ return stringify({
754
+ key: record.key,
755
+ level: record.level,
756
+ datetime: record.datetime,
757
+ message: record.message,
758
+ metadata: record.metadata,
759
+ extra: record.extra
760
+ });
761
+ }
762
+ };
763
+
764
+ const ansiStyles = {
765
+ black: ansi.black,
766
+ red: ansi.red,
767
+ green: ansi.green,
768
+ yellow: ansi.yellow,
769
+ blue: ansi.blue,
770
+ magenta: ansi.magenta,
771
+ cyan: ansi.cyan,
772
+ white: ansi.white,
773
+ gray: ansi.gray,
774
+ bgBlack: ansi.bgBlack,
775
+ bgRed: ansi.bgRed,
776
+ bgGreen: ansi.bgGreen,
777
+ bgYellow: ansi.bgYellow,
778
+ bgBlue: ansi.bgBlue,
779
+ bgMagenta: ansi.bgMagenta,
780
+ bgCyan: ansi.bgCyan,
781
+ bgWhite: ansi.bgWhite,
782
+ bold: ansi.bold,
783
+ underline: ansi.underline,
784
+ // http://www.calmar.ws/vim/256-xterm-24bit-rgb-color-chart.html
785
+ orange: {
786
+ open: ansi.color.ansi256(ansi.hexToAnsi256(styleToHexColor.orange)),
787
+ close: ansi.color.close
788
+ },
789
+ "gray-light": {
790
+ open: ansi.color.ansi256(ansi.hexToAnsi256(styleToHexColor["gray-light"])),
791
+ close: ansi.color.close
792
+ }
793
+ };
794
+ function style$2(styles, string) {
795
+ if (!styles || styles.length === 0 || !string) {
796
+ return string;
797
+ }
798
+
799
+ // eslint-disable-next-line unicorn/no-array-reduce
800
+ return styles.reduce((styledString, styleName) => {
801
+ const codePair = ansiStyles[styleName];
802
+ if (!codePair) {
803
+ throw new Error(`Unknown style: ${styleName}`);
804
+ }
805
+ return codePair.open + styledString + codePair.close;
806
+ }, string);
807
+ }
808
+ const ANSIFormatter = {
809
+ format: record => formatRecordToString(record, style$2)
810
+ };
811
+
812
+ function style$1(styles, string) {
813
+ if (!styles || styles.length === 0 || !string) {
814
+ return string;
815
+ }
816
+ return `<span style="${styles.map(styleName => styleToHtmlStyleThemeLight[styleName].open).join("; ")}">${string}</span>`;
817
+ }
818
+ const HTMLFormatter = {
819
+ format(record) {
820
+ return formatRecordToString(record, style$1);
821
+ }
822
+ };
823
+
824
+ const style = (styleToHtmlStyle, args) => (styles, string) => {
825
+ if (!styles || styles.length === 0 || !string) {
826
+ return string;
827
+ }
828
+ const htmlStyles = styles.map(styleName => styleToHtmlStyle[styleName]);
829
+ args.push(htmlStyles.map(s => s.open).join("; "), htmlStyles.map(s => s.close).join("; "));
830
+ return `%c${string}%c`;
831
+ };
832
+ class BrowserConsoleFormatter {
833
+ constructor(theme = "light") {
834
+ this.styleToHtmlStyle = theme === "dark" ? styleToHtmlStyleThemeDark : styleToHtmlStyleThemeLight;
835
+ }
836
+ format(record) {
837
+ const args = [];
838
+ const string = formatRecordToString(record, style(this.styleToHtmlStyle, args));
839
+ return [string, ...args];
840
+ }
841
+ }
842
+
843
+ /* eslint-disable no-console */
844
+ function consoleOutput(param, record) {
845
+ console[record.level >= Level.ERROR ? "error" : "log"](...param);
846
+ }
847
+
848
+ /* eslint-disable prefer-template */
849
+
850
+ class StringHandler {
851
+ _buffer = "";
852
+ constructor(minLevel) {
853
+ this.minLevel = minLevel;
854
+ }
855
+ get string() {
856
+ return this._buffer;
857
+ }
858
+ handle(record) {
859
+ this._buffer += RawFormatter.format(record) + "\n";
860
+ }
861
+ }
862
+
863
+ function getDebugString() {
864
+ const querystring = document.location.search;
865
+ const debugFromLocalStorage =
866
+ // eslint-disable-next-line unicorn/prefer-global-this, @typescript-eslint/no-unnecessary-condition
867
+ window.localStorage?.getItem("debug") || "";
868
+ if (!querystring) {
869
+ return debugFromLocalStorage;
870
+ }
871
+
872
+ // https://developer.mozilla.org/en-US/docs/Web/API/URLUtils/search#Get_the_value_of_a_single_search_param
873
+ const debugFromQueryString = decodeURI(querystring.replace(
874
+ // eslint-disable-next-line prefer-regex-literals, regexp/no-super-linear-backtracking
875
+ new RegExp("^(?:.*[&?]DEBUG(?:=([^&]*))?)?.*$", "i"), "$1"));
876
+ return (debugFromLocalStorage ? `${debugFromLocalStorage},` : "") + debugFromQueryString;
877
+ }
878
+
879
+ // debug string can change any time (localStorage), so we need a new object each time.
880
+ const findDebugLevel$2 = (minLevel, key) => createFindDebugLevel(getDebugString())(minLevel, key);
881
+ const getDefaultTheme = () => {
882
+ try {
883
+ const configInLocalStorage = localStorage.getItem("NIGHTINGALE_THEME");
884
+ if (configInLocalStorage && configInLocalStorage === "dark") {
885
+ return configInLocalStorage;
886
+ }
887
+ } catch {}
888
+ return "light";
889
+ };
890
+ const createHandler = (theme = getDefaultTheme()) => {
891
+ const browserConsoleFormatter = new BrowserConsoleFormatter(theme);
892
+ return record => {
893
+ consoleOutput(browserConsoleFormatter.format(record), record);
894
+ };
895
+ };
896
+ class BrowserConsoleHandler {
897
+ minLevel = 0;
898
+ constructor(minLevel, options = {}) {
899
+ this.isHandling = (level, key) => level >= findDebugLevel$2(minLevel, key);
900
+ this.handle = createHandler(options.theme);
901
+ }
902
+ }
903
+
904
+ const defaultFormatter = ANSIFormatter.format;
905
+ const createHandle$1 = (formatter = defaultFormatter, output = consoleOutput) => {
906
+ return record => {
907
+ output(formatter(record), record);
908
+ };
909
+ };
910
+ const findDebugLevel$1 = createFindDebugLevel(process.env.DEBUG);
911
+ class ConsoleHandler {
912
+ minLevel = (() => Level.ALL)();
913
+ constructor(minLevel, options = {}) {
914
+ this.minLevel = minLevel;
915
+ this.isHandling = (level, key) => level >= findDebugLevel$1(minLevel, key);
916
+ this.handle = createHandle$1(options.formatter, options.output);
917
+ }
918
+ }
919
+
920
+ /* eslint-disable no-console */
921
+ function cliConsoleOutput(param, record) {
922
+ console[record.level >= Level.ERROR ? "error" : "log"](...param);
923
+ }
924
+
925
+ const createHandle = ({
926
+ json
927
+ }) => {
928
+ const formatter = json ? JSONFormatter.format : ANSIFormatter.format;
929
+ const output = json ? consoleOutput : cliConsoleOutput;
930
+ return record => {
931
+ output(formatter(record), record);
932
+ };
933
+ };
934
+ const findDebugLevel = createFindDebugLevel(process.env.DEBUG);
935
+ class ConsoleCLIHandler {
936
+ minLevel = (() => Level.ALL)();
937
+ constructor(minLevel, options = {}) {
938
+ this.minLevel = minLevel;
939
+ this.isHandling = (level, key) => level >= findDebugLevel(minLevel, key);
940
+ this.handle = createHandle(options);
941
+ }
942
+ }
943
+
944
+ class LoggerCLI extends Logger {
945
+ processors = [];
946
+ constructor(key, {
947
+ displayName,
948
+ processors,
949
+ json = false
950
+ } = {}) {
951
+ super(key, displayName);
952
+ this.handlers = [new ConsoleCLIHandler(Level$1.INFO, {
953
+ json
954
+ })];
955
+ this.processors = processors ?? [];
956
+ this.json = json;
957
+ }
958
+ getHandlersAndProcessors() {
959
+ return {
960
+ handlers: this.handlers,
961
+ processors: this.processors
962
+ };
963
+ }
964
+ logJsonOnly(messageOrError, metadata, level = Level$1.INFO) {
965
+ if (this.json) {
966
+ this.log(messageOrError, metadata, level);
967
+ }
968
+ }
969
+ debugJsonOnly(messageOrError, metadata) {
970
+ if (this.json) {
971
+ this.debug(messageOrError, metadata);
972
+ }
973
+ }
974
+ noticeJsonOnly(messageOrError, metadata) {
975
+ if (this.json) {
976
+ this.notice(messageOrError, metadata);
977
+ }
978
+ }
979
+ infoJsonOnly(messageOrError, metadata) {
980
+ if (this.json) {
981
+ this.info(messageOrError, metadata);
982
+ }
983
+ }
984
+ warnJsonOnly(messageOrError, metadata) {
985
+ if (this.json) {
986
+ this.warn(messageOrError, metadata);
987
+ }
988
+ }
989
+ group(name, fn) {
990
+ if (this.json) {
991
+ fn();
992
+ } else {
993
+ console.group(name);
994
+ fn();
995
+ console.groupEnd();
996
+ }
997
+ }
998
+ }
999
+
107
1000
  /**
108
1001
  * listen to uncaughtException and unhandledRejection
109
1002
  * @param {Logger} [logger]
110
1003
  */
111
- function listenUnhandledErrors(logger) {
112
- if (logger === void 0) {
113
- logger = new Logger('nightingale:listenUnhandledErrors', 'UnhandledErrors');
114
- }
115
- process.on('uncaughtException', function (error) {
1004
+ function listenUnhandledErrors(logger = new Logger("nightingale:listenUnhandledErrors", "UnhandledErrors")) {
1005
+ process.on("uncaughtException", error => {
116
1006
  logger.error(error, {
117
1007
  unhandled: true,
118
- type: 'uncaughtException'
1008
+ type: "uncaughtException"
119
1009
  });
120
1010
  });
121
- process.on('unhandledRejection', function (error) {
1011
+ process.on("unhandledRejection", error => {
122
1012
  logger.error(error, {
123
1013
  unhandled: true,
124
- type: 'unhandledRejection'
1014
+ type: "unhandledRejection"
125
1015
  });
126
1016
  });
127
1017
  }
128
1018
 
129
- export { addConfig, configure, listenUnhandledErrors };
1019
+ export { ANSIFormatter, BrowserConsoleFormatter, BrowserConsoleHandler, ConsoleCLIHandler, ConsoleHandler, HTMLFormatter, JSONFormatter, LoggerCLI, MarkdownFormatter, RawFormatter, StringHandler, addConfig, configure, consoleOutput, createFindDebugLevel, formatObject, formatRecordToString, levelToStyles, levelToSymbol, listenUnhandledErrors, styleToHexColor, styleToHtmlStyleThemeDark, styleToHtmlStyleThemeLight };
130
1020
  //# sourceMappingURL=index-browser.es.js.map