@logtape/logtape 0.11.0 → 0.12.0-dev.182

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/logger.ts ADDED
@@ -0,0 +1,1124 @@
1
+ import type { ContextLocalStorage } from "./context.ts";
2
+ import type { Filter } from "./filter.ts";
3
+ import { compareLogLevel, type LogLevel } from "./level.ts";
4
+ import type { LogRecord } from "./record.ts";
5
+ import type { Sink } from "./sink.ts";
6
+
7
+ /**
8
+ * A logger interface. It provides methods to log messages at different
9
+ * severity levels.
10
+ *
11
+ * ```typescript
12
+ * const logger = getLogger("category");
13
+ * logger.debug `A debug message with ${value}.`;
14
+ * logger.info `An info message with ${value}.`;
15
+ * logger.warn `A warning message with ${value}.`;
16
+ * logger.error `An error message with ${value}.`;
17
+ * logger.fatal `A fatal error message with ${value}.`;
18
+ * ```
19
+ */
20
+ export interface Logger {
21
+ /**
22
+ * The category of the logger. It is an array of strings.
23
+ */
24
+ readonly category: readonly string[];
25
+
26
+ /**
27
+ * The logger with the supercategory of the current logger. If the current
28
+ * logger is the root logger, this is `null`.
29
+ */
30
+ readonly parent: Logger | null;
31
+
32
+ /**
33
+ * Get a child logger with the given subcategory.
34
+ *
35
+ * ```typescript
36
+ * const logger = getLogger("category");
37
+ * const subLogger = logger.getChild("sub-category");
38
+ * ```
39
+ *
40
+ * The above code is equivalent to:
41
+ *
42
+ * ```typescript
43
+ * const logger = getLogger("category");
44
+ * const subLogger = getLogger(["category", "sub-category"]);
45
+ * ```
46
+ *
47
+ * @param subcategory The subcategory.
48
+ * @returns The child logger.
49
+ */
50
+ getChild(
51
+ subcategory: string | readonly [string] | readonly [string, ...string[]],
52
+ ): Logger;
53
+
54
+ /**
55
+ * Get a logger with contextual properties. This is useful for
56
+ * log multiple messages with the shared set of properties.
57
+ *
58
+ * ```typescript
59
+ * const logger = getLogger("category");
60
+ * const ctx = logger.with({ foo: 123, bar: "abc" });
61
+ * ctx.info("A message with {foo} and {bar}.");
62
+ * ctx.warn("Another message with {foo}, {bar}, and {baz}.", { baz: true });
63
+ * ```
64
+ *
65
+ * The above code is equivalent to:
66
+ *
67
+ * ```typescript
68
+ * const logger = getLogger("category");
69
+ * logger.info("A message with {foo} and {bar}.", { foo: 123, bar: "abc" });
70
+ * logger.warn(
71
+ * "Another message with {foo}, {bar}, and {baz}.",
72
+ * { foo: 123, bar: "abc", baz: true },
73
+ * );
74
+ * ```
75
+ *
76
+ * @param properties
77
+ * @returns
78
+ * @since 0.5.0
79
+ */
80
+ with(properties: Record<string, unknown>): Logger;
81
+
82
+ /**
83
+ * Log a debug message. Use this as a template string prefix.
84
+ *
85
+ * ```typescript
86
+ * logger.debug `A debug message with ${value}.`;
87
+ * ```
88
+ *
89
+ * @param message The message template strings array.
90
+ * @param values The message template values.
91
+ */
92
+ debug(message: TemplateStringsArray, ...values: readonly unknown[]): void;
93
+
94
+ /**
95
+ * Log a debug message with properties.
96
+ *
97
+ * ```typescript
98
+ * logger.debug('A debug message with {value}.', { value });
99
+ * ```
100
+ *
101
+ * If the properties are expensive to compute, you can pass a callback that
102
+ * returns the properties:
103
+ *
104
+ * ```typescript
105
+ * logger.debug(
106
+ * 'A debug message with {value}.',
107
+ * () => ({ value: expensiveComputation() })
108
+ * );
109
+ * ```
110
+ *
111
+ * @param message The message template. Placeholders to be replaced with
112
+ * `values` are indicated by keys in curly braces (e.g.,
113
+ * `{value}`).
114
+ * @param properties The values to replace placeholders with. For lazy
115
+ * evaluation, this can be a callback that returns the
116
+ * properties.
117
+ */
118
+ debug(
119
+ message: string,
120
+ properties?: Record<string, unknown> | (() => Record<string, unknown>),
121
+ ): void;
122
+
123
+ /**
124
+ * Log a debug values with no message. This is useful when you
125
+ * want to log properties without a message, e.g., when you want to log
126
+ * the context of a request or an operation.
127
+ *
128
+ * ```typescript
129
+ * logger.debug({ method: 'GET', url: '/api/v1/resource' });
130
+ * ```
131
+ *
132
+ * Note that this is a shorthand for:
133
+ *
134
+ * ```typescript
135
+ * logger.debug('{*}', { method: 'GET', url: '/api/v1/resource' });
136
+ * ```
137
+ *
138
+ * If the properties are expensive to compute, you cannot use this shorthand
139
+ * and should use the following syntax instead:
140
+ *
141
+ * ```typescript
142
+ * logger.debug('{*}', () => ({
143
+ * method: expensiveMethod(),
144
+ * url: expensiveUrl(),
145
+ * }));
146
+ * ```
147
+ *
148
+ * @param properties The values to log. Note that this does not take
149
+ * a callback.
150
+ * @since 0.11.0
151
+ */
152
+ debug(properties: Record<string, unknown>): void;
153
+
154
+ /**
155
+ * Lazily log a debug message. Use this when the message values are expensive
156
+ * to compute and should only be computed if the message is actually logged.
157
+ *
158
+ * ```typescript
159
+ * logger.debug(l => l`A debug message with ${expensiveValue()}.`);
160
+ * ```
161
+ *
162
+ * @param callback A callback that returns the message template prefix.
163
+ * @throws {TypeError} If no log record was made inside the callback.
164
+ */
165
+ debug(callback: LogCallback): void;
166
+
167
+ /**
168
+ * Log an informational message. Use this as a template string prefix.
169
+ *
170
+ * ```typescript
171
+ * logger.info `An info message with ${value}.`;
172
+ * ```
173
+ *
174
+ * @param message The message template strings array.
175
+ * @param values The message template values.
176
+ */
177
+ info(message: TemplateStringsArray, ...values: readonly unknown[]): void;
178
+
179
+ /**
180
+ * Log an informational message with properties.
181
+ *
182
+ * ```typescript
183
+ * logger.info('An info message with {value}.', { value });
184
+ * ```
185
+ *
186
+ * If the properties are expensive to compute, you can pass a callback that
187
+ * returns the properties:
188
+ *
189
+ * ```typescript
190
+ * logger.info(
191
+ * 'An info message with {value}.',
192
+ * () => ({ value: expensiveComputation() })
193
+ * );
194
+ * ```
195
+ *
196
+ * @param message The message template. Placeholders to be replaced with
197
+ * `values` are indicated by keys in curly braces (e.g.,
198
+ * `{value}`).
199
+ * @param properties The values to replace placeholders with. For lazy
200
+ * evaluation, this can be a callback that returns the
201
+ * properties.
202
+ */
203
+ info(
204
+ message: string,
205
+ properties?: Record<string, unknown> | (() => Record<string, unknown>),
206
+ ): void;
207
+
208
+ /**
209
+ * Log an informational values with no message. This is useful when you
210
+ * want to log properties without a message, e.g., when you want to log
211
+ * the context of a request or an operation.
212
+ *
213
+ * ```typescript
214
+ * logger.info({ method: 'GET', url: '/api/v1/resource' });
215
+ * ```
216
+ *
217
+ * Note that this is a shorthand for:
218
+ *
219
+ * ```typescript
220
+ * logger.info('{*}', { method: 'GET', url: '/api/v1/resource' });
221
+ * ```
222
+ *
223
+ * If the properties are expensive to compute, you cannot use this shorthand
224
+ * and should use the following syntax instead:
225
+ *
226
+ * ```typescript
227
+ * logger.info('{*}', () => ({
228
+ * method: expensiveMethod(),
229
+ * url: expensiveUrl(),
230
+ * }));
231
+ * ```
232
+ *
233
+ * @param properties The values to log. Note that this does not take
234
+ * a callback.
235
+ * @since 0.11.0
236
+ */
237
+ info(properties: Record<string, unknown>): void;
238
+
239
+ /**
240
+ * Lazily log an informational message. Use this when the message values are
241
+ * expensive to compute and should only be computed if the message is actually
242
+ * logged.
243
+ *
244
+ * ```typescript
245
+ * logger.info(l => l`An info message with ${expensiveValue()}.`);
246
+ * ```
247
+ *
248
+ * @param callback A callback that returns the message template prefix.
249
+ * @throws {TypeError} If no log record was made inside the callback.
250
+ */
251
+ info(callback: LogCallback): void;
252
+
253
+ /**
254
+ * Log a warning message. Use this as a template string prefix.
255
+ *
256
+ * ```typescript
257
+ * logger.warn `A warning message with ${value}.`;
258
+ * ```
259
+ *
260
+ * @param message The message template strings array.
261
+ * @param values The message template values.
262
+ */
263
+ warn(message: TemplateStringsArray, ...values: readonly unknown[]): void;
264
+
265
+ /**
266
+ * Log a warning message with properties.
267
+ *
268
+ * ```typescript
269
+ * logger.warn('A warning message with {value}.', { value });
270
+ * ```
271
+ *
272
+ * If the properties are expensive to compute, you can pass a callback that
273
+ * returns the properties:
274
+ *
275
+ * ```typescript
276
+ * logger.warn(
277
+ * 'A warning message with {value}.',
278
+ * () => ({ value: expensiveComputation() })
279
+ * );
280
+ * ```
281
+ *
282
+ * @param message The message template. Placeholders to be replaced with
283
+ * `values` are indicated by keys in curly braces (e.g.,
284
+ * `{value}`).
285
+ * @param properties The values to replace placeholders with. For lazy
286
+ * evaluation, this can be a callback that returns the
287
+ * properties.
288
+ */
289
+ warn(
290
+ message: string,
291
+ properties?: Record<string, unknown> | (() => Record<string, unknown>),
292
+ ): void;
293
+
294
+ /**
295
+ * Log a warning values with no message. This is useful when you
296
+ * want to log properties without a message, e.g., when you want to log
297
+ * the context of a request or an operation.
298
+ *
299
+ * ```typescript
300
+ * logger.warn({ method: 'GET', url: '/api/v1/resource' });
301
+ * ```
302
+ *
303
+ * Note that this is a shorthand for:
304
+ *
305
+ * ```typescript
306
+ * logger.warn('{*}', { method: 'GET', url: '/api/v1/resource' });
307
+ * ```
308
+ *
309
+ * If the properties are expensive to compute, you cannot use this shorthand
310
+ * and should use the following syntax instead:
311
+ *
312
+ * ```typescript
313
+ * logger.warn('{*}', () => ({
314
+ * method: expensiveMethod(),
315
+ * url: expensiveUrl(),
316
+ * }));
317
+ * ```
318
+ *
319
+ * @param properties The values to log. Note that this does not take
320
+ * a callback.
321
+ * @since 0.11.0
322
+ */
323
+ warn(properties: Record<string, unknown>): void;
324
+
325
+ /**
326
+ * Lazily log a warning message. Use this when the message values are
327
+ * expensive to compute and should only be computed if the message is actually
328
+ * logged.
329
+ *
330
+ * ```typescript
331
+ * logger.warn(l => l`A warning message with ${expensiveValue()}.`);
332
+ * ```
333
+ *
334
+ * @param callback A callback that returns the message template prefix.
335
+ * @throws {TypeError} If no log record was made inside the callback.
336
+ */
337
+ warn(callback: LogCallback): void;
338
+
339
+ /**
340
+ * Log an error message. Use this as a template string prefix.
341
+ *
342
+ * ```typescript
343
+ * logger.error `An error message with ${value}.`;
344
+ * ```
345
+ *
346
+ * @param message The message template strings array.
347
+ * @param values The message template values.
348
+ */
349
+ error(message: TemplateStringsArray, ...values: readonly unknown[]): void;
350
+
351
+ /**
352
+ * Log an error message with properties.
353
+ *
354
+ * ```typescript
355
+ * logger.warn('An error message with {value}.', { value });
356
+ * ```
357
+ *
358
+ * If the properties are expensive to compute, you can pass a callback that
359
+ * returns the properties:
360
+ *
361
+ * ```typescript
362
+ * logger.error(
363
+ * 'An error message with {value}.',
364
+ * () => ({ value: expensiveComputation() })
365
+ * );
366
+ * ```
367
+ *
368
+ * @param message The message template. Placeholders to be replaced with
369
+ * `values` are indicated by keys in curly braces (e.g.,
370
+ * `{value}`).
371
+ * @param properties The values to replace placeholders with. For lazy
372
+ * evaluation, this can be a callback that returns the
373
+ * properties.
374
+ */
375
+ error(
376
+ message: string,
377
+ properties?: Record<string, unknown> | (() => Record<string, unknown>),
378
+ ): void;
379
+
380
+ /**
381
+ * Log an error values with no message. This is useful when you
382
+ * want to log properties without a message, e.g., when you want to log
383
+ * the context of a request or an operation.
384
+ *
385
+ * ```typescript
386
+ * logger.error({ method: 'GET', url: '/api/v1/resource' });
387
+ * ```
388
+ *
389
+ * Note that this is a shorthand for:
390
+ *
391
+ * ```typescript
392
+ * logger.error('{*}', { method: 'GET', url: '/api/v1/resource' });
393
+ * ```
394
+ *
395
+ * If the properties are expensive to compute, you cannot use this shorthand
396
+ * and should use the following syntax instead:
397
+ *
398
+ * ```typescript
399
+ * logger.error('{*}', () => ({
400
+ * method: expensiveMethod(),
401
+ * url: expensiveUrl(),
402
+ * }));
403
+ * ```
404
+ *
405
+ * @param properties The values to log. Note that this does not take
406
+ * a callback.
407
+ * @since 0.11.0
408
+ */
409
+ error(properties: Record<string, unknown>): void;
410
+
411
+ /**
412
+ * Lazily log an error message. Use this when the message values are
413
+ * expensive to compute and should only be computed if the message is actually
414
+ * logged.
415
+ *
416
+ * ```typescript
417
+ * logger.error(l => l`An error message with ${expensiveValue()}.`);
418
+ * ```
419
+ *
420
+ * @param callback A callback that returns the message template prefix.
421
+ * @throws {TypeError} If no log record was made inside the callback.
422
+ */
423
+ error(callback: LogCallback): void;
424
+
425
+ /**
426
+ * Log a fatal error message. Use this as a template string prefix.
427
+ *
428
+ * ```typescript
429
+ * logger.fatal `A fatal error message with ${value}.`;
430
+ * ```
431
+ *
432
+ * @param message The message template strings array.
433
+ * @param values The message template values.
434
+ */
435
+ fatal(message: TemplateStringsArray, ...values: readonly unknown[]): void;
436
+
437
+ /**
438
+ * Log a fatal error message with properties.
439
+ *
440
+ * ```typescript
441
+ * logger.warn('A fatal error message with {value}.', { value });
442
+ * ```
443
+ *
444
+ * If the properties are expensive to compute, you can pass a callback that
445
+ * returns the properties:
446
+ *
447
+ * ```typescript
448
+ * logger.fatal(
449
+ * 'A fatal error message with {value}.',
450
+ * () => ({ value: expensiveComputation() })
451
+ * );
452
+ * ```
453
+ *
454
+ * @param message The message template. Placeholders to be replaced with
455
+ * `values` are indicated by keys in curly braces (e.g.,
456
+ * `{value}`).
457
+ * @param properties The values to replace placeholders with. For lazy
458
+ * evaluation, this can be a callback that returns the
459
+ * properties.
460
+ */
461
+ fatal(
462
+ message: string,
463
+ properties?: Record<string, unknown> | (() => Record<string, unknown>),
464
+ ): void;
465
+
466
+ /**
467
+ * Log a fatal error values with no message. This is useful when you
468
+ * want to log properties without a message, e.g., when you want to log
469
+ * the context of a request or an operation.
470
+ *
471
+ * ```typescript
472
+ * logger.fatal({ method: 'GET', url: '/api/v1/resource' });
473
+ * ```
474
+ *
475
+ * Note that this is a shorthand for:
476
+ *
477
+ * ```typescript
478
+ * logger.fatal('{*}', { method: 'GET', url: '/api/v1/resource' });
479
+ * ```
480
+ *
481
+ * If the properties are expensive to compute, you cannot use this shorthand
482
+ * and should use the following syntax instead:
483
+ *
484
+ * ```typescript
485
+ * logger.fatal('{*}', () => ({
486
+ * method: expensiveMethod(),
487
+ * url: expensiveUrl(),
488
+ * }));
489
+ * ```
490
+ *
491
+ * @param properties The values to log. Note that this does not take
492
+ * a callback.
493
+ * @since 0.11.0
494
+ */
495
+ fatal(properties: Record<string, unknown>): void;
496
+
497
+ /**
498
+ * Lazily log a fatal error message. Use this when the message values are
499
+ * expensive to compute and should only be computed if the message is actually
500
+ * logged.
501
+ *
502
+ * ```typescript
503
+ * logger.fatal(l => l`A fatal error message with ${expensiveValue()}.`);
504
+ * ```
505
+ *
506
+ * @param callback A callback that returns the message template prefix.
507
+ * @throws {TypeError} If no log record was made inside the callback.
508
+ */
509
+ fatal(callback: LogCallback): void;
510
+ }
511
+
512
+ /**
513
+ * A logging callback function. It is used to defer the computation of a
514
+ * message template until it is actually logged.
515
+ * @param prefix The message template prefix.
516
+ * @returns The rendered message array.
517
+ */
518
+ export type LogCallback = (prefix: LogTemplatePrefix) => unknown[];
519
+
520
+ /**
521
+ * A logging template prefix function. It is used to log a message in
522
+ * a {@link LogCallback} function.
523
+ * @param message The message template strings array.
524
+ * @param values The message template values.
525
+ * @returns The rendered message array.
526
+ */
527
+ export type LogTemplatePrefix = (
528
+ message: TemplateStringsArray,
529
+ ...values: unknown[]
530
+ ) => unknown[];
531
+
532
+ /**
533
+ * Get a logger with the given category.
534
+ *
535
+ * ```typescript
536
+ * const logger = getLogger(["my-app"]);
537
+ * ```
538
+ *
539
+ * @param category The category of the logger. It can be a string or an array
540
+ * of strings. If it is a string, it is equivalent to an array
541
+ * with a single element.
542
+ * @returns The logger.
543
+ */
544
+ export function getLogger(category: string | readonly string[] = []): Logger {
545
+ return LoggerImpl.getLogger(category);
546
+ }
547
+
548
+ /**
549
+ * The symbol for the global root logger.
550
+ */
551
+ const globalRootLoggerSymbol = Symbol.for("logtape.rootLogger");
552
+
553
+ /**
554
+ * The global root logger registry.
555
+ */
556
+ interface GlobalRootLoggerRegistry {
557
+ [globalRootLoggerSymbol]?: LoggerImpl;
558
+ }
559
+
560
+ /**
561
+ * A logger implementation. Do not use this directly; use {@link getLogger}
562
+ * instead. This class is exported for testing purposes.
563
+ */
564
+ export class LoggerImpl implements Logger {
565
+ readonly parent: LoggerImpl | null;
566
+ readonly children: Record<string, LoggerImpl | WeakRef<LoggerImpl>>;
567
+ readonly category: readonly string[];
568
+ readonly sinks: Sink[];
569
+ parentSinks: "inherit" | "override" = "inherit";
570
+ readonly filters: Filter[];
571
+ lowestLevel: LogLevel | null = "debug";
572
+ contextLocalStorage?: ContextLocalStorage<Record<string, unknown>>;
573
+
574
+ static getLogger(category: string | readonly string[] = []): LoggerImpl {
575
+ let rootLogger: LoggerImpl | null = globalRootLoggerSymbol in globalThis
576
+ ? ((globalThis as GlobalRootLoggerRegistry)[globalRootLoggerSymbol] ??
577
+ null)
578
+ : null;
579
+ if (rootLogger == null) {
580
+ rootLogger = new LoggerImpl(null, []);
581
+ (globalThis as GlobalRootLoggerRegistry)[globalRootLoggerSymbol] =
582
+ rootLogger;
583
+ }
584
+ if (typeof category === "string") return rootLogger.getChild(category);
585
+ if (category.length === 0) return rootLogger;
586
+ return rootLogger.getChild(category as readonly [string, ...string[]]);
587
+ }
588
+
589
+ private constructor(parent: LoggerImpl | null, category: readonly string[]) {
590
+ this.parent = parent;
591
+ this.children = {};
592
+ this.category = category;
593
+ this.sinks = [];
594
+ this.filters = [];
595
+ }
596
+
597
+ getChild(
598
+ subcategory:
599
+ | string
600
+ | readonly [string]
601
+ | readonly [string, ...(readonly string[])],
602
+ ): LoggerImpl {
603
+ const name = typeof subcategory === "string" ? subcategory : subcategory[0];
604
+ const childRef = this.children[name];
605
+ let child: LoggerImpl | undefined = childRef instanceof LoggerImpl
606
+ ? childRef
607
+ : childRef?.deref();
608
+ if (child == null) {
609
+ child = new LoggerImpl(this, [...this.category, name]);
610
+ this.children[name] = "WeakRef" in globalThis
611
+ ? new WeakRef(child)
612
+ : child;
613
+ }
614
+ if (typeof subcategory === "string" || subcategory.length === 1) {
615
+ return child;
616
+ }
617
+ return child.getChild(
618
+ subcategory.slice(1) as [string, ...(readonly string[])],
619
+ );
620
+ }
621
+
622
+ /**
623
+ * Reset the logger. This removes all sinks and filters from the logger.
624
+ */
625
+ reset(): void {
626
+ while (this.sinks.length > 0) this.sinks.shift();
627
+ this.parentSinks = "inherit";
628
+ while (this.filters.length > 0) this.filters.shift();
629
+ this.lowestLevel = "debug";
630
+ }
631
+
632
+ /**
633
+ * Reset the logger and all its descendants. This removes all sinks and
634
+ * filters from the logger and all its descendants.
635
+ */
636
+ resetDescendants(): void {
637
+ for (const child of Object.values(this.children)) {
638
+ const logger = child instanceof LoggerImpl ? child : child.deref();
639
+ if (logger != null) logger.resetDescendants();
640
+ }
641
+ this.reset();
642
+ }
643
+
644
+ with(properties: Record<string, unknown>): Logger {
645
+ return new LoggerCtx(this, { ...properties });
646
+ }
647
+
648
+ filter(record: LogRecord): boolean {
649
+ for (const filter of this.filters) {
650
+ if (!filter(record)) return false;
651
+ }
652
+ if (this.filters.length < 1) return this.parent?.filter(record) ?? true;
653
+ return true;
654
+ }
655
+
656
+ *getSinks(level: LogLevel): Iterable<Sink> {
657
+ if (
658
+ this.lowestLevel === null || compareLogLevel(level, this.lowestLevel) < 0
659
+ ) {
660
+ return;
661
+ }
662
+ if (this.parent != null && this.parentSinks === "inherit") {
663
+ for (const sink of this.parent.getSinks(level)) yield sink;
664
+ }
665
+ for (const sink of this.sinks) yield sink;
666
+ }
667
+
668
+ emit(record: LogRecord, bypassSinks?: Set<Sink>): void {
669
+ if (
670
+ this.lowestLevel === null ||
671
+ compareLogLevel(record.level, this.lowestLevel) < 0 ||
672
+ !this.filter(record)
673
+ ) {
674
+ return;
675
+ }
676
+ for (const sink of this.getSinks(record.level)) {
677
+ if (bypassSinks?.has(sink)) continue;
678
+ try {
679
+ sink(record);
680
+ } catch (error) {
681
+ const bypassSinks2 = new Set(bypassSinks);
682
+ bypassSinks2.add(sink);
683
+ metaLogger.log(
684
+ "fatal",
685
+ "Failed to emit a log record to sink {sink}: {error}",
686
+ { sink, error, record },
687
+ bypassSinks2,
688
+ );
689
+ }
690
+ }
691
+ }
692
+
693
+ log(
694
+ level: LogLevel,
695
+ rawMessage: string,
696
+ properties: Record<string, unknown> | (() => Record<string, unknown>),
697
+ bypassSinks?: Set<Sink>,
698
+ ): void {
699
+ const implicitContext =
700
+ LoggerImpl.getLogger().contextLocalStorage?.getStore() ?? {};
701
+ let cachedProps: Record<string, unknown> | undefined = undefined;
702
+ const record: LogRecord = typeof properties === "function"
703
+ ? {
704
+ category: this.category,
705
+ level,
706
+ timestamp: Date.now(),
707
+ get message() {
708
+ return parseMessageTemplate(rawMessage, this.properties);
709
+ },
710
+ rawMessage,
711
+ get properties() {
712
+ if (cachedProps == null) {
713
+ cachedProps = {
714
+ ...implicitContext,
715
+ ...properties(),
716
+ };
717
+ }
718
+ return cachedProps;
719
+ },
720
+ }
721
+ : {
722
+ category: this.category,
723
+ level,
724
+ timestamp: Date.now(),
725
+ message: parseMessageTemplate(rawMessage, {
726
+ ...implicitContext,
727
+ ...properties,
728
+ }),
729
+ rawMessage,
730
+ properties: { ...implicitContext, ...properties },
731
+ };
732
+ this.emit(record, bypassSinks);
733
+ }
734
+
735
+ logLazily(
736
+ level: LogLevel,
737
+ callback: LogCallback,
738
+ properties: Record<string, unknown> = {},
739
+ ): void {
740
+ const implicitContext =
741
+ LoggerImpl.getLogger().contextLocalStorage?.getStore() ?? {};
742
+ let rawMessage: TemplateStringsArray | undefined = undefined;
743
+ let msg: unknown[] | undefined = undefined;
744
+ function realizeMessage(): [unknown[], TemplateStringsArray] {
745
+ if (msg == null || rawMessage == null) {
746
+ msg = callback((tpl, ...values) => {
747
+ rawMessage = tpl;
748
+ return renderMessage(tpl, values);
749
+ });
750
+ if (rawMessage == null) throw new TypeError("No log record was made.");
751
+ }
752
+ return [msg, rawMessage];
753
+ }
754
+ this.emit({
755
+ category: this.category,
756
+ level,
757
+ get message() {
758
+ return realizeMessage()[0];
759
+ },
760
+ get rawMessage() {
761
+ return realizeMessage()[1];
762
+ },
763
+ timestamp: Date.now(),
764
+ properties: { ...implicitContext, ...properties },
765
+ });
766
+ }
767
+
768
+ logTemplate(
769
+ level: LogLevel,
770
+ messageTemplate: TemplateStringsArray,
771
+ values: unknown[],
772
+ properties: Record<string, unknown> = {},
773
+ ): void {
774
+ const implicitContext =
775
+ LoggerImpl.getLogger().contextLocalStorage?.getStore() ?? {};
776
+ this.emit({
777
+ category: this.category,
778
+ level,
779
+ message: renderMessage(messageTemplate, values),
780
+ rawMessage: messageTemplate,
781
+ timestamp: Date.now(),
782
+ properties: { ...implicitContext, ...properties },
783
+ });
784
+ }
785
+
786
+ debug(
787
+ message:
788
+ | TemplateStringsArray
789
+ | string
790
+ | LogCallback
791
+ | Record<string, unknown>,
792
+ ...values: unknown[]
793
+ ): void {
794
+ if (typeof message === "string") {
795
+ this.log("debug", message, (values[0] ?? {}) as Record<string, unknown>);
796
+ } else if (typeof message === "function") {
797
+ this.logLazily("debug", message);
798
+ } else if (!Array.isArray(message)) {
799
+ this.log("debug", "{*}", message as Record<string, unknown>);
800
+ } else {
801
+ this.logTemplate("debug", message as TemplateStringsArray, values);
802
+ }
803
+ }
804
+
805
+ info(
806
+ message:
807
+ | TemplateStringsArray
808
+ | string
809
+ | LogCallback
810
+ | Record<string, unknown>,
811
+ ...values: unknown[]
812
+ ): void {
813
+ if (typeof message === "string") {
814
+ this.log("info", message, (values[0] ?? {}) as Record<string, unknown>);
815
+ } else if (typeof message === "function") {
816
+ this.logLazily("info", message);
817
+ } else if (!Array.isArray(message)) {
818
+ this.log("info", "{*}", message as Record<string, unknown>);
819
+ } else {
820
+ this.logTemplate("info", message as TemplateStringsArray, values);
821
+ }
822
+ }
823
+
824
+ warn(
825
+ message:
826
+ | TemplateStringsArray
827
+ | string
828
+ | LogCallback
829
+ | Record<string, unknown>,
830
+ ...values: unknown[]
831
+ ): void {
832
+ if (typeof message === "string") {
833
+ this.log(
834
+ "warning",
835
+ message,
836
+ (values[0] ?? {}) as Record<string, unknown>,
837
+ );
838
+ } else if (typeof message === "function") {
839
+ this.logLazily("warning", message);
840
+ } else if (!Array.isArray(message)) {
841
+ this.log("warning", "{*}", message as Record<string, unknown>);
842
+ } else {
843
+ this.logTemplate("warning", message as TemplateStringsArray, values);
844
+ }
845
+ }
846
+
847
+ error(
848
+ message:
849
+ | TemplateStringsArray
850
+ | string
851
+ | LogCallback
852
+ | Record<string, unknown>,
853
+ ...values: unknown[]
854
+ ): void {
855
+ if (typeof message === "string") {
856
+ this.log("error", message, (values[0] ?? {}) as Record<string, unknown>);
857
+ } else if (typeof message === "function") {
858
+ this.logLazily("error", message);
859
+ } else if (!Array.isArray(message)) {
860
+ this.log("error", "{*}", message as Record<string, unknown>);
861
+ } else {
862
+ this.logTemplate("error", message as TemplateStringsArray, values);
863
+ }
864
+ }
865
+
866
+ fatal(
867
+ message:
868
+ | TemplateStringsArray
869
+ | string
870
+ | LogCallback
871
+ | Record<string, unknown>,
872
+ ...values: unknown[]
873
+ ): void {
874
+ if (typeof message === "string") {
875
+ this.log("fatal", message, (values[0] ?? {}) as Record<string, unknown>);
876
+ } else if (typeof message === "function") {
877
+ this.logLazily("fatal", message);
878
+ } else if (!Array.isArray(message)) {
879
+ this.log("fatal", "{*}", message as Record<string, unknown>);
880
+ } else {
881
+ this.logTemplate("fatal", message as TemplateStringsArray, values);
882
+ }
883
+ }
884
+ }
885
+
886
+ /**
887
+ * A logger implementation with contextual properties. Do not use this
888
+ * directly; use {@link Logger.with} instead. This class is exported
889
+ * for testing purposes.
890
+ */
891
+ export class LoggerCtx implements Logger {
892
+ logger: LoggerImpl;
893
+ properties: Record<string, unknown>;
894
+
895
+ constructor(logger: LoggerImpl, properties: Record<string, unknown>) {
896
+ this.logger = logger;
897
+ this.properties = properties;
898
+ }
899
+
900
+ get category(): readonly string[] {
901
+ return this.logger.category;
902
+ }
903
+
904
+ get parent(): Logger | null {
905
+ return this.logger.parent;
906
+ }
907
+
908
+ getChild(
909
+ subcategory: string | readonly [string] | readonly [string, ...string[]],
910
+ ): Logger {
911
+ return this.logger.getChild(subcategory).with(this.properties);
912
+ }
913
+
914
+ with(properties: Record<string, unknown>): Logger {
915
+ return new LoggerCtx(this.logger, { ...this.properties, ...properties });
916
+ }
917
+
918
+ log(
919
+ level: LogLevel,
920
+ message: string,
921
+ properties: Record<string, unknown> | (() => Record<string, unknown>),
922
+ bypassSinks?: Set<Sink>,
923
+ ): void {
924
+ this.logger.log(
925
+ level,
926
+ message,
927
+ typeof properties === "function"
928
+ ? () => ({
929
+ ...this.properties,
930
+ ...properties(),
931
+ })
932
+ : { ...this.properties, ...properties },
933
+ bypassSinks,
934
+ );
935
+ }
936
+
937
+ logLazily(level: LogLevel, callback: LogCallback): void {
938
+ this.logger.logLazily(level, callback, this.properties);
939
+ }
940
+
941
+ logTemplate(
942
+ level: LogLevel,
943
+ messageTemplate: TemplateStringsArray,
944
+ values: unknown[],
945
+ ): void {
946
+ this.logger.logTemplate(level, messageTemplate, values, this.properties);
947
+ }
948
+
949
+ debug(
950
+ message:
951
+ | TemplateStringsArray
952
+ | string
953
+ | LogCallback
954
+ | Record<string, unknown>,
955
+ ...values: unknown[]
956
+ ): void {
957
+ if (typeof message === "string") {
958
+ this.log("debug", message, (values[0] ?? {}) as Record<string, unknown>);
959
+ } else if (typeof message === "function") {
960
+ this.logLazily("debug", message);
961
+ } else if (!Array.isArray(message)) {
962
+ this.log("debug", "{*}", message as Record<string, unknown>);
963
+ } else {
964
+ this.logTemplate("debug", message as TemplateStringsArray, values);
965
+ }
966
+ }
967
+
968
+ info(
969
+ message:
970
+ | TemplateStringsArray
971
+ | string
972
+ | LogCallback
973
+ | Record<string, unknown>,
974
+ ...values: unknown[]
975
+ ): void {
976
+ if (typeof message === "string") {
977
+ this.log("info", message, (values[0] ?? {}) as Record<string, unknown>);
978
+ } else if (typeof message === "function") {
979
+ this.logLazily("info", message);
980
+ } else if (!Array.isArray(message)) {
981
+ this.log("info", "{*}", message as Record<string, unknown>);
982
+ } else {
983
+ this.logTemplate("info", message as TemplateStringsArray, values);
984
+ }
985
+ }
986
+
987
+ warn(
988
+ message:
989
+ | TemplateStringsArray
990
+ | string
991
+ | LogCallback
992
+ | Record<string, unknown>,
993
+ ...values: unknown[]
994
+ ): void {
995
+ if (typeof message === "string") {
996
+ this.log(
997
+ "warning",
998
+ message,
999
+ (values[0] ?? {}) as Record<string, unknown>,
1000
+ );
1001
+ } else if (typeof message === "function") {
1002
+ this.logLazily("warning", message);
1003
+ } else if (!Array.isArray(message)) {
1004
+ this.log("warning", "{*}", message as Record<string, unknown>);
1005
+ } else {
1006
+ this.logTemplate("warning", message as TemplateStringsArray, values);
1007
+ }
1008
+ }
1009
+
1010
+ error(
1011
+ message:
1012
+ | TemplateStringsArray
1013
+ | string
1014
+ | LogCallback
1015
+ | Record<string, unknown>,
1016
+ ...values: unknown[]
1017
+ ): void {
1018
+ if (typeof message === "string") {
1019
+ this.log("error", message, (values[0] ?? {}) as Record<string, unknown>);
1020
+ } else if (typeof message === "function") {
1021
+ this.logLazily("error", message);
1022
+ } else if (!Array.isArray(message)) {
1023
+ this.log("error", "{*}", message as Record<string, unknown>);
1024
+ } else {
1025
+ this.logTemplate("error", message as TemplateStringsArray, values);
1026
+ }
1027
+ }
1028
+
1029
+ fatal(
1030
+ message:
1031
+ | TemplateStringsArray
1032
+ | string
1033
+ | LogCallback
1034
+ | Record<string, unknown>,
1035
+ ...values: unknown[]
1036
+ ): void {
1037
+ if (typeof message === "string") {
1038
+ this.log("fatal", message, (values[0] ?? {}) as Record<string, unknown>);
1039
+ } else if (typeof message === "function") {
1040
+ this.logLazily("fatal", message);
1041
+ } else if (!Array.isArray(message)) {
1042
+ this.log("fatal", "{*}", message as Record<string, unknown>);
1043
+ } else {
1044
+ this.logTemplate("fatal", message as TemplateStringsArray, values);
1045
+ }
1046
+ }
1047
+ }
1048
+
1049
+ /**
1050
+ * The meta logger. It is a logger with the category `["logtape", "meta"]`.
1051
+ */
1052
+ const metaLogger = LoggerImpl.getLogger(["logtape", "meta"]);
1053
+
1054
+ /**
1055
+ * Parse a message template into a message template array and a values array.
1056
+ * @param template The message template.
1057
+ * @param properties The values to replace placeholders with.
1058
+ * @returns The message template array and the values array.
1059
+ */
1060
+ export function parseMessageTemplate(
1061
+ template: string,
1062
+ properties: Record<string, unknown>,
1063
+ ): readonly unknown[] {
1064
+ const message: unknown[] = [];
1065
+ let part = "";
1066
+ for (let i = 0; i < template.length; i++) {
1067
+ const char = template.charAt(i);
1068
+ const nextChar = template.charAt(i + 1);
1069
+
1070
+ if (char === "{" && nextChar === "{") {
1071
+ // Escaped { character
1072
+ part = part + char;
1073
+ i++;
1074
+ } else if (char === "}" && nextChar === "}") {
1075
+ // Escaped } character
1076
+ part = part + char;
1077
+ i++;
1078
+ } else if (char === "{") {
1079
+ // Start of a placeholder
1080
+ message.push(part);
1081
+ part = "";
1082
+ } else if (char === "}") {
1083
+ // End of a placeholder
1084
+ let prop: unknown;
1085
+ if (part.match(/^\s*\*\s*$/)) {
1086
+ prop = part in properties
1087
+ ? properties[part]
1088
+ : "*" in properties
1089
+ ? properties["*"]
1090
+ : properties;
1091
+ } else if (part.match(/^\s|\s$/)) {
1092
+ prop = part in properties ? properties[part] : properties[part.trim()];
1093
+ } else {
1094
+ prop = properties[part];
1095
+ }
1096
+ message.push(prop);
1097
+ part = "";
1098
+ } else {
1099
+ // Default case
1100
+ part = part + char;
1101
+ }
1102
+ }
1103
+ message.push(part);
1104
+ return message;
1105
+ }
1106
+
1107
+ /**
1108
+ * Render a message template with values.
1109
+ * @param template The message template.
1110
+ * @param values The message template values.
1111
+ * @returns The message template values interleaved between the substitution
1112
+ * values.
1113
+ */
1114
+ export function renderMessage(
1115
+ template: TemplateStringsArray,
1116
+ values: readonly unknown[],
1117
+ ): unknown[] {
1118
+ const args = [];
1119
+ for (let i = 0; i < template.length; i++) {
1120
+ args.push(template[i]);
1121
+ if (i < values.length) args.push(values[i]);
1122
+ }
1123
+ return args;
1124
+ }