@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
@@ -0,0 +1,281 @@
1
+ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
2
+ const __util = require_rolldown_runtime.__toESM(require("#util"));
3
+
4
+ //#region formatter.ts
5
+ /**
6
+ * The severity level abbreviations.
7
+ */
8
+ const levelAbbreviations = {
9
+ "debug": "DBG",
10
+ "info": "INF",
11
+ "warning": "WRN",
12
+ "error": "ERR",
13
+ "fatal": "FTL"
14
+ };
15
+ /**
16
+ * A platform-specific inspect function. In Deno, this is {@link Deno.inspect},
17
+ * and in Node.js/Bun it is `util.inspect()`. If neither is available, it
18
+ * falls back to {@link JSON.stringify}.
19
+ *
20
+ * @param value The value to inspect.
21
+ * @param options The options for inspecting the value.
22
+ * If `colors` is `true`, the output will be ANSI-colored.
23
+ * @returns The string representation of the value.
24
+ */
25
+ const inspect = typeof document !== "undefined" || typeof navigator !== "undefined" && navigator.product === "ReactNative" ? (v) => JSON.stringify(v) : "Deno" in globalThis && "inspect" in globalThis.Deno && typeof globalThis.Deno.inspect === "function" ? (v, opts) => globalThis.Deno.inspect(v, {
26
+ strAbbreviateSize: Infinity,
27
+ iterableLimit: Infinity,
28
+ ...opts
29
+ }) : __util != null && "inspect" in __util && typeof __util.inspect === "function" ? (v, opts) => __util.inspect(v, {
30
+ maxArrayLength: Infinity,
31
+ maxStringLength: Infinity,
32
+ ...opts
33
+ }) : (v) => JSON.stringify(v);
34
+ /**
35
+ * Get a text formatter with the specified options. Although it's flexible
36
+ * enough to create a custom formatter, if you want more control, you can
37
+ * create a custom formatter that satisfies the {@link TextFormatter} type
38
+ * instead.
39
+ *
40
+ * For more information on the options, see {@link TextFormatterOptions}.
41
+ *
42
+ * By default, the formatter formats log records as follows:
43
+ *
44
+ * ```
45
+ * 2023-11-14 22:13:20.000 +00:00 [INF] category·subcategory: Hello, world!
46
+ * ```
47
+ * @param options The options for the text formatter.
48
+ * @returns The text formatter.
49
+ * @since 0.6.0
50
+ */
51
+ function getTextFormatter(options = {}) {
52
+ const timestampRenderer = options.timestamp == null || options.timestamp === "date-time-timezone" ? (ts) => new Date(ts).toISOString().replace("T", " ").replace("Z", " +00:00") : options.timestamp === "date-time-tz" ? (ts) => new Date(ts).toISOString().replace("T", " ").replace("Z", " +00") : options.timestamp === "date-time" ? (ts) => new Date(ts).toISOString().replace("T", " ").replace("Z", "") : options.timestamp === "time-timezone" ? (ts) => new Date(ts).toISOString().replace(/.*T/, "").replace("Z", " +00:00") : options.timestamp === "time-tz" ? (ts) => new Date(ts).toISOString().replace(/.*T/, "").replace("Z", " +00") : options.timestamp === "time" ? (ts) => new Date(ts).toISOString().replace(/.*T/, "").replace("Z", "") : options.timestamp === "date" ? (ts) => new Date(ts).toISOString().replace(/T.*/, "") : options.timestamp === "rfc3339" ? (ts) => new Date(ts).toISOString() : options.timestamp === "none" || options.timestamp === "disabled" ? () => null : options.timestamp;
53
+ const categorySeparator = options.category ?? "·";
54
+ const valueRenderer = options.value ?? inspect;
55
+ const levelRenderer = options.level == null || options.level === "ABBR" ? (level) => levelAbbreviations[level] : options.level === "abbr" ? (level) => levelAbbreviations[level].toLowerCase() : options.level === "FULL" ? (level) => level.toUpperCase() : options.level === "full" ? (level) => level : options.level === "L" ? (level) => level.charAt(0).toUpperCase() : options.level === "l" ? (level) => level.charAt(0) : options.level;
56
+ const formatter = options.format ?? (({ timestamp, level, category, message }) => `${timestamp ? `${timestamp} ` : ""}[${level}] ${category}: ${message}`);
57
+ return (record) => {
58
+ let message = "";
59
+ for (let i = 0; i < record.message.length; i++) if (i % 2 === 0) message += record.message[i];
60
+ else message += valueRenderer(record.message[i]);
61
+ const timestamp = timestampRenderer(record.timestamp);
62
+ const level = levelRenderer(record.level);
63
+ const category = typeof categorySeparator === "function" ? categorySeparator(record.category) : record.category.join(categorySeparator);
64
+ const values = {
65
+ timestamp,
66
+ level,
67
+ category,
68
+ message,
69
+ record
70
+ };
71
+ return `${formatter(values)}\n`;
72
+ };
73
+ }
74
+ /**
75
+ * The default text formatter. This formatter formats log records as follows:
76
+ *
77
+ * ```
78
+ * 2023-11-14 22:13:20.000 +00:00 [INF] category·subcategory: Hello, world!
79
+ * ```
80
+ *
81
+ * @param record The log record to format.
82
+ * @returns The formatted log record.
83
+ */
84
+ const defaultTextFormatter = getTextFormatter();
85
+ const RESET = "\x1B[0m";
86
+ const ansiColors = {
87
+ black: "\x1B[30m",
88
+ red: "\x1B[31m",
89
+ green: "\x1B[32m",
90
+ yellow: "\x1B[33m",
91
+ blue: "\x1B[34m",
92
+ magenta: "\x1B[35m",
93
+ cyan: "\x1B[36m",
94
+ white: "\x1B[37m"
95
+ };
96
+ const ansiStyles = {
97
+ bold: "\x1B[1m",
98
+ dim: "\x1B[2m",
99
+ italic: "\x1B[3m",
100
+ underline: "\x1B[4m",
101
+ strikethrough: "\x1B[9m"
102
+ };
103
+ const defaultLevelColors = {
104
+ debug: "blue",
105
+ info: "green",
106
+ warning: "yellow",
107
+ error: "red",
108
+ fatal: "magenta"
109
+ };
110
+ /**
111
+ * Get an ANSI color formatter with the specified options.
112
+ *
113
+ * ![A preview of an ANSI color formatter.](https://i.imgur.com/I8LlBUf.png)
114
+ * @param option The options for the ANSI color formatter.
115
+ * @returns The ANSI color formatter.
116
+ * @since 0.6.0
117
+ */
118
+ function getAnsiColorFormatter(options = {}) {
119
+ const format = options.format;
120
+ const timestampStyle = typeof options.timestampStyle === "undefined" ? "dim" : options.timestampStyle;
121
+ const timestampColor = options.timestampColor ?? null;
122
+ const timestampPrefix = `${timestampStyle == null ? "" : ansiStyles[timestampStyle]}${timestampColor == null ? "" : ansiColors[timestampColor]}`;
123
+ const timestampSuffix = timestampStyle == null && timestampColor == null ? "" : RESET;
124
+ const levelStyle = typeof options.levelStyle === "undefined" ? "bold" : options.levelStyle;
125
+ const levelColors = options.levelColors ?? defaultLevelColors;
126
+ const categoryStyle = typeof options.categoryStyle === "undefined" ? "dim" : options.categoryStyle;
127
+ const categoryColor = options.categoryColor ?? null;
128
+ const categoryPrefix = `${categoryStyle == null ? "" : ansiStyles[categoryStyle]}${categoryColor == null ? "" : ansiColors[categoryColor]}`;
129
+ const categorySuffix = categoryStyle == null && categoryColor == null ? "" : RESET;
130
+ return getTextFormatter({
131
+ timestamp: "date-time-tz",
132
+ value(value) {
133
+ return inspect(value, { colors: true });
134
+ },
135
+ ...options,
136
+ format({ timestamp, level, category, message, record }) {
137
+ const levelColor = levelColors[record.level];
138
+ timestamp = `${timestampPrefix}${timestamp}${timestampSuffix}`;
139
+ level = `${levelStyle == null ? "" : ansiStyles[levelStyle]}${levelColor == null ? "" : ansiColors[levelColor]}${level}${levelStyle == null && levelColor == null ? "" : RESET}`;
140
+ return format == null ? `${timestamp} ${level} ${categoryPrefix}${category}:${categorySuffix} ${message}` : format({
141
+ timestamp,
142
+ level,
143
+ category: `${categoryPrefix}${category}${categorySuffix}`,
144
+ message,
145
+ record
146
+ });
147
+ }
148
+ });
149
+ }
150
+ /**
151
+ * A text formatter that uses ANSI colors to format log records.
152
+ *
153
+ * ![A preview of ansiColorFormatter.](https://i.imgur.com/I8LlBUf.png)
154
+ *
155
+ * @param record The log record to format.
156
+ * @returns The formatted log record.
157
+ * @since 0.5.0
158
+ */
159
+ const ansiColorFormatter = getAnsiColorFormatter();
160
+ /**
161
+ * Get a [JSON Lines] formatter with the specified options. The log records
162
+ * will be rendered as JSON objects, one per line, which is a common format
163
+ * for log files. This format is also known as Newline-Delimited JSON (NDJSON).
164
+ * It looks like this:
165
+ *
166
+ * ```json
167
+ * {"@timestamp":"2023-11-14T22:13:20.000Z","level":"INFO","message":"Hello, world!","logger":"my.logger","properties":{"key":"value"}}
168
+ * ```
169
+ *
170
+ * [JSON Lines]: https://jsonlines.org/
171
+ * @param options The options for the JSON Lines formatter.
172
+ * @returns The JSON Lines formatter.
173
+ * @since 0.11.0
174
+ */
175
+ function getJsonLinesFormatter(options = {}) {
176
+ let joinCategory;
177
+ if (typeof options.categorySeparator === "function") joinCategory = options.categorySeparator;
178
+ else {
179
+ const separator = options.categorySeparator ?? ".";
180
+ joinCategory = (category) => category.join(separator);
181
+ }
182
+ let getMessage;
183
+ if (options.message === "template") getMessage = (record) => {
184
+ if (typeof record.rawMessage === "string") return record.rawMessage;
185
+ let msg = "";
186
+ for (let i = 0; i < record.rawMessage.length; i++) msg += i % 2 < 1 ? record.rawMessage[i] : "{}";
187
+ return msg;
188
+ };
189
+ else getMessage = (record) => {
190
+ let msg = "";
191
+ for (let i = 0; i < record.message.length; i++) msg += i % 2 < 1 ? record.message[i] : JSON.stringify(record.message[i]);
192
+ return msg;
193
+ };
194
+ const propertiesOption = options.properties ?? "nest:properties";
195
+ let getProperties;
196
+ if (propertiesOption === "flatten") getProperties = (properties) => properties;
197
+ else if (propertiesOption.startsWith("prepend:")) {
198
+ const prefix = propertiesOption.substring(8);
199
+ if (prefix === "") throw new TypeError(`Invalid properties option: ${JSON.stringify(propertiesOption)}. It must be of the form "prepend:<prefix>" where <prefix> is a non-empty string.`);
200
+ getProperties = (properties) => {
201
+ const result = {};
202
+ for (const key in properties) result[`${prefix}${key}`] = properties[key];
203
+ return result;
204
+ };
205
+ } else if (propertiesOption.startsWith("nest:")) {
206
+ const key = propertiesOption.substring(5);
207
+ getProperties = (properties) => ({ [key]: properties });
208
+ } else throw new TypeError(`Invalid properties option: ${JSON.stringify(propertiesOption)}. It must be "flatten", "prepend:<prefix>", or "nest:<key>".`);
209
+ return (record) => {
210
+ return JSON.stringify({
211
+ "@timestamp": new Date(record.timestamp).toISOString(),
212
+ level: record.level === "warning" ? "WARN" : record.level.toUpperCase(),
213
+ message: getMessage(record),
214
+ logger: joinCategory(record.category),
215
+ ...getProperties(record.properties)
216
+ });
217
+ };
218
+ }
219
+ /**
220
+ * The default [JSON Lines] formatter. This formatter formats log records
221
+ * as JSON objects, one per line, which is a common format for log files.
222
+ * It looks like this:
223
+ *
224
+ * ```json
225
+ * {"@timestamp":"2023-11-14T22:13:20.000Z","level":"INFO","message":"Hello, world!","logger":"my.logger","properties":{"key":"value"}}
226
+ * ```
227
+ *
228
+ * You can customize the output by passing options to
229
+ * {@link getJsonLinesFormatter}. For example, you can change the category
230
+ * separator, the message format, and how the properties are formatted.
231
+ *
232
+ * [JSON Lines]: https://jsonlines.org/
233
+ * @since 0.11.0
234
+ */
235
+ const jsonLinesFormatter = getJsonLinesFormatter();
236
+ /**
237
+ * The styles for the log level in the console.
238
+ */
239
+ const logLevelStyles = {
240
+ "debug": "background-color: gray; color: white;",
241
+ "info": "background-color: white; color: black;",
242
+ "warning": "background-color: orange; color: black;",
243
+ "error": "background-color: red; color: white;",
244
+ "fatal": "background-color: maroon; color: white;"
245
+ };
246
+ /**
247
+ * The default console formatter.
248
+ *
249
+ * @param record The log record to format.
250
+ * @returns The formatted log record, as an array of arguments for
251
+ * {@link console.log}.
252
+ */
253
+ function defaultConsoleFormatter(record) {
254
+ let msg = "";
255
+ const values = [];
256
+ for (let i = 0; i < record.message.length; i++) if (i % 2 === 0) msg += record.message[i];
257
+ else {
258
+ msg += "%o";
259
+ values.push(record.message[i]);
260
+ }
261
+ const date = new Date(record.timestamp);
262
+ 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")}`;
263
+ return [
264
+ `%c${time} %c${levelAbbreviations[record.level]}%c %c${record.category.join("·")} %c${msg}`,
265
+ "color: gray;",
266
+ logLevelStyles[record.level],
267
+ "background-color: default;",
268
+ "color: gray;",
269
+ "color: default;",
270
+ ...values
271
+ ];
272
+ }
273
+
274
+ //#endregion
275
+ exports.ansiColorFormatter = ansiColorFormatter;
276
+ exports.defaultConsoleFormatter = defaultConsoleFormatter;
277
+ exports.defaultTextFormatter = defaultTextFormatter;
278
+ exports.getAnsiColorFormatter = getAnsiColorFormatter;
279
+ exports.getJsonLinesFormatter = getJsonLinesFormatter;
280
+ exports.getTextFormatter = getTextFormatter;
281
+ exports.jsonLinesFormatter = jsonLinesFormatter;
@@ -0,0 +1,338 @@
1
+ import { LogLevel } from "./level.cjs";
2
+ import { LogRecord } from "./record.cjs";
3
+
4
+ //#region formatter.d.ts
5
+
6
+ /**
7
+ * A text formatter is a function that accepts a log record and returns
8
+ * a string.
9
+ *
10
+ * @param record The log record to format.
11
+ * @returns The formatted log record.
12
+ */
13
+ type TextFormatter = (record: LogRecord) => string;
14
+ /**
15
+ * The formatted values for a log record.
16
+ * @since 0.6.0
17
+ */
18
+ interface FormattedValues {
19
+ /**
20
+ * The formatted timestamp.
21
+ */
22
+ timestamp: string | null;
23
+ /**
24
+ * The formatted log level.
25
+ */
26
+ level: string;
27
+ /**
28
+ * The formatted category.
29
+ */
30
+ category: string;
31
+ /**
32
+ * The formatted message.
33
+ */
34
+ message: string;
35
+ /**
36
+ * The unformatted log record.
37
+ */
38
+ record: LogRecord;
39
+ }
40
+ /**
41
+ * The various options for the built-in text formatters.
42
+ * @since 0.6.0
43
+ */
44
+ interface TextFormatterOptions {
45
+ /**
46
+ * The timestamp format. This can be one of the following:
47
+ *
48
+ * - `"date-time-timezone"`: The date and time with the full timezone offset
49
+ * (e.g., `"2023-11-14 22:13:20.000 +00:00"`).
50
+ * - `"date-time-tz"`: The date and time with the short timezone offset
51
+ * (e.g., `"2023-11-14 22:13:20.000 +00"`).
52
+ * - `"date-time"`: The date and time without the timezone offset
53
+ * (e.g., `"2023-11-14 22:13:20.000"`).
54
+ * - `"time-timezone"`: The time with the full timezone offset but without
55
+ * the date (e.g., `"22:13:20.000 +00:00"`).
56
+ * - `"time-tz"`: The time with the short timezone offset but without the date
57
+ * (e.g., `"22:13:20.000 +00"`).
58
+ * - `"time"`: The time without the date or timezone offset
59
+ * (e.g., `"22:13:20.000"`).
60
+ * - `"date"`: The date without the time or timezone offset
61
+ * (e.g., `"2023-11-14"`).
62
+ * - `"rfc3339"`: The date and time in RFC 3339 format
63
+ * (e.g., `"2023-11-14T22:13:20.000Z"`).
64
+ * - `"none"` or `"disabled"`: No display
65
+ *
66
+ * Alternatively, this can be a function that accepts a timestamp and returns
67
+ * a string.
68
+ *
69
+ * The default is `"date-time-timezone"`.
70
+ */
71
+ timestamp?: "date-time-timezone" | "date-time-tz" | "date-time" | "time-timezone" | "time-tz" | "time" | "date" | "rfc3339" | "none" | "disabled" | ((ts: number) => string | null);
72
+ /**
73
+ * The log level format. This can be one of the following:
74
+ *
75
+ * - `"ABBR"`: The log level abbreviation in uppercase (e.g., `"INF"`).
76
+ * - `"FULL"`: The full log level name in uppercase (e.g., `"INFO"`).
77
+ * - `"L"`: The first letter of the log level in uppercase (e.g., `"I"`).
78
+ * - `"abbr"`: The log level abbreviation in lowercase (e.g., `"inf"`).
79
+ * - `"full"`: The full log level name in lowercase (e.g., `"info"`).
80
+ * - `"l"`: The first letter of the log level in lowercase (e.g., `"i"`).
81
+ *
82
+ * Alternatively, this can be a function that accepts a log level and returns
83
+ * a string.
84
+ *
85
+ * The default is `"ABBR"`.
86
+ */
87
+ level?: "ABBR" | "FULL" | "L" | "abbr" | "full" | "l" | ((level: LogLevel) => string);
88
+ /**
89
+ * The separator between category names. For example, if the separator is
90
+ * `"·"`, the category `["a", "b", "c"]` will be formatted as `"a·b·c"`.
91
+ * The default separator is `"·"`.
92
+ *
93
+ * If this is a function, it will be called with the category array and
94
+ * should return a string, which will be used for rendering the category.
95
+ */
96
+ category?: string | ((category: readonly string[]) => string);
97
+ /**
98
+ * The format of the embedded values.
99
+ *
100
+ * A function that renders a value to a string. This function is used to
101
+ * render the values in the log record. The default is [`util.inspect()`] in
102
+ * Node.js/Bun and [`Deno.inspect()`] in Deno.
103
+ *
104
+ * [`util.inspect()`]: https://nodejs.org/api/util.html#utilinspectobject-options
105
+ * [`Deno.inspect()`]: https://docs.deno.com/api/deno/~/Deno.inspect
106
+ * @param value The value to render.
107
+ * @returns The string representation of the value.
108
+ */
109
+ value?: (value: unknown) => string;
110
+ /**
111
+ * How those formatted parts are concatenated.
112
+ *
113
+ * A function that formats the log record. This function is called with the
114
+ * formatted values and should return a string. Note that the formatted
115
+ * *should not* include a newline character at the end.
116
+ *
117
+ * By default, this is a function that formats the log record as follows:
118
+ *
119
+ * ```
120
+ * 2023-11-14 22:13:20.000 +00:00 [INF] category·subcategory: Hello, world!
121
+ * ```
122
+ * @param values The formatted values.
123
+ * @returns The formatted log record.
124
+ */
125
+ format?: (values: FormattedValues) => string;
126
+ }
127
+ /**
128
+ * Get a text formatter with the specified options. Although it's flexible
129
+ * enough to create a custom formatter, if you want more control, you can
130
+ * create a custom formatter that satisfies the {@link TextFormatter} type
131
+ * instead.
132
+ *
133
+ * For more information on the options, see {@link TextFormatterOptions}.
134
+ *
135
+ * By default, the formatter formats log records as follows:
136
+ *
137
+ * ```
138
+ * 2023-11-14 22:13:20.000 +00:00 [INF] category·subcategory: Hello, world!
139
+ * ```
140
+ * @param options The options for the text formatter.
141
+ * @returns The text formatter.
142
+ * @since 0.6.0
143
+ */
144
+ declare function getTextFormatter(options?: TextFormatterOptions): TextFormatter;
145
+ /**
146
+ * The default text formatter. This formatter formats log records as follows:
147
+ *
148
+ * ```
149
+ * 2023-11-14 22:13:20.000 +00:00 [INF] category·subcategory: Hello, world!
150
+ * ```
151
+ *
152
+ * @param record The log record to format.
153
+ * @returns The formatted log record.
154
+ */
155
+ declare const defaultTextFormatter: TextFormatter;
156
+ /**
157
+ * The ANSI colors. These can be used to colorize text in the console.
158
+ * @since 0.6.0
159
+ */
160
+ type AnsiColor = "black" | "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white";
161
+ /**
162
+ * The ANSI text styles.
163
+ * @since 0.6.0
164
+ */
165
+ type AnsiStyle = "bold" | "dim" | "italic" | "underline" | "strikethrough";
166
+ /**
167
+ * The various options for the ANSI color formatter.
168
+ * @since 0.6.0
169
+ */
170
+ interface AnsiColorFormatterOptions extends TextFormatterOptions {
171
+ /**
172
+ * The timestamp format. This can be one of the following:
173
+ *
174
+ * - `"date-time-timezone"`: The date and time with the full timezone offset
175
+ * (e.g., `"2023-11-14 22:13:20.000 +00:00"`).
176
+ * - `"date-time-tz"`: The date and time with the short timezone offset
177
+ * (e.g., `"2023-11-14 22:13:20.000 +00"`).
178
+ * - `"date-time"`: The date and time without the timezone offset
179
+ * (e.g., `"2023-11-14 22:13:20.000"`).
180
+ * - `"time-timezone"`: The time with the full timezone offset but without
181
+ * the date (e.g., `"22:13:20.000 +00:00"`).
182
+ * - `"time-tz"`: The time with the short timezone offset but without the date
183
+ * (e.g., `"22:13:20.000 +00"`).
184
+ * - `"time"`: The time without the date or timezone offset
185
+ * (e.g., `"22:13:20.000"`).
186
+ * - `"date"`: The date without the time or timezone offset
187
+ * (e.g., `"2023-11-14"`).
188
+ * - `"rfc3339"`: The date and time in RFC 3339 format
189
+ * (e.g., `"2023-11-14T22:13:20.000Z"`).
190
+ *
191
+ * Alternatively, this can be a function that accepts a timestamp and returns
192
+ * a string.
193
+ *
194
+ * The default is `"date-time-tz"`.
195
+ */
196
+ timestamp?: "date-time-timezone" | "date-time-tz" | "date-time" | "time-timezone" | "time-tz" | "time" | "date" | "rfc3339" | ((ts: number) => string);
197
+ /**
198
+ * The ANSI style for the timestamp. `"dim"` is used by default.
199
+ */
200
+ timestampStyle?: AnsiStyle | null;
201
+ /**
202
+ * The ANSI color for the timestamp. No color is used by default.
203
+ */
204
+ timestampColor?: AnsiColor | null;
205
+ /**
206
+ * The ANSI style for the log level. `"bold"` is used by default.
207
+ */
208
+ levelStyle?: AnsiStyle | null;
209
+ /**
210
+ * The ANSI colors for the log levels. The default colors are as follows:
211
+ *
212
+ * - `"debug"`: `"blue"`
213
+ * - `"info"`: `"green"`
214
+ * - `"warning"`: `"yellow"`
215
+ * - `"error"`: `"red"`
216
+ * - `"fatal"`: `"magenta"`
217
+ */
218
+ levelColors?: Record<LogLevel, AnsiColor | null>;
219
+ /**
220
+ * The ANSI style for the category. `"dim"` is used by default.
221
+ */
222
+ categoryStyle?: AnsiStyle | null;
223
+ /**
224
+ * The ANSI color for the category. No color is used by default.
225
+ */
226
+ categoryColor?: AnsiColor | null;
227
+ }
228
+ /**
229
+ * Get an ANSI color formatter with the specified options.
230
+ *
231
+ * ![A preview of an ANSI color formatter.](https://i.imgur.com/I8LlBUf.png)
232
+ * @param option The options for the ANSI color formatter.
233
+ * @returns The ANSI color formatter.
234
+ * @since 0.6.0
235
+ */
236
+ declare function getAnsiColorFormatter(options?: AnsiColorFormatterOptions): TextFormatter;
237
+ /**
238
+ * A text formatter that uses ANSI colors to format log records.
239
+ *
240
+ * ![A preview of ansiColorFormatter.](https://i.imgur.com/I8LlBUf.png)
241
+ *
242
+ * @param record The log record to format.
243
+ * @returns The formatted log record.
244
+ * @since 0.5.0
245
+ */
246
+ declare const ansiColorFormatter: TextFormatter;
247
+ /**
248
+ * Options for the {@link getJsonLinesFormatter} function.
249
+ * @since 0.11.0
250
+ */
251
+ interface JsonLinesFormatterOptions {
252
+ /**
253
+ * The separator between category names. For example, if the separator is
254
+ * `"."`, the category `["a", "b", "c"]` will be formatted as `"a.b.c"`.
255
+ * If this is a function, it will be called with the category array and
256
+ * should return a string or an array of strings, which will be used
257
+ * for rendering the category.
258
+ *
259
+ * @default `"."`
260
+ */
261
+ readonly categorySeparator?: string | ((category: readonly string[]) => string | readonly string[]);
262
+ /**
263
+ * The message format. This can be one of the following:
264
+ *
265
+ * - `"template"`: The raw message template is used as the message.
266
+ * - `"rendered"`: The message is rendered with the values.
267
+ *
268
+ * @default `"rendered"`
269
+ */
270
+ readonly message?: "template" | "rendered";
271
+ /**
272
+ * The properties format. This can be one of the following:
273
+ *
274
+ * - `"flatten"`: The properties are flattened into the root object.
275
+ * - `"prepend:<prefix>"`: The properties are prepended with the given prefix
276
+ * (e.g., `"prepend:ctx_"` will prepend `ctx_` to each property key).
277
+ * - `"nest:<key>"`: The properties are nested under the given key
278
+ * (e.g., `"nest:properties"` will nest the properties under the
279
+ * `properties` key).
280
+ *
281
+ * @default `"nest:properties"`
282
+ */
283
+ readonly properties?: "flatten" | `prepend:${string}` | `nest:${string}`;
284
+ }
285
+ /**
286
+ * Get a [JSON Lines] formatter with the specified options. The log records
287
+ * will be rendered as JSON objects, one per line, which is a common format
288
+ * for log files. This format is also known as Newline-Delimited JSON (NDJSON).
289
+ * It looks like this:
290
+ *
291
+ * ```json
292
+ * {"@timestamp":"2023-11-14T22:13:20.000Z","level":"INFO","message":"Hello, world!","logger":"my.logger","properties":{"key":"value"}}
293
+ * ```
294
+ *
295
+ * [JSON Lines]: https://jsonlines.org/
296
+ * @param options The options for the JSON Lines formatter.
297
+ * @returns The JSON Lines formatter.
298
+ * @since 0.11.0
299
+ */
300
+ declare function getJsonLinesFormatter(options?: JsonLinesFormatterOptions): TextFormatter;
301
+ /**
302
+ * The default [JSON Lines] formatter. This formatter formats log records
303
+ * as JSON objects, one per line, which is a common format for log files.
304
+ * It looks like this:
305
+ *
306
+ * ```json
307
+ * {"@timestamp":"2023-11-14T22:13:20.000Z","level":"INFO","message":"Hello, world!","logger":"my.logger","properties":{"key":"value"}}
308
+ * ```
309
+ *
310
+ * You can customize the output by passing options to
311
+ * {@link getJsonLinesFormatter}. For example, you can change the category
312
+ * separator, the message format, and how the properties are formatted.
313
+ *
314
+ * [JSON Lines]: https://jsonlines.org/
315
+ * @since 0.11.0
316
+ */
317
+ declare const jsonLinesFormatter: TextFormatter;
318
+ /**
319
+ * A console formatter is a function that accepts a log record and returns
320
+ * an array of arguments to pass to {@link console.log}.
321
+ *
322
+ * @param record The log record to format.
323
+ * @returns The formatted log record, as an array of arguments for
324
+ * {@link console.log}.
325
+ */
326
+ type ConsoleFormatter = (record: LogRecord) => readonly unknown[];
327
+ /**
328
+ * The default console formatter.
329
+ *
330
+ * @param record The log record to format.
331
+ * @returns The formatted log record, as an array of arguments for
332
+ * {@link console.log}.
333
+ */
334
+ declare function defaultConsoleFormatter(record: LogRecord): readonly unknown[];
335
+ //# sourceMappingURL=formatter.d.ts.map
336
+ //#endregion
337
+ export { AnsiColor, AnsiColorFormatterOptions, AnsiStyle, ConsoleFormatter, FormattedValues, JsonLinesFormatterOptions, TextFormatter, TextFormatterOptions, ansiColorFormatter, defaultConsoleFormatter, defaultTextFormatter, getAnsiColorFormatter, getJsonLinesFormatter, getTextFormatter, jsonLinesFormatter };
338
+ //# sourceMappingURL=formatter.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatter.d.cts","names":[],"sources":["../formatter.ts"],"sourcesContent":[],"mappings":";;;;;;;AAWA;AA8DA;AA+BA;;;AAuGoB,KApMR,aAAA,GAoMQ,CAAA,MAAA,EApMiB,SAoMjB,EAAA,GAAA,MAAA;AAAe;AAoBnC;;;AAEG,UA5Jc,eAAA,CA4Jd;EAAa;AA6EhB;AAQA;EAyBY,SAAA,EAAA,MAAS,GAAA,IAAA;EA2BJ;;;EAwCW,KAKT,EAAA,MAAA;EAAS;;;EAgBc,QAA1B,EAAA,MAAA;EAAM;;;EA7DiD,OAAA,EAAA,MAAA;EAkFvD;;;EACyB,MACtC,EAjWO,SAiWP;AAAa;AA4DhB;AAMA;AAsDA;;AACW,UAndM,oBAAA,CAmdN;EAA8B;AACzB;AA8FhB;AAUA;AAoBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mEAlhBe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;oBAyCK;;;;;;;;;;;;;;;;;;;iBAoBJ,gBAAA,WACL,uBACR;;;;;;;;;;;cA6EU,sBAAsB;;;;;KAQvB,SAAA;;;;;KAyBA,SAAA;;;;;UA2BK,yBAAA,SAAkC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBAwChC;;;;mBAKA;;;;eAKJ;;;;;;;;;;gBAWC,OAAO,UAAU;;;;kBAKf;;;;kBAKA;;;;;;;;;;iBAWF,qBAAA,WACL,4BACR;;;;;;;;;;cA4DU,oBAAoB;;;;;UAMhB,yBAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAsDD,qBAAA,WACL,4BACR;;;;;;;;;;;;;;;;;cA8FU,oBAAoB;;;;;;;;;KAUrB,gBAAA,YAA4B;;;;;;;;iBAoBxB,uBAAA,SAAgC"}