@logtape/logtape 1.1.3 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/formatter.ts DELETED
@@ -1,933 +0,0 @@
1
- import * as util from "#util";
2
- import type { LogLevel } from "./level.ts";
3
- import type { LogRecord } from "./record.ts";
4
-
5
- /**
6
- * A text formatter is a function that accepts a log record and returns
7
- * a string.
8
- *
9
- * @param record The log record to format.
10
- * @returns The formatted log record.
11
- */
12
- export type TextFormatter = (record: LogRecord) => string;
13
-
14
- /**
15
- * The severity level abbreviations.
16
- */
17
- const levelAbbreviations: Record<LogLevel, string> = {
18
- "trace": "TRC",
19
- "debug": "DBG",
20
- "info": "INF",
21
- "warning": "WRN",
22
- "error": "ERR",
23
- "fatal": "FTL",
24
- };
25
-
26
- /**
27
- * A platform-specific inspect function. In Deno, this is {@link Deno.inspect},
28
- * and in Node.js/Bun it is `util.inspect()`. If neither is available, it
29
- * falls back to {@link JSON.stringify}.
30
- *
31
- * @param value The value to inspect.
32
- * @param options The options for inspecting the value.
33
- * If `colors` is `true`, the output will be ANSI-colored.
34
- * @returns The string representation of the value.
35
- */
36
- const inspect: (value: unknown, options?: { colors?: boolean }) => string =
37
- // @ts-ignore: Browser detection
38
- // dnt-shim-ignore
39
- typeof document !== "undefined" ||
40
- // @ts-ignore: React Native detection
41
- // dnt-shim-ignore
42
- typeof navigator !== "undefined" && navigator.product === "ReactNative"
43
- ? (v) => JSON.stringify(v)
44
- // @ts-ignore: Deno global
45
- // dnt-shim-ignore
46
- : "Deno" in globalThis && "inspect" in globalThis.Deno &&
47
- // @ts-ignore: Deno global
48
- // dnt-shim-ignore
49
- typeof globalThis.Deno.inspect === "function"
50
- ? (v, opts) =>
51
- // @ts-ignore: Deno global
52
- // dnt-shim-ignore
53
- globalThis.Deno.inspect(v, {
54
- strAbbreviateSize: Infinity,
55
- iterableLimit: Infinity,
56
- ...opts,
57
- })
58
- // @ts-ignore: Node.js global
59
- // dnt-shim-ignore
60
- : util != null && "inspect" in util && typeof util.inspect === "function"
61
- ? (v, opts) =>
62
- // @ts-ignore: Node.js global
63
- // dnt-shim-ignore
64
- util.inspect(v, {
65
- maxArrayLength: Infinity,
66
- maxStringLength: Infinity,
67
- ...opts,
68
- })
69
- : (v) => JSON.stringify(v);
70
-
71
- /**
72
- * The formatted values for a log record.
73
- * @since 0.6.0
74
- */
75
- export interface FormattedValues {
76
- /**
77
- * The formatted timestamp.
78
- */
79
- timestamp: string | null;
80
-
81
- /**
82
- * The formatted log level.
83
- */
84
- level: string;
85
-
86
- /**
87
- * The formatted category.
88
- */
89
- category: string;
90
-
91
- /**
92
- * The formatted message.
93
- */
94
- message: string;
95
-
96
- /**
97
- * The unformatted log record.
98
- */
99
- record: LogRecord;
100
- }
101
-
102
- /**
103
- * The various options for the built-in text formatters.
104
- * @since 0.6.0
105
- */
106
- export interface TextFormatterOptions {
107
- /**
108
- * The timestamp format. This can be one of the following:
109
- *
110
- * - `"date-time-timezone"`: The date and time with the full timezone offset
111
- * (e.g., `"2023-11-14 22:13:20.000 +00:00"`).
112
- * - `"date-time-tz"`: The date and time with the short timezone offset
113
- * (e.g., `"2023-11-14 22:13:20.000 +00"`).
114
- * - `"date-time"`: The date and time without the timezone offset
115
- * (e.g., `"2023-11-14 22:13:20.000"`).
116
- * - `"time-timezone"`: The time with the full timezone offset but without
117
- * the date (e.g., `"22:13:20.000 +00:00"`).
118
- * - `"time-tz"`: The time with the short timezone offset but without the date
119
- * (e.g., `"22:13:20.000 +00"`).
120
- * - `"time"`: The time without the date or timezone offset
121
- * (e.g., `"22:13:20.000"`).
122
- * - `"date"`: The date without the time or timezone offset
123
- * (e.g., `"2023-11-14"`).
124
- * - `"rfc3339"`: The date and time in RFC 3339 format
125
- * (e.g., `"2023-11-14T22:13:20.000Z"`).
126
- * - `"none"` or `"disabled"`: No display
127
- *
128
- * Alternatively, this can be a function that accepts a timestamp and returns
129
- * a string.
130
- *
131
- * The default is `"date-time-timezone"`.
132
- */
133
- timestamp?:
134
- | "date-time-timezone"
135
- | "date-time-tz"
136
- | "date-time"
137
- | "time-timezone"
138
- | "time-tz"
139
- | "time"
140
- | "date"
141
- | "rfc3339"
142
- | "none"
143
- | "disabled"
144
- | ((ts: number) => string | null);
145
-
146
- /**
147
- * The log level format. This can be one of the following:
148
- *
149
- * - `"ABBR"`: The log level abbreviation in uppercase (e.g., `"INF"`).
150
- * - `"FULL"`: The full log level name in uppercase (e.g., `"INFO"`).
151
- * - `"L"`: The first letter of the log level in uppercase (e.g., `"I"`).
152
- * - `"abbr"`: The log level abbreviation in lowercase (e.g., `"inf"`).
153
- * - `"full"`: The full log level name in lowercase (e.g., `"info"`).
154
- * - `"l"`: The first letter of the log level in lowercase (e.g., `"i"`).
155
- *
156
- * Alternatively, this can be a function that accepts a log level and returns
157
- * a string.
158
- *
159
- * The default is `"ABBR"`.
160
- */
161
- level?:
162
- | "ABBR"
163
- | "FULL"
164
- | "L"
165
- | "abbr"
166
- | "full"
167
- | "l"
168
- | ((level: LogLevel) => string);
169
-
170
- /**
171
- * The separator between category names. For example, if the separator is
172
- * `"·"`, the category `["a", "b", "c"]` will be formatted as `"a·b·c"`.
173
- * The default separator is `"·"`.
174
- *
175
- * If this is a function, it will be called with the category array and
176
- * should return a string, which will be used for rendering the category.
177
- */
178
- category?: string | ((category: readonly string[]) => string);
179
-
180
- /**
181
- * The format of the embedded values.
182
- *
183
- * A function that renders a value to a string. This function is used to
184
- * render the values in the log record. The default is [`util.inspect()`] in
185
- * Node.js/Bun and [`Deno.inspect()`] in Deno.
186
- *
187
- * [`util.inspect()`]: https://nodejs.org/api/util.html#utilinspectobject-options
188
- * [`Deno.inspect()`]: https://docs.deno.com/api/deno/~/Deno.inspect
189
- * @param value The value to render.
190
- * @returns The string representation of the value.
191
- */
192
- value?: (value: unknown) => string;
193
-
194
- /**
195
- * How those formatted parts are concatenated.
196
- *
197
- * A function that formats the log record. This function is called with the
198
- * formatted values and should return a string. Note that the formatted
199
- * *should not* include a newline character at the end.
200
- *
201
- * By default, this is a function that formats the log record as follows:
202
- *
203
- * ```
204
- * 2023-11-14 22:13:20.000 +00:00 [INF] category·subcategory: Hello, world!
205
- * ```
206
- * @param values The formatted values.
207
- * @returns The formatted log record.
208
- */
209
- format?: (values: FormattedValues) => string;
210
- }
211
-
212
- // Optimized helper functions for timestamp formatting
213
- function padZero(num: number): string {
214
- return num < 10 ? `0${num}` : `${num}`;
215
- }
216
-
217
- function padThree(num: number): string {
218
- return num < 10 ? `00${num}` : num < 100 ? `0${num}` : `${num}`;
219
- }
220
-
221
- // Pre-optimized timestamp formatter functions
222
- const timestampFormatters = {
223
- "date-time-timezone": (ts: number): string => {
224
- const d = new Date(ts);
225
- const year = d.getUTCFullYear();
226
- const month = padZero(d.getUTCMonth() + 1);
227
- const day = padZero(d.getUTCDate());
228
- const hour = padZero(d.getUTCHours());
229
- const minute = padZero(d.getUTCMinutes());
230
- const second = padZero(d.getUTCSeconds());
231
- const ms = padThree(d.getUTCMilliseconds());
232
- return `${year}-${month}-${day} ${hour}:${minute}:${second}.${ms} +00:00`;
233
- },
234
- "date-time-tz": (ts: number): string => {
235
- const d = new Date(ts);
236
- const year = d.getUTCFullYear();
237
- const month = padZero(d.getUTCMonth() + 1);
238
- const day = padZero(d.getUTCDate());
239
- const hour = padZero(d.getUTCHours());
240
- const minute = padZero(d.getUTCMinutes());
241
- const second = padZero(d.getUTCSeconds());
242
- const ms = padThree(d.getUTCMilliseconds());
243
- return `${year}-${month}-${day} ${hour}:${minute}:${second}.${ms} +00`;
244
- },
245
- "date-time": (ts: number): string => {
246
- const d = new Date(ts);
247
- const year = d.getUTCFullYear();
248
- const month = padZero(d.getUTCMonth() + 1);
249
- const day = padZero(d.getUTCDate());
250
- const hour = padZero(d.getUTCHours());
251
- const minute = padZero(d.getUTCMinutes());
252
- const second = padZero(d.getUTCSeconds());
253
- const ms = padThree(d.getUTCMilliseconds());
254
- return `${year}-${month}-${day} ${hour}:${minute}:${second}.${ms}`;
255
- },
256
- "time-timezone": (ts: number): string => {
257
- const d = new Date(ts);
258
- const hour = padZero(d.getUTCHours());
259
- const minute = padZero(d.getUTCMinutes());
260
- const second = padZero(d.getUTCSeconds());
261
- const ms = padThree(d.getUTCMilliseconds());
262
- return `${hour}:${minute}:${second}.${ms} +00:00`;
263
- },
264
- "time-tz": (ts: number): string => {
265
- const d = new Date(ts);
266
- const hour = padZero(d.getUTCHours());
267
- const minute = padZero(d.getUTCMinutes());
268
- const second = padZero(d.getUTCSeconds());
269
- const ms = padThree(d.getUTCMilliseconds());
270
- return `${hour}:${minute}:${second}.${ms} +00`;
271
- },
272
- "time": (ts: number): string => {
273
- const d = new Date(ts);
274
- const hour = padZero(d.getUTCHours());
275
- const minute = padZero(d.getUTCMinutes());
276
- const second = padZero(d.getUTCSeconds());
277
- const ms = padThree(d.getUTCMilliseconds());
278
- return `${hour}:${minute}:${second}.${ms}`;
279
- },
280
- "date": (ts: number): string => {
281
- const d = new Date(ts);
282
- const year = d.getUTCFullYear();
283
- const month = padZero(d.getUTCMonth() + 1);
284
- const day = padZero(d.getUTCDate());
285
- return `${year}-${month}-${day}`;
286
- },
287
- "rfc3339": (ts: number): string => new Date(ts).toISOString(),
288
- "none": (): null => null,
289
- } as const;
290
-
291
- // Pre-computed level renderers for common cases
292
- const levelRenderersCache = {
293
- ABBR: levelAbbreviations,
294
- abbr: {
295
- trace: "trc",
296
- debug: "dbg",
297
- info: "inf",
298
- warning: "wrn",
299
- error: "err",
300
- fatal: "ftl",
301
- } as const,
302
- FULL: {
303
- trace: "TRACE",
304
- debug: "DEBUG",
305
- info: "INFO",
306
- warning: "WARNING",
307
- error: "ERROR",
308
- fatal: "FATAL",
309
- } as const,
310
- full: {
311
- trace: "trace",
312
- debug: "debug",
313
- info: "info",
314
- warning: "warning",
315
- error: "error",
316
- fatal: "fatal",
317
- } as const,
318
- L: {
319
- trace: "T",
320
- debug: "D",
321
- info: "I",
322
- warning: "W",
323
- error: "E",
324
- fatal: "F",
325
- } as const,
326
- l: {
327
- trace: "t",
328
- debug: "d",
329
- info: "i",
330
- warning: "w",
331
- error: "e",
332
- fatal: "f",
333
- } as const,
334
- } as const;
335
-
336
- /**
337
- * Get a text formatter with the specified options. Although it's flexible
338
- * enough to create a custom formatter, if you want more control, you can
339
- * create a custom formatter that satisfies the {@link TextFormatter} type
340
- * instead.
341
- *
342
- * For more information on the options, see {@link TextFormatterOptions}.
343
- *
344
- * By default, the formatter formats log records as follows:
345
- *
346
- * ```
347
- * 2023-11-14 22:13:20.000 +00:00 [INF] category·subcategory: Hello, world!
348
- * ```
349
- * @param options The options for the text formatter.
350
- * @returns The text formatter.
351
- * @since 0.6.0
352
- */
353
- export function getTextFormatter(
354
- options: TextFormatterOptions = {},
355
- ): TextFormatter {
356
- // Pre-compute timestamp formatter with optimized lookup
357
- const timestampRenderer = (() => {
358
- const tsOption = options.timestamp;
359
- if (tsOption == null) {
360
- return timestampFormatters["date-time-timezone"];
361
- } else if (tsOption === "disabled") {
362
- return timestampFormatters["none"];
363
- } else if (
364
- typeof tsOption === "string" && tsOption in timestampFormatters
365
- ) {
366
- return timestampFormatters[tsOption as keyof typeof timestampFormatters];
367
- } else {
368
- return tsOption as (ts: number) => string | null;
369
- }
370
- })();
371
-
372
- const categorySeparator = options.category ?? "·";
373
- const valueRenderer = options.value ?? inspect;
374
-
375
- // Pre-compute level renderer for better performance
376
- const levelRenderer = (() => {
377
- const levelOption = options.level;
378
- if (levelOption == null || levelOption === "ABBR") {
379
- return (level: LogLevel): string => levelRenderersCache.ABBR[level];
380
- } else if (levelOption === "abbr") {
381
- return (level: LogLevel): string => levelRenderersCache.abbr[level];
382
- } else if (levelOption === "FULL") {
383
- return (level: LogLevel): string => levelRenderersCache.FULL[level];
384
- } else if (levelOption === "full") {
385
- return (level: LogLevel): string => levelRenderersCache.full[level];
386
- } else if (levelOption === "L") {
387
- return (level: LogLevel): string => levelRenderersCache.L[level];
388
- } else if (levelOption === "l") {
389
- return (level: LogLevel): string => levelRenderersCache.l[level];
390
- } else {
391
- return levelOption;
392
- }
393
- })();
394
-
395
- const formatter: (values: FormattedValues) => string = options.format ??
396
- (({ timestamp, level, category, message }: FormattedValues) =>
397
- `${timestamp ? `${timestamp} ` : ""}[${level}] ${category}: ${message}`);
398
-
399
- return (record: LogRecord): string => {
400
- // Optimized message building
401
- const msgParts = record.message;
402
- const msgLen = msgParts.length;
403
-
404
- let message: string;
405
- if (msgLen === 1) {
406
- // Fast path for simple messages with no interpolation
407
- message = msgParts[0] as string;
408
- } else if (msgLen <= 6) {
409
- // Fast path for small messages - direct concatenation
410
- message = "";
411
- for (let i = 0; i < msgLen; i++) {
412
- message += (i % 2 === 0) ? msgParts[i] : valueRenderer(msgParts[i]);
413
- }
414
- } else {
415
- // Optimized path for larger messages - array join
416
- const parts: string[] = new Array(msgLen);
417
- for (let i = 0; i < msgLen; i++) {
418
- parts[i] = (i % 2 === 0)
419
- ? msgParts[i] as string
420
- : valueRenderer(msgParts[i]);
421
- }
422
- message = parts.join("");
423
- }
424
-
425
- const timestamp = timestampRenderer(record.timestamp);
426
- const level = levelRenderer(record.level);
427
- const category = typeof categorySeparator === "function"
428
- ? categorySeparator(record.category)
429
- : record.category.join(categorySeparator);
430
-
431
- const values: FormattedValues = {
432
- timestamp,
433
- level,
434
- category,
435
- message,
436
- record,
437
- };
438
- return `${formatter(values)}\n`;
439
- };
440
- }
441
-
442
- /**
443
- * The default text formatter. This formatter formats log records as follows:
444
- *
445
- * ```
446
- * 2023-11-14 22:13:20.000 +00:00 [INF] category·subcategory: Hello, world!
447
- * ```
448
- *
449
- * @param record The log record to format.
450
- * @returns The formatted log record.
451
- */
452
- export const defaultTextFormatter: TextFormatter = getTextFormatter();
453
-
454
- const RESET = "\x1b[0m";
455
-
456
- /**
457
- * The ANSI colors. These can be used to colorize text in the console.
458
- * @since 0.6.0
459
- */
460
- export type AnsiColor =
461
- | "black"
462
- | "red"
463
- | "green"
464
- | "yellow"
465
- | "blue"
466
- | "magenta"
467
- | "cyan"
468
- | "white";
469
-
470
- const ansiColors: Record<AnsiColor, string> = {
471
- black: "\x1b[30m",
472
- red: "\x1b[31m",
473
- green: "\x1b[32m",
474
- yellow: "\x1b[33m",
475
- blue: "\x1b[34m",
476
- magenta: "\x1b[35m",
477
- cyan: "\x1b[36m",
478
- white: "\x1b[37m",
479
- };
480
-
481
- /**
482
- * The ANSI text styles.
483
- * @since 0.6.0
484
- */
485
- export type AnsiStyle =
486
- | "bold"
487
- | "dim"
488
- | "italic"
489
- | "underline"
490
- | "strikethrough";
491
-
492
- const ansiStyles: Record<AnsiStyle, string> = {
493
- bold: "\x1b[1m",
494
- dim: "\x1b[2m",
495
- italic: "\x1b[3m",
496
- underline: "\x1b[4m",
497
- strikethrough: "\x1b[9m",
498
- };
499
-
500
- const defaultLevelColors: Record<LogLevel, AnsiColor | null> = {
501
- trace: null,
502
- debug: "blue",
503
- info: "green",
504
- warning: "yellow",
505
- error: "red",
506
- fatal: "magenta",
507
- };
508
-
509
- /**
510
- * The various options for the ANSI color formatter.
511
- * @since 0.6.0
512
- */
513
- export interface AnsiColorFormatterOptions extends TextFormatterOptions {
514
- /**
515
- * The timestamp format. This can be one of the following:
516
- *
517
- * - `"date-time-timezone"`: The date and time with the full timezone offset
518
- * (e.g., `"2023-11-14 22:13:20.000 +00:00"`).
519
- * - `"date-time-tz"`: The date and time with the short timezone offset
520
- * (e.g., `"2023-11-14 22:13:20.000 +00"`).
521
- * - `"date-time"`: The date and time without the timezone offset
522
- * (e.g., `"2023-11-14 22:13:20.000"`).
523
- * - `"time-timezone"`: The time with the full timezone offset but without
524
- * the date (e.g., `"22:13:20.000 +00:00"`).
525
- * - `"time-tz"`: The time with the short timezone offset but without the date
526
- * (e.g., `"22:13:20.000 +00"`).
527
- * - `"time"`: The time without the date or timezone offset
528
- * (e.g., `"22:13:20.000"`).
529
- * - `"date"`: The date without the time or timezone offset
530
- * (e.g., `"2023-11-14"`).
531
- * - `"rfc3339"`: The date and time in RFC 3339 format
532
- * (e.g., `"2023-11-14T22:13:20.000Z"`).
533
- *
534
- * Alternatively, this can be a function that accepts a timestamp and returns
535
- * a string.
536
- *
537
- * The default is `"date-time-tz"`.
538
- */
539
- timestamp?:
540
- | "date-time-timezone"
541
- | "date-time-tz"
542
- | "date-time"
543
- | "time-timezone"
544
- | "time-tz"
545
- | "time"
546
- | "date"
547
- | "rfc3339"
548
- | ((ts: number) => string);
549
-
550
- /**
551
- * The ANSI style for the timestamp. `"dim"` is used by default.
552
- */
553
- timestampStyle?: AnsiStyle | null;
554
-
555
- /**
556
- * The ANSI color for the timestamp. No color is used by default.
557
- */
558
- timestampColor?: AnsiColor | null;
559
-
560
- /**
561
- * The ANSI style for the log level. `"bold"` is used by default.
562
- */
563
- levelStyle?: AnsiStyle | null;
564
-
565
- /**
566
- * The ANSI colors for the log levels. The default colors are as follows:
567
- *
568
- * - `"trace"`: `null` (no color)
569
- * - `"debug"`: `"blue"`
570
- * - `"info"`: `"green"`
571
- * - `"warning"`: `"yellow"`
572
- * - `"error"`: `"red"`
573
- * - `"fatal"`: `"magenta"`
574
- */
575
- levelColors?: Record<LogLevel, AnsiColor | null>;
576
-
577
- /**
578
- * The ANSI style for the category. `"dim"` is used by default.
579
- */
580
- categoryStyle?: AnsiStyle | null;
581
-
582
- /**
583
- * The ANSI color for the category. No color is used by default.
584
- */
585
- categoryColor?: AnsiColor | null;
586
- }
587
-
588
- /**
589
- * Get an ANSI color formatter with the specified options.
590
- *
591
- * ![A preview of an ANSI color formatter.](https://i.imgur.com/I8LlBUf.png)
592
- * @param option The options for the ANSI color formatter.
593
- * @returns The ANSI color formatter.
594
- * @since 0.6.0
595
- */
596
- export function getAnsiColorFormatter(
597
- options: AnsiColorFormatterOptions = {},
598
- ): TextFormatter {
599
- const format = options.format;
600
- const timestampStyle = typeof options.timestampStyle === "undefined"
601
- ? "dim"
602
- : options.timestampStyle;
603
- const timestampColor = options.timestampColor ?? null;
604
- const timestampPrefix = `${
605
- timestampStyle == null ? "" : ansiStyles[timestampStyle]
606
- }${timestampColor == null ? "" : ansiColors[timestampColor]}`;
607
- const timestampSuffix = timestampStyle == null && timestampColor == null
608
- ? ""
609
- : RESET;
610
- const levelStyle = typeof options.levelStyle === "undefined"
611
- ? "bold"
612
- : options.levelStyle;
613
- const levelColors = options.levelColors ?? defaultLevelColors;
614
- const categoryStyle = typeof options.categoryStyle === "undefined"
615
- ? "dim"
616
- : options.categoryStyle;
617
- const categoryColor = options.categoryColor ?? null;
618
- const categoryPrefix = `${
619
- categoryStyle == null ? "" : ansiStyles[categoryStyle]
620
- }${categoryColor == null ? "" : ansiColors[categoryColor]}`;
621
- const categorySuffix = categoryStyle == null && categoryColor == null
622
- ? ""
623
- : RESET;
624
- return getTextFormatter({
625
- timestamp: "date-time-tz",
626
- value(value: unknown): string {
627
- return inspect(value, { colors: true });
628
- },
629
- ...options,
630
- format({ timestamp, level, category, message, record }): string {
631
- const levelColor = levelColors[record.level];
632
- timestamp = `${timestampPrefix}${timestamp}${timestampSuffix}`;
633
- level = `${levelStyle == null ? "" : ansiStyles[levelStyle]}${
634
- levelColor == null ? "" : ansiColors[levelColor]
635
- }${level}${levelStyle == null && levelColor == null ? "" : RESET}`;
636
- return format == null
637
- ? `${timestamp} ${level} ${categoryPrefix}${category}:${categorySuffix} ${message}`
638
- : format({
639
- timestamp,
640
- level,
641
- category: `${categoryPrefix}${category}${categorySuffix}`,
642
- message,
643
- record,
644
- });
645
- },
646
- });
647
- }
648
-
649
- /**
650
- * A text formatter that uses ANSI colors to format log records.
651
- *
652
- * ![A preview of ansiColorFormatter.](https://i.imgur.com/I8LlBUf.png)
653
- *
654
- * @param record The log record to format.
655
- * @returns The formatted log record.
656
- * @since 0.5.0
657
- */
658
- export const ansiColorFormatter: TextFormatter = getAnsiColorFormatter();
659
-
660
- /**
661
- * Options for the {@link getJsonLinesFormatter} function.
662
- * @since 0.11.0
663
- */
664
- export interface JsonLinesFormatterOptions {
665
- /**
666
- * The separator between category names. For example, if the separator is
667
- * `"."`, the category `["a", "b", "c"]` will be formatted as `"a.b.c"`.
668
- * If this is a function, it will be called with the category array and
669
- * should return a string or an array of strings, which will be used
670
- * for rendering the category.
671
- *
672
- * @default `"."`
673
- */
674
- readonly categorySeparator?:
675
- | string
676
- | ((category: readonly string[]) => string | readonly string[]);
677
-
678
- /**
679
- * The message format. This can be one of the following:
680
- *
681
- * - `"template"`: The raw message template is used as the message.
682
- * - `"rendered"`: The message is rendered with the values.
683
- *
684
- * @default `"rendered"`
685
- */
686
- readonly message?: "template" | "rendered";
687
-
688
- /**
689
- * The properties format. This can be one of the following:
690
- *
691
- * - `"flatten"`: The properties are flattened into the root object.
692
- * - `"prepend:<prefix>"`: The properties are prepended with the given prefix
693
- * (e.g., `"prepend:ctx_"` will prepend `ctx_` to each property key).
694
- * - `"nest:<key>"`: The properties are nested under the given key
695
- * (e.g., `"nest:properties"` will nest the properties under the
696
- * `properties` key).
697
- *
698
- * @default `"nest:properties"`
699
- */
700
- readonly properties?: "flatten" | `prepend:${string}` | `nest:${string}`;
701
- }
702
-
703
- /**
704
- * Get a [JSON Lines] formatter with the specified options. The log records
705
- * will be rendered as JSON objects, one per line, which is a common format
706
- * for log files. This format is also known as Newline-Delimited JSON (NDJSON).
707
- * It looks like this:
708
- *
709
- * ```json
710
- * {"@timestamp":"2023-11-14T22:13:20.000Z","level":"INFO","message":"Hello, world!","logger":"my.logger","properties":{"key":"value"}}
711
- * ```
712
- *
713
- * [JSON Lines]: https://jsonlines.org/
714
- * @param options The options for the JSON Lines formatter.
715
- * @returns The JSON Lines formatter.
716
- * @since 0.11.0
717
- */
718
- export function getJsonLinesFormatter(
719
- options: JsonLinesFormatterOptions = {},
720
- ): TextFormatter {
721
- // Most common configuration - optimize for the default case
722
- if (!options.categorySeparator && !options.message && !options.properties) {
723
- // Ultra-minimalist path - eliminate all possible overhead
724
- return (record: LogRecord): string => {
725
- // Direct benchmark pattern match (most common case first)
726
- if (record.message.length === 3) {
727
- return JSON.stringify({
728
- "@timestamp": new Date(record.timestamp).toISOString(),
729
- level: record.level === "warning"
730
- ? "WARN"
731
- : record.level.toUpperCase(),
732
- message: record.message[0] + JSON.stringify(record.message[1]) +
733
- record.message[2],
734
- logger: record.category.join("."),
735
- properties: record.properties,
736
- }) + "\n";
737
- }
738
-
739
- // Single message (second most common)
740
- if (record.message.length === 1) {
741
- return JSON.stringify({
742
- "@timestamp": new Date(record.timestamp).toISOString(),
743
- level: record.level === "warning"
744
- ? "WARN"
745
- : record.level.toUpperCase(),
746
- message: record.message[0],
747
- logger: record.category.join("."),
748
- properties: record.properties,
749
- }) + "\n";
750
- }
751
-
752
- // Complex messages (fallback)
753
- let msg = record.message[0] as string;
754
- for (let i = 1; i < record.message.length; i++) {
755
- msg += (i & 1) ? JSON.stringify(record.message[i]) : record.message[i];
756
- }
757
-
758
- return JSON.stringify({
759
- "@timestamp": new Date(record.timestamp).toISOString(),
760
- level: record.level === "warning" ? "WARN" : record.level.toUpperCase(),
761
- message: msg,
762
- logger: record.category.join("."),
763
- properties: record.properties,
764
- }) + "\n";
765
- };
766
- }
767
-
768
- // Pre-compile configuration for non-default cases
769
- const isTemplateMessage = options.message === "template";
770
- const propertiesOption = options.properties ?? "nest:properties";
771
-
772
- // Pre-compile category joining strategy
773
- let joinCategory: (category: readonly string[]) => string | readonly string[];
774
- if (typeof options.categorySeparator === "function") {
775
- joinCategory = options.categorySeparator;
776
- } else {
777
- const separator = options.categorySeparator ?? ".";
778
- joinCategory = (category: readonly string[]): string =>
779
- category.join(separator);
780
- }
781
-
782
- // Pre-compile properties handling strategy
783
- let getProperties: (
784
- properties: Record<string, unknown>,
785
- ) => Record<string, unknown>;
786
-
787
- if (propertiesOption === "flatten") {
788
- getProperties = (properties) => properties;
789
- } else if (propertiesOption.startsWith("prepend:")) {
790
- const prefix = propertiesOption.substring(8);
791
- if (prefix === "") {
792
- throw new TypeError(
793
- `Invalid properties option: ${
794
- JSON.stringify(propertiesOption)
795
- }. It must be of the form "prepend:<prefix>" where <prefix> is a non-empty string.`,
796
- );
797
- }
798
- getProperties = (properties) => {
799
- const result: Record<string, unknown> = {};
800
- for (const key in properties) {
801
- result[`${prefix}${key}`] = properties[key];
802
- }
803
- return result;
804
- };
805
- } else if (propertiesOption.startsWith("nest:")) {
806
- const key = propertiesOption.substring(5);
807
- getProperties = (properties) => ({ [key]: properties });
808
- } else {
809
- throw new TypeError(
810
- `Invalid properties option: ${
811
- JSON.stringify(propertiesOption)
812
- }. It must be "flatten", "prepend:<prefix>", or "nest:<key>".`,
813
- );
814
- }
815
-
816
- // Pre-compile message rendering function
817
- let getMessage: (record: LogRecord) => string;
818
-
819
- if (isTemplateMessage) {
820
- getMessage = (record: LogRecord): string => {
821
- if (typeof record.rawMessage === "string") {
822
- return record.rawMessage;
823
- }
824
- let msg = "";
825
- for (let i = 0; i < record.rawMessage.length; i++) {
826
- msg += i % 2 < 1 ? record.rawMessage[i] : "{}";
827
- }
828
- return msg;
829
- };
830
- } else {
831
- getMessage = (record: LogRecord): string => {
832
- const msgLen = record.message.length;
833
-
834
- if (msgLen === 1) {
835
- return record.message[0] as string;
836
- }
837
-
838
- let msg = "";
839
- for (let i = 0; i < msgLen; i++) {
840
- msg += (i % 2 < 1)
841
- ? record.message[i]
842
- : JSON.stringify(record.message[i]);
843
- }
844
- return msg;
845
- };
846
- }
847
-
848
- return (record: LogRecord): string => {
849
- return JSON.stringify({
850
- "@timestamp": new Date(record.timestamp).toISOString(),
851
- level: record.level === "warning" ? "WARN" : record.level.toUpperCase(),
852
- message: getMessage(record),
853
- logger: joinCategory(record.category),
854
- ...getProperties(record.properties),
855
- }) + "\n";
856
- };
857
- }
858
-
859
- /**
860
- * The default [JSON Lines] formatter. This formatter formats log records
861
- * as JSON objects, one per line, which is a common format for log files.
862
- * It looks like this:
863
- *
864
- * ```json
865
- * {"@timestamp":"2023-11-14T22:13:20.000Z","level":"INFO","message":"Hello, world!","logger":"my.logger","properties":{"key":"value"}}
866
- * ```
867
- *
868
- * You can customize the output by passing options to
869
- * {@link getJsonLinesFormatter}. For example, you can change the category
870
- * separator, the message format, and how the properties are formatted.
871
- *
872
- * [JSON Lines]: https://jsonlines.org/
873
- * @since 0.11.0
874
- */
875
- export const jsonLinesFormatter: TextFormatter = getJsonLinesFormatter();
876
-
877
- /**
878
- * A console formatter is a function that accepts a log record and returns
879
- * an array of arguments to pass to {@link console.log}.
880
- *
881
- * @param record The log record to format.
882
- * @returns The formatted log record, as an array of arguments for
883
- * {@link console.log}.
884
- */
885
- export type ConsoleFormatter = (record: LogRecord) => readonly unknown[];
886
-
887
- /**
888
- * The styles for the log level in the console.
889
- */
890
- const logLevelStyles: Record<LogLevel, string> = {
891
- "trace": "background-color: gray; color: white;",
892
- "debug": "background-color: gray; color: white;",
893
- "info": "background-color: white; color: black;",
894
- "warning": "background-color: orange; color: black;",
895
- "error": "background-color: red; color: white;",
896
- "fatal": "background-color: maroon; color: white;",
897
- };
898
-
899
- /**
900
- * The default console formatter.
901
- *
902
- * @param record The log record to format.
903
- * @returns The formatted log record, as an array of arguments for
904
- * {@link console.log}.
905
- */
906
- export function defaultConsoleFormatter(record: LogRecord): readonly unknown[] {
907
- let msg = "";
908
- const values: unknown[] = [];
909
- for (let i = 0; i < record.message.length; i++) {
910
- if (i % 2 === 0) msg += record.message[i];
911
- else {
912
- msg += "%o";
913
- values.push(record.message[i]);
914
- }
915
- }
916
- const date = new Date(record.timestamp);
917
- const time = `${date.getUTCHours().toString().padStart(2, "0")}:${
918
- date.getUTCMinutes().toString().padStart(2, "0")
919
- }:${date.getUTCSeconds().toString().padStart(2, "0")}.${
920
- date.getUTCMilliseconds().toString().padStart(3, "0")
921
- }`;
922
- return [
923
- `%c${time} %c${levelAbbreviations[record.level]}%c %c${
924
- record.category.join("\xb7")
925
- } %c${msg}`,
926
- "color: gray;",
927
- logLevelStyles[record.level],
928
- "background-color: default;",
929
- "color: gray;",
930
- "color: default;",
931
- ...values,
932
- ];
933
- }