@logtape/logtape 0.11.0-dev.175 → 0.12.0-dev.181

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 (171) hide show
  1. package/config.test.ts +591 -0
  2. package/config.ts +421 -0
  3. package/context.test.ts +187 -0
  4. package/context.ts +55 -0
  5. package/deno.json +36 -0
  6. package/dist/_virtual/rolldown_runtime.cjs +30 -0
  7. package/dist/config.cjs +247 -0
  8. package/dist/config.d.cts +189 -0
  9. package/dist/config.d.cts.map +1 -0
  10. package/dist/config.d.ts +189 -0
  11. package/dist/config.d.ts.map +1 -0
  12. package/dist/config.js +241 -0
  13. package/dist/config.js.map +1 -0
  14. package/dist/context.cjs +30 -0
  15. package/dist/context.d.cts +39 -0
  16. package/dist/context.d.cts.map +1 -0
  17. package/dist/context.d.ts +39 -0
  18. package/dist/context.d.ts.map +1 -0
  19. package/dist/context.js +31 -0
  20. package/dist/context.js.map +1 -0
  21. package/dist/filter.cjs +32 -0
  22. package/dist/filter.d.cts +37 -0
  23. package/dist/filter.d.cts.map +1 -0
  24. package/{types → dist}/filter.d.ts +12 -6
  25. package/dist/filter.d.ts.map +1 -0
  26. package/dist/filter.js +31 -0
  27. package/dist/filter.js.map +1 -0
  28. package/dist/formatter.cjs +281 -0
  29. package/dist/formatter.d.cts +338 -0
  30. package/dist/formatter.d.cts.map +1 -0
  31. package/dist/formatter.d.ts +338 -0
  32. package/dist/formatter.d.ts.map +1 -0
  33. package/dist/formatter.js +275 -0
  34. package/dist/formatter.js.map +1 -0
  35. package/dist/level.cjs +64 -0
  36. package/dist/level.d.cts +34 -0
  37. package/dist/level.d.cts.map +1 -0
  38. package/{types → dist}/level.d.ts +7 -5
  39. package/dist/level.d.ts.map +1 -0
  40. package/dist/level.js +62 -0
  41. package/dist/level.js.map +1 -0
  42. package/dist/logger.cjs +351 -0
  43. package/dist/logger.d.cts +501 -0
  44. package/dist/logger.d.cts.map +1 -0
  45. package/dist/logger.d.ts +501 -0
  46. package/dist/logger.d.ts.map +1 -0
  47. package/dist/logger.js +351 -0
  48. package/dist/logger.js.map +1 -0
  49. package/dist/mod.cjs +33 -0
  50. package/dist/mod.d.cts +9 -0
  51. package/dist/mod.d.ts +9 -0
  52. package/dist/mod.js +9 -0
  53. package/dist/record.d.cts +50 -0
  54. package/dist/record.d.cts.map +1 -0
  55. package/dist/record.d.ts +50 -0
  56. package/dist/record.d.ts.map +1 -0
  57. package/dist/sink.cjs +95 -0
  58. package/dist/sink.d.cts +112 -0
  59. package/dist/sink.d.cts.map +1 -0
  60. package/{types → dist}/sink.d.ts +49 -45
  61. package/dist/sink.d.ts.map +1 -0
  62. package/dist/sink.js +94 -0
  63. package/dist/sink.js.map +1 -0
  64. package/dist/util.cjs +9 -0
  65. package/dist/util.d.cts +12 -0
  66. package/dist/util.d.cts.map +1 -0
  67. package/dist/util.d.ts +12 -0
  68. package/dist/util.d.ts.map +1 -0
  69. package/dist/util.deno.cjs +16 -0
  70. package/dist/util.deno.d.cts +12 -0
  71. package/dist/util.deno.d.cts.map +1 -0
  72. package/dist/util.deno.d.ts +12 -0
  73. package/dist/util.deno.d.ts.map +1 -0
  74. package/dist/util.deno.js +16 -0
  75. package/dist/util.deno.js.map +1 -0
  76. package/dist/util.js +9 -0
  77. package/dist/util.js.map +1 -0
  78. package/dist/util.node.cjs +10 -0
  79. package/dist/util.node.d.cts +12 -0
  80. package/dist/util.node.d.cts.map +1 -0
  81. package/dist/util.node.d.ts +12 -0
  82. package/dist/util.node.d.ts.map +1 -0
  83. package/dist/util.node.js +10 -0
  84. package/dist/util.node.js.map +1 -0
  85. package/filter.test.ts +70 -0
  86. package/filter.ts +57 -0
  87. package/fixtures.ts +30 -0
  88. package/formatter.test.ts +530 -0
  89. package/formatter.ts +724 -0
  90. package/level.test.ts +47 -0
  91. package/level.ts +67 -0
  92. package/logger.test.ts +823 -0
  93. package/logger.ts +1124 -0
  94. package/mod.ts +54 -0
  95. package/package.json +35 -23
  96. package/record.ts +49 -0
  97. package/sink.test.ts +219 -0
  98. package/sink.ts +167 -0
  99. package/tsdown.config.ts +24 -0
  100. package/util.deno.ts +19 -0
  101. package/util.node.ts +12 -0
  102. package/util.ts +11 -0
  103. package/esm/_dnt.shims.js +0 -57
  104. package/esm/config.js +0 -297
  105. package/esm/context.js +0 -23
  106. package/esm/filter.js +0 -42
  107. package/esm/formatter.js +0 -370
  108. package/esm/level.js +0 -59
  109. package/esm/logger.js +0 -517
  110. package/esm/mod.js +0 -8
  111. package/esm/nodeUtil.cjs +0 -20
  112. package/esm/nodeUtil.js +0 -2
  113. package/esm/package.json +0 -3
  114. package/esm/record.js +0 -1
  115. package/esm/sink.js +0 -96
  116. package/script/_dnt.shims.js +0 -60
  117. package/script/config.js +0 -331
  118. package/script/context.js +0 -26
  119. package/script/filter.js +0 -46
  120. package/script/formatter.js +0 -380
  121. package/script/level.js +0 -64
  122. package/script/logger.js +0 -548
  123. package/script/mod.js +0 -36
  124. package/script/nodeUtil.js +0 -20
  125. package/script/package.json +0 -3
  126. package/script/record.js +0 -2
  127. package/script/sink.js +0 -101
  128. package/types/_dnt.shims.d.ts +0 -2
  129. package/types/_dnt.shims.d.ts.map +0 -1
  130. package/types/_dnt.test_shims.d.ts.map +0 -1
  131. package/types/config.d.ts +0 -183
  132. package/types/config.d.ts.map +0 -1
  133. package/types/config.test.d.ts.map +0 -1
  134. package/types/context.d.ts +0 -35
  135. package/types/context.d.ts.map +0 -1
  136. package/types/context.test.d.ts.map +0 -1
  137. package/types/deps/jsr.io/@std/assert/0.222.1/_constants.d.ts.map +0 -1
  138. package/types/deps/jsr.io/@std/assert/0.222.1/_diff.d.ts.map +0 -1
  139. package/types/deps/jsr.io/@std/assert/0.222.1/_format.d.ts.map +0 -1
  140. package/types/deps/jsr.io/@std/assert/0.222.1/assert.d.ts.map +0 -1
  141. package/types/deps/jsr.io/@std/assert/0.222.1/assert_equals.d.ts.map +0 -1
  142. package/types/deps/jsr.io/@std/assert/0.222.1/assert_false.d.ts.map +0 -1
  143. package/types/deps/jsr.io/@std/assert/0.222.1/assert_greater_or_equal.d.ts.map +0 -1
  144. package/types/deps/jsr.io/@std/assert/0.222.1/assert_is_error.d.ts.map +0 -1
  145. package/types/deps/jsr.io/@std/assert/0.222.1/assert_less_or_equal.d.ts.map +0 -1
  146. package/types/deps/jsr.io/@std/assert/0.222.1/assert_rejects.d.ts.map +0 -1
  147. package/types/deps/jsr.io/@std/assert/0.222.1/assert_strict_equals.d.ts.map +0 -1
  148. package/types/deps/jsr.io/@std/assert/0.222.1/assert_throws.d.ts.map +0 -1
  149. package/types/deps/jsr.io/@std/assert/0.222.1/assertion_error.d.ts.map +0 -1
  150. package/types/deps/jsr.io/@std/assert/0.222.1/equal.d.ts.map +0 -1
  151. package/types/deps/jsr.io/@std/async/0.222.1/delay.d.ts.map +0 -1
  152. package/types/deps/jsr.io/@std/fmt/0.222.1/colors.d.ts.map +0 -1
  153. package/types/filter.d.ts.map +0 -1
  154. package/types/filter.test.d.ts.map +0 -1
  155. package/types/fixtures.d.ts.map +0 -1
  156. package/types/formatter.d.ts +0 -332
  157. package/types/formatter.d.ts.map +0 -1
  158. package/types/formatter.test.d.ts.map +0 -1
  159. package/types/level.d.ts.map +0 -1
  160. package/types/level.test.d.ts.map +0 -1
  161. package/types/logger.d.ts +0 -573
  162. package/types/logger.d.ts.map +0 -1
  163. package/types/logger.test.d.ts.map +0 -1
  164. package/types/mod.d.ts +0 -9
  165. package/types/mod.d.ts.map +0 -1
  166. package/types/nodeUtil.d.ts +0 -12
  167. package/types/nodeUtil.d.ts.map +0 -1
  168. package/types/record.d.ts +0 -44
  169. package/types/record.d.ts.map +0 -1
  170. package/types/sink.d.ts.map +0 -1
  171. package/types/sink.test.d.ts.map +0 -1
package/esm/formatter.js DELETED
@@ -1,370 +0,0 @@
1
- import util from "./nodeUtil.js";
2
- /**
3
- * The severity level abbreviations.
4
- */
5
- const levelAbbreviations = {
6
- "debug": "DBG",
7
- "info": "INF",
8
- "warning": "WRN",
9
- "error": "ERR",
10
- "fatal": "FTL",
11
- };
12
- /**
13
- * A platform-specific inspect function. In Deno, this is {@link Deno.inspect},
14
- * and in Node.js/Bun it is `util.inspect()`. If neither is available, it
15
- * falls back to {@link JSON.stringify}.
16
- *
17
- * @param value The value to inspect.
18
- * @param options The options for inspecting the value.
19
- * If `colors` is `true`, the output will be ANSI-colored.
20
- * @returns The string representation of the value.
21
- */
22
- const inspect =
23
- // @ts-ignore: Browser detection
24
- // dnt-shim-ignore
25
- typeof document !== "undefined" ||
26
- // @ts-ignore: React Native detection
27
- // dnt-shim-ignore
28
- typeof navigator !== "undefined" && navigator.product === "ReactNative"
29
- ? (v) => JSON.stringify(v)
30
- // @ts-ignore: Deno global
31
- // dnt-shim-ignore
32
- : "Deno" in globalThis && "inspect" in globalThis.Deno &&
33
- // @ts-ignore: Deno global
34
- // dnt-shim-ignore
35
- typeof globalThis.Deno.inspect === "function"
36
- ? (v, opts) =>
37
- // @ts-ignore: Deno global
38
- // dnt-shim-ignore
39
- globalThis.Deno.inspect(v, {
40
- strAbbreviateSize: Infinity,
41
- iterableLimit: Infinity,
42
- ...opts,
43
- })
44
- // @ts-ignore: Node.js global
45
- // dnt-shim-ignore
46
- : util != null && "inspect" in util && typeof util.inspect === "function"
47
- ? (v, opts) =>
48
- // @ts-ignore: Node.js global
49
- // dnt-shim-ignore
50
- util.inspect(v, {
51
- maxArrayLength: Infinity,
52
- maxStringLength: Infinity,
53
- ...opts,
54
- })
55
- : (v) => JSON.stringify(v);
56
- /**
57
- * Get a text formatter with the specified options. Although it's flexible
58
- * enough to create a custom formatter, if you want more control, you can
59
- * create a custom formatter that satisfies the {@link TextFormatter} type
60
- * instead.
61
- *
62
- * For more information on the options, see {@link TextFormatterOptions}.
63
- *
64
- * By default, the formatter formats log records as follows:
65
- *
66
- * ```
67
- * 2023-11-14 22:13:20.000 +00:00 [INF] category·subcategory: Hello, world!
68
- * ```
69
- * @param options The options for the text formatter.
70
- * @returns The text formatter.
71
- * @since 0.6.0
72
- */
73
- export function getTextFormatter(options = {}) {
74
- const timestampRenderer = options.timestamp == null || options.timestamp === "date-time-timezone"
75
- ? (ts) => new Date(ts).toISOString().replace("T", " ").replace("Z", " +00:00")
76
- : options.timestamp === "date-time-tz"
77
- ? (ts) => new Date(ts).toISOString().replace("T", " ").replace("Z", " +00")
78
- : options.timestamp === "date-time"
79
- ? (ts) => new Date(ts).toISOString().replace("T", " ").replace("Z", "")
80
- : options.timestamp === "time-timezone"
81
- ? (ts) => new Date(ts).toISOString().replace(/.*T/, "").replace("Z", " +00:00")
82
- : options.timestamp === "time-tz"
83
- ? (ts) => new Date(ts).toISOString().replace(/.*T/, "").replace("Z", " +00")
84
- : options.timestamp === "time"
85
- ? (ts) => new Date(ts).toISOString().replace(/.*T/, "").replace("Z", "")
86
- : options.timestamp === "date"
87
- ? (ts) => new Date(ts).toISOString().replace(/T.*/, "")
88
- : options.timestamp === "rfc3339"
89
- ? (ts) => new Date(ts).toISOString()
90
- : options.timestamp === "none" || options.timestamp === "disabled"
91
- ? () => null
92
- : options.timestamp;
93
- const categorySeparator = options.category ?? "·";
94
- const valueRenderer = options.value ?? inspect;
95
- const levelRenderer = options.level == null || options.level === "ABBR"
96
- ? (level) => levelAbbreviations[level]
97
- : options.level === "abbr"
98
- ? (level) => levelAbbreviations[level].toLowerCase()
99
- : options.level === "FULL"
100
- ? (level) => level.toUpperCase()
101
- : options.level === "full"
102
- ? (level) => level
103
- : options.level === "L"
104
- ? (level) => level.charAt(0).toUpperCase()
105
- : options.level === "l"
106
- ? (level) => level.charAt(0)
107
- : options.level;
108
- const formatter = options.format ??
109
- (({ timestamp, level, category, message }) => `${timestamp ? `${timestamp} ` : ""}[${level}] ${category}: ${message}`);
110
- return (record) => {
111
- let message = "";
112
- for (let i = 0; i < record.message.length; i++) {
113
- if (i % 2 === 0)
114
- message += record.message[i];
115
- else
116
- message += valueRenderer(record.message[i]);
117
- }
118
- const timestamp = timestampRenderer(record.timestamp);
119
- const level = levelRenderer(record.level);
120
- const category = typeof categorySeparator === "function"
121
- ? categorySeparator(record.category)
122
- : record.category.join(categorySeparator);
123
- const values = {
124
- timestamp,
125
- level,
126
- category,
127
- message,
128
- record,
129
- };
130
- return `${formatter(values)}\n`;
131
- };
132
- }
133
- /**
134
- * The default text formatter. This formatter formats log records as follows:
135
- *
136
- * ```
137
- * 2023-11-14 22:13:20.000 +00:00 [INF] category·subcategory: Hello, world!
138
- * ```
139
- *
140
- * @param record The log record to format.
141
- * @returns The formatted log record.
142
- */
143
- export const defaultTextFormatter = getTextFormatter();
144
- const RESET = "\x1b[0m";
145
- const ansiColors = {
146
- black: "\x1b[30m",
147
- red: "\x1b[31m",
148
- green: "\x1b[32m",
149
- yellow: "\x1b[33m",
150
- blue: "\x1b[34m",
151
- magenta: "\x1b[35m",
152
- cyan: "\x1b[36m",
153
- white: "\x1b[37m",
154
- };
155
- const ansiStyles = {
156
- bold: "\x1b[1m",
157
- dim: "\x1b[2m",
158
- italic: "\x1b[3m",
159
- underline: "\x1b[4m",
160
- strikethrough: "\x1b[9m",
161
- };
162
- const defaultLevelColors = {
163
- debug: "blue",
164
- info: "green",
165
- warning: "yellow",
166
- error: "red",
167
- fatal: "magenta",
168
- };
169
- /**
170
- * Get an ANSI color formatter with the specified options.
171
- *
172
- * ![A preview of an ANSI color formatter.](https://i.imgur.com/I8LlBUf.png)
173
- * @param option The options for the ANSI color formatter.
174
- * @returns The ANSI color formatter.
175
- * @since 0.6.0
176
- */
177
- export function getAnsiColorFormatter(options = {}) {
178
- const format = options.format;
179
- const timestampStyle = typeof options.timestampStyle === "undefined"
180
- ? "dim"
181
- : options.timestampStyle;
182
- const timestampColor = options.timestampColor ?? null;
183
- const timestampPrefix = `${timestampStyle == null ? "" : ansiStyles[timestampStyle]}${timestampColor == null ? "" : ansiColors[timestampColor]}`;
184
- const timestampSuffix = timestampStyle == null && timestampColor == null
185
- ? ""
186
- : RESET;
187
- const levelStyle = typeof options.levelStyle === "undefined"
188
- ? "bold"
189
- : options.levelStyle;
190
- const levelColors = options.levelColors ?? defaultLevelColors;
191
- const categoryStyle = typeof options.categoryStyle === "undefined"
192
- ? "dim"
193
- : options.categoryStyle;
194
- const categoryColor = options.categoryColor ?? null;
195
- const categoryPrefix = `${categoryStyle == null ? "" : ansiStyles[categoryStyle]}${categoryColor == null ? "" : ansiColors[categoryColor]}`;
196
- const categorySuffix = categoryStyle == null && categoryColor == null
197
- ? ""
198
- : RESET;
199
- return getTextFormatter({
200
- timestamp: "date-time-tz",
201
- value(value) {
202
- return inspect(value, { colors: true });
203
- },
204
- ...options,
205
- format({ timestamp, level, category, message, record }) {
206
- const levelColor = levelColors[record.level];
207
- timestamp = `${timestampPrefix}${timestamp}${timestampSuffix}`;
208
- level = `${levelStyle == null ? "" : ansiStyles[levelStyle]}${levelColor == null ? "" : ansiColors[levelColor]}${level}${levelStyle == null && levelColor == null ? "" : RESET}`;
209
- return format == null
210
- ? `${timestamp} ${level} ${categoryPrefix}${category}:${categorySuffix} ${message}`
211
- : format({
212
- timestamp,
213
- level,
214
- category: `${categoryPrefix}${category}${categorySuffix}`,
215
- message,
216
- record,
217
- });
218
- },
219
- });
220
- }
221
- /**
222
- * A text formatter that uses ANSI colors to format log records.
223
- *
224
- * ![A preview of ansiColorFormatter.](https://i.imgur.com/I8LlBUf.png)
225
- *
226
- * @param record The log record to format.
227
- * @returns The formatted log record.
228
- * @since 0.5.0
229
- */
230
- export const ansiColorFormatter = getAnsiColorFormatter();
231
- /**
232
- * Get a [JSON Lines] formatter with the specified options. The log records
233
- * will be rendered as JSON objects, one per line, which is a common format
234
- * for log files. This format is also known as Newline-Delimited JSON (NDJSON).
235
- * It looks like this:
236
- *
237
- * ```json
238
- * {"@timestamp":"2023-11-14T22:13:20.000Z","level":"INFO","message":"Hello, world!","logger":"my.logger","properties":{"key":"value"}}
239
- * ```
240
- *
241
- * [JSON Lines]: https://jsonlines.org/
242
- * @param options The options for the JSON Lines formatter.
243
- * @returns The JSON Lines formatter.
244
- * @since 0.11.0
245
- */
246
- export function getJsonLinesFormatter(options = {}) {
247
- let joinCategory;
248
- if (typeof options.categorySeparator === "function") {
249
- joinCategory = options.categorySeparator;
250
- }
251
- else {
252
- const separator = options.categorySeparator ?? ".";
253
- joinCategory = (category) => category.join(separator);
254
- }
255
- let getMessage;
256
- if (options.message === "template") {
257
- getMessage = (record) => {
258
- if (typeof record.rawMessage === "string") {
259
- return record.rawMessage;
260
- }
261
- let msg = "";
262
- for (let i = 0; i < record.rawMessage.length; i++) {
263
- msg += i % 2 < 1 ? record.rawMessage[i] : "{}";
264
- }
265
- return msg;
266
- };
267
- }
268
- else {
269
- getMessage = (record) => {
270
- let msg = "";
271
- for (let i = 0; i < record.message.length; i++) {
272
- msg += i % 2 < 1
273
- ? record.message[i]
274
- : JSON.stringify(record.message[i]);
275
- }
276
- return msg;
277
- };
278
- }
279
- const propertiesOption = options.properties ?? "nest:properties";
280
- let getProperties;
281
- if (propertiesOption === "flatten") {
282
- getProperties = (properties) => properties;
283
- }
284
- else if (propertiesOption.startsWith("prepend:")) {
285
- const prefix = propertiesOption.substring(8);
286
- if (prefix === "") {
287
- throw new TypeError(`Invalid properties option: ${JSON.stringify(propertiesOption)}. It must be of the form "prepend:<prefix>" where <prefix> is a non-empty string.`);
288
- }
289
- getProperties = (properties) => {
290
- const result = {};
291
- for (const key in properties) {
292
- result[`${prefix}${key}`] = properties[key];
293
- }
294
- return result;
295
- };
296
- }
297
- else if (propertiesOption.startsWith("nest:")) {
298
- const key = propertiesOption.substring(5);
299
- getProperties = (properties) => ({ [key]: properties });
300
- }
301
- else {
302
- throw new TypeError(`Invalid properties option: ${JSON.stringify(propertiesOption)}. It must be "flatten", "prepend:<prefix>", or "nest:<key>".`);
303
- }
304
- return (record) => {
305
- return JSON.stringify({
306
- "@timestamp": new Date(record.timestamp).toISOString(),
307
- level: record.level === "warning" ? "WARN" : record.level.toUpperCase(),
308
- message: getMessage(record),
309
- logger: joinCategory(record.category),
310
- ...getProperties(record.properties),
311
- });
312
- };
313
- }
314
- /**
315
- * The default [JSON Lines] formatter. This formatter formats log records
316
- * as JSON objects, one per line, which is a common format for log files.
317
- * It looks like this:
318
- *
319
- * ```json
320
- * {"@timestamp":"2023-11-14T22:13:20.000Z","level":"INFO","message":"Hello, world!","logger":"my.logger","properties":{"key":"value"}}
321
- * ```
322
- *
323
- * You can customize the output by passing options to
324
- * {@link getJsonLinesFormatter}. For example, you can change the category
325
- * separator, the message format, and how the properties are formatted.
326
- *
327
- * [JSON Lines]: https://jsonlines.org/
328
- * @since 0.11.0
329
- */
330
- export const jsonLinesFormatter = getJsonLinesFormatter();
331
- /**
332
- * The styles for the log level in the console.
333
- */
334
- const logLevelStyles = {
335
- "debug": "background-color: gray; color: white;",
336
- "info": "background-color: white; color: black;",
337
- "warning": "background-color: orange; color: black;",
338
- "error": "background-color: red; color: white;",
339
- "fatal": "background-color: maroon; color: white;",
340
- };
341
- /**
342
- * The default console formatter.
343
- *
344
- * @param record The log record to format.
345
- * @returns The formatted log record, as an array of arguments for
346
- * {@link console.log}.
347
- */
348
- export function defaultConsoleFormatter(record) {
349
- let msg = "";
350
- const values = [];
351
- for (let i = 0; i < record.message.length; i++) {
352
- if (i % 2 === 0)
353
- msg += record.message[i];
354
- else {
355
- msg += "%o";
356
- values.push(record.message[i]);
357
- }
358
- }
359
- const date = new Date(record.timestamp);
360
- const time = `${date.getUTCHours().toString().padStart(2, "0")}:${date.getUTCMinutes().toString().padStart(2, "0")}:${date.getUTCSeconds().toString().padStart(2, "0")}.${date.getUTCMilliseconds().toString().padStart(3, "0")}`;
361
- return [
362
- `%c${time} %c${levelAbbreviations[record.level]}%c %c${record.category.join("\xb7")} %c${msg}`,
363
- "color: gray;",
364
- logLevelStyles[record.level],
365
- "background-color: default;",
366
- "color: gray;",
367
- "color: default;",
368
- ...values,
369
- ];
370
- }
package/esm/level.js DELETED
@@ -1,59 +0,0 @@
1
- const logLevels = ["debug", "info", "warning", "error", "fatal"];
2
- /**
3
- * Parses a log level from a string.
4
- *
5
- * @param level The log level as a string. This is case-insensitive.
6
- * @returns The log level.
7
- * @throws {TypeError} If the log level is invalid.
8
- */
9
- export function parseLogLevel(level) {
10
- level = level.toLowerCase();
11
- switch (level) {
12
- case "debug":
13
- case "info":
14
- case "warning":
15
- case "error":
16
- case "fatal":
17
- return level;
18
- default:
19
- throw new TypeError(`Invalid log level: ${level}.`);
20
- }
21
- }
22
- /**
23
- * Checks if a string is a valid log level. This function can be used as
24
- * as a type guard to narrow the type of a string to a {@link LogLevel}.
25
- *
26
- * @param level The log level as a string. This is case-sensitive.
27
- * @returns `true` if the string is a valid log level.
28
- */
29
- export function isLogLevel(level) {
30
- switch (level) {
31
- case "debug":
32
- case "info":
33
- case "warning":
34
- case "error":
35
- case "fatal":
36
- return true;
37
- default:
38
- return false;
39
- }
40
- }
41
- /**
42
- * Compares two log levels.
43
- * @param a The first log level.
44
- * @param b The second log level.
45
- * @returns A negative number if `a` is less than `b`, a positive number if `a`
46
- * is greater than `b`, or zero if they are equal.
47
- * @since 0.8.0
48
- */
49
- export function compareLogLevel(a, b) {
50
- const aIndex = logLevels.indexOf(a);
51
- if (aIndex < 0) {
52
- throw new TypeError(`Invalid log level: ${JSON.stringify(a)}.`);
53
- }
54
- const bIndex = logLevels.indexOf(b);
55
- if (bIndex < 0) {
56
- throw new TypeError(`Invalid log level: ${JSON.stringify(b)}.`);
57
- }
58
- return aIndex - bIndex;
59
- }