@logtape/file 0.9.0-dev.1

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 (84) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +43 -0
  3. package/esm/_dnt.shims.js +57 -0
  4. package/esm/file/filesink.base.js +78 -0
  5. package/esm/file/filesink.node.js +47 -0
  6. package/esm/file/mod.js +1 -0
  7. package/esm/logtape/config.js +287 -0
  8. package/esm/logtape/context.js +23 -0
  9. package/esm/logtape/filter.js +42 -0
  10. package/esm/logtape/formatter.js +261 -0
  11. package/esm/logtape/level.js +59 -0
  12. package/esm/logtape/logger.js +480 -0
  13. package/esm/logtape/mod.js +8 -0
  14. package/esm/logtape/nodeUtil.js +2 -0
  15. package/esm/logtape/record.js +1 -0
  16. package/esm/logtape/sink.js +96 -0
  17. package/esm/package.json +3 -0
  18. package/package.json +55 -0
  19. package/script/_dnt.shims.js +60 -0
  20. package/script/file/filesink.base.js +82 -0
  21. package/script/file/filesink.node.js +55 -0
  22. package/script/file/mod.js +6 -0
  23. package/script/logtape/config.js +321 -0
  24. package/script/logtape/context.js +26 -0
  25. package/script/logtape/filter.js +46 -0
  26. package/script/logtape/formatter.js +270 -0
  27. package/script/logtape/level.js +64 -0
  28. package/script/logtape/logger.js +511 -0
  29. package/script/logtape/mod.js +34 -0
  30. package/script/logtape/nodeUtil.js +7 -0
  31. package/script/logtape/record.js +2 -0
  32. package/script/logtape/sink.js +101 -0
  33. package/script/package.json +3 -0
  34. package/types/_dnt.shims.d.ts +2 -0
  35. package/types/_dnt.shims.d.ts.map +1 -0
  36. package/types/_dnt.test_shims.d.ts.map +1 -0
  37. package/types/deps/jsr.io/@david/which-runtime/0.2.1/mod.d.ts.map +1 -0
  38. package/types/deps/jsr.io/@std/assert/0.222.1/_constants.d.ts.map +1 -0
  39. package/types/deps/jsr.io/@std/assert/0.222.1/_diff.d.ts.map +1 -0
  40. package/types/deps/jsr.io/@std/assert/0.222.1/_format.d.ts.map +1 -0
  41. package/types/deps/jsr.io/@std/assert/0.222.1/assert_equals.d.ts.map +1 -0
  42. package/types/deps/jsr.io/@std/assert/0.222.1/assertion_error.d.ts.map +1 -0
  43. package/types/deps/jsr.io/@std/assert/0.222.1/equal.d.ts.map +1 -0
  44. package/types/deps/jsr.io/@std/fmt/0.222.1/colors.d.ts.map +1 -0
  45. package/types/deps/jsr.io/@std/path/1.0.8/_common/assert_path.d.ts.map +1 -0
  46. package/types/deps/jsr.io/@std/path/1.0.8/_common/constants.d.ts.map +1 -0
  47. package/types/deps/jsr.io/@std/path/1.0.8/_common/normalize.d.ts.map +1 -0
  48. package/types/deps/jsr.io/@std/path/1.0.8/_common/normalize_string.d.ts.map +1 -0
  49. package/types/deps/jsr.io/@std/path/1.0.8/_os.d.ts.map +1 -0
  50. package/types/deps/jsr.io/@std/path/1.0.8/join.d.ts.map +1 -0
  51. package/types/deps/jsr.io/@std/path/1.0.8/posix/_util.d.ts.map +1 -0
  52. package/types/deps/jsr.io/@std/path/1.0.8/posix/join.d.ts.map +1 -0
  53. package/types/deps/jsr.io/@std/path/1.0.8/posix/normalize.d.ts.map +1 -0
  54. package/types/deps/jsr.io/@std/path/1.0.8/windows/_util.d.ts.map +1 -0
  55. package/types/deps/jsr.io/@std/path/1.0.8/windows/join.d.ts.map +1 -0
  56. package/types/deps/jsr.io/@std/path/1.0.8/windows/normalize.d.ts.map +1 -0
  57. package/types/file/filesink.base.d.ts +89 -0
  58. package/types/file/filesink.base.d.ts.map +1 -0
  59. package/types/file/filesink.node.d.ts +34 -0
  60. package/types/file/filesink.node.d.ts.map +1 -0
  61. package/types/file/filesink.test.d.ts.map +1 -0
  62. package/types/file/mod.d.ts +3 -0
  63. package/types/file/mod.d.ts.map +1 -0
  64. package/types/logtape/config.d.ts +183 -0
  65. package/types/logtape/config.d.ts.map +1 -0
  66. package/types/logtape/context.d.ts +35 -0
  67. package/types/logtape/context.d.ts.map +1 -0
  68. package/types/logtape/filter.d.ts +31 -0
  69. package/types/logtape/filter.d.ts.map +1 -0
  70. package/types/logtape/fixtures.d.ts.map +1 -0
  71. package/types/logtape/formatter.d.ts +260 -0
  72. package/types/logtape/formatter.d.ts.map +1 -0
  73. package/types/logtape/level.d.ts +32 -0
  74. package/types/logtape/level.d.ts.map +1 -0
  75. package/types/logtape/logger.d.ts +423 -0
  76. package/types/logtape/logger.d.ts.map +1 -0
  77. package/types/logtape/mod.d.ts +9 -0
  78. package/types/logtape/mod.d.ts.map +1 -0
  79. package/types/logtape/nodeUtil.d.ts +3 -0
  80. package/types/logtape/nodeUtil.d.ts.map +1 -0
  81. package/types/logtape/record.d.ts +44 -0
  82. package/types/logtape/record.d.ts.map +1 -0
  83. package/types/logtape/sink.d.ts +108 -0
  84. package/types/logtape/sink.d.ts.map +1 -0
@@ -0,0 +1,270 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ansiColorFormatter = exports.defaultTextFormatter = void 0;
7
+ exports.getTextFormatter = getTextFormatter;
8
+ exports.getAnsiColorFormatter = getAnsiColorFormatter;
9
+ exports.defaultConsoleFormatter = defaultConsoleFormatter;
10
+ const nodeUtil_js_1 = __importDefault(require("./nodeUtil.js"));
11
+ /**
12
+ * The severity level abbreviations.
13
+ */
14
+ const levelAbbreviations = {
15
+ "debug": "DBG",
16
+ "info": "INF",
17
+ "warning": "WRN",
18
+ "error": "ERR",
19
+ "fatal": "FTL",
20
+ };
21
+ /**
22
+ * A platform-specific inspect function. In Deno, this is {@link Deno.inspect},
23
+ * and in Node.js/Bun it is `util.inspect()`. If neither is available, it
24
+ * falls back to {@link JSON.stringify}.
25
+ *
26
+ * @param value The value to inspect.
27
+ * @param options The options for inspecting the value.
28
+ * If `colors` is `true`, the output will be ANSI-colored.
29
+ * @returns The string representation of the value.
30
+ */
31
+ const inspect =
32
+ // @ts-ignore: Deno global
33
+ // dnt-shim-ignore
34
+ "Deno" in globalThis && "inspect" in globalThis.Deno &&
35
+ // @ts-ignore: Deno global
36
+ // dnt-shim-ignore
37
+ typeof globalThis.Deno.inspect === "function"
38
+ ? (v, opts) =>
39
+ // @ts-ignore: Deno global
40
+ // dnt-shim-ignore
41
+ globalThis.Deno.inspect(v, {
42
+ strAbbreviateSize: Infinity,
43
+ iterableLimit: Infinity,
44
+ ...opts,
45
+ })
46
+ // @ts-ignore: Node.js global
47
+ // dnt-shim-ignore
48
+ : nodeUtil_js_1.default != null && "inspect" in nodeUtil_js_1.default && typeof nodeUtil_js_1.default.inspect === "function"
49
+ ? (v, opts) =>
50
+ // @ts-ignore: Node.js global
51
+ // dnt-shim-ignore
52
+ nodeUtil_js_1.default.inspect(v, {
53
+ maxArrayLength: Infinity,
54
+ maxStringLength: Infinity,
55
+ ...opts,
56
+ })
57
+ : (v) => JSON.stringify(v);
58
+ /**
59
+ * Get a text formatter with the specified options. Although it's flexible
60
+ * enough to create a custom formatter, if you want more control, you can
61
+ * create a custom formatter that satisfies the {@link TextFormatter} type
62
+ * instead.
63
+ *
64
+ * For more information on the options, see {@link TextFormatterOptions}.
65
+ *
66
+ * By default, the formatter formats log records as follows:
67
+ *
68
+ * ```
69
+ * 2023-11-14 22:13:20.000 +00:00 [INF] category·subcategory: Hello, world!
70
+ * ```
71
+ * @param options The options for the text formatter.
72
+ * @returns The text formatter.
73
+ * @since 0.6.0
74
+ */
75
+ function getTextFormatter(options = {}) {
76
+ const timestampRenderer = options.timestamp == null || options.timestamp === "date-time-timezone"
77
+ ? (ts) => new Date(ts).toISOString().replace("T", " ").replace("Z", " +00:00")
78
+ : options.timestamp === "date-time-tz"
79
+ ? (ts) => new Date(ts).toISOString().replace("T", " ").replace("Z", " +00")
80
+ : options.timestamp === "date-time"
81
+ ? (ts) => new Date(ts).toISOString().replace("T", " ").replace("Z", "")
82
+ : options.timestamp === "time-timezone"
83
+ ? (ts) => new Date(ts).toISOString().replace(/.*T/, "").replace("Z", " +00:00")
84
+ : options.timestamp === "time-tz"
85
+ ? (ts) => new Date(ts).toISOString().replace(/.*T/, "").replace("Z", " +00")
86
+ : options.timestamp === "time"
87
+ ? (ts) => new Date(ts).toISOString().replace(/.*T/, "").replace("Z", "")
88
+ : options.timestamp === "date"
89
+ ? (ts) => new Date(ts).toISOString().replace(/T.*/, "")
90
+ : options.timestamp === "rfc3339"
91
+ ? (ts) => new Date(ts).toISOString()
92
+ : options.timestamp;
93
+ const categorySeparator = options.category ?? "·";
94
+ const valueRenderer = options.value ?? inspect;
95
+ const levelRenderer = options.level == null || options.level === "ABBR"
96
+ ? (level) => levelAbbreviations[level]
97
+ : options.level === "abbr"
98
+ ? (level) => levelAbbreviations[level].toLowerCase()
99
+ : options.level === "FULL"
100
+ ? (level) => level.toUpperCase()
101
+ : options.level === "full"
102
+ ? (level) => level
103
+ : options.level === "L"
104
+ ? (level) => level.charAt(0).toUpperCase()
105
+ : options.level === "l"
106
+ ? (level) => level.charAt(0)
107
+ : options.level;
108
+ const formatter = options.format ??
109
+ (({ timestamp, level, category, message }) => `${timestamp} [${level}] ${category}: ${message}`);
110
+ return (record) => {
111
+ let message = "";
112
+ for (let i = 0; i < record.message.length; i++) {
113
+ if (i % 2 === 0)
114
+ message += record.message[i];
115
+ else
116
+ message += valueRenderer(record.message[i]);
117
+ }
118
+ const timestamp = timestampRenderer(record.timestamp);
119
+ const level = levelRenderer(record.level);
120
+ const category = typeof categorySeparator === "function"
121
+ ? categorySeparator(record.category)
122
+ : record.category.join(categorySeparator);
123
+ const values = {
124
+ timestamp,
125
+ level,
126
+ category,
127
+ message,
128
+ record,
129
+ };
130
+ return `${formatter(values)}\n`;
131
+ };
132
+ }
133
+ /**
134
+ * The default text formatter. This formatter formats log records as follows:
135
+ *
136
+ * ```
137
+ * 2023-11-14 22:13:20.000 +00:00 [INF] category·subcategory: Hello, world!
138
+ * ```
139
+ *
140
+ * @param record The log record to format.
141
+ * @returns The formatted log record.
142
+ */
143
+ exports.defaultTextFormatter = getTextFormatter();
144
+ const RESET = "\x1b[0m";
145
+ const ansiColors = {
146
+ black: "\x1b[30m",
147
+ red: "\x1b[31m",
148
+ green: "\x1b[32m",
149
+ yellow: "\x1b[33m",
150
+ blue: "\x1b[34m",
151
+ magenta: "\x1b[35m",
152
+ cyan: "\x1b[36m",
153
+ white: "\x1b[37m",
154
+ };
155
+ const ansiStyles = {
156
+ bold: "\x1b[1m",
157
+ dim: "\x1b[2m",
158
+ italic: "\x1b[3m",
159
+ underline: "\x1b[4m",
160
+ strikethrough: "\x1b[9m",
161
+ };
162
+ const defaultLevelColors = {
163
+ debug: "blue",
164
+ info: "green",
165
+ warning: "yellow",
166
+ error: "red",
167
+ fatal: "magenta",
168
+ };
169
+ /**
170
+ * Get an ANSI color formatter with the specified options.
171
+ *
172
+ * ![A preview of an ANSI color formatter.](https://i.imgur.com/I8LlBUf.png)
173
+ * @param option The options for the ANSI color formatter.
174
+ * @returns The ANSI color formatter.
175
+ * @since 0.6.0
176
+ */
177
+ function getAnsiColorFormatter(options = {}) {
178
+ const format = options.format;
179
+ const timestampStyle = typeof options.timestampStyle === "undefined"
180
+ ? "dim"
181
+ : options.timestampStyle;
182
+ const timestampColor = options.timestampColor ?? null;
183
+ const timestampPrefix = `${timestampStyle == null ? "" : ansiStyles[timestampStyle]}${timestampColor == null ? "" : ansiColors[timestampColor]}`;
184
+ const timestampSuffix = timestampStyle == null && timestampColor == null
185
+ ? ""
186
+ : RESET;
187
+ const levelStyle = typeof options.levelStyle === "undefined"
188
+ ? "bold"
189
+ : options.levelStyle;
190
+ const levelColors = options.levelColors ?? defaultLevelColors;
191
+ const categoryStyle = typeof options.categoryStyle === "undefined"
192
+ ? "dim"
193
+ : options.categoryStyle;
194
+ const categoryColor = options.categoryColor ?? null;
195
+ const categoryPrefix = `${categoryStyle == null ? "" : ansiStyles[categoryStyle]}${categoryColor == null ? "" : ansiColors[categoryColor]}`;
196
+ const categorySuffix = categoryStyle == null && categoryColor == null
197
+ ? ""
198
+ : RESET;
199
+ return getTextFormatter({
200
+ timestamp: "date-time-tz",
201
+ value(value) {
202
+ return inspect(value, { colors: true });
203
+ },
204
+ ...options,
205
+ format({ timestamp, level, category, message, record }) {
206
+ const levelColor = levelColors[record.level];
207
+ timestamp = `${timestampPrefix}${timestamp}${timestampSuffix}`;
208
+ level = `${levelStyle == null ? "" : ansiStyles[levelStyle]}${levelColor == null ? "" : ansiColors[levelColor]}${level}${levelStyle == null && levelColor == null ? "" : RESET}`;
209
+ return format == null
210
+ ? `${timestamp} ${level} ${categoryPrefix}${category}:${categorySuffix} ${message}`
211
+ : format({
212
+ timestamp,
213
+ level,
214
+ category: `${categoryPrefix}${category}${categorySuffix}`,
215
+ message,
216
+ record,
217
+ });
218
+ },
219
+ });
220
+ }
221
+ /**
222
+ * A text formatter that uses ANSI colors to format log records.
223
+ *
224
+ * ![A preview of ansiColorFormatter.](https://i.imgur.com/I8LlBUf.png)
225
+ *
226
+ * @param record The log record to format.
227
+ * @returns The formatted log record.
228
+ * @since 0.5.0
229
+ */
230
+ exports.ansiColorFormatter = getAnsiColorFormatter();
231
+ /**
232
+ * The styles for the log level in the console.
233
+ */
234
+ const logLevelStyles = {
235
+ "debug": "background-color: gray; color: white;",
236
+ "info": "background-color: white; color: black;",
237
+ "warning": "background-color: orange; color: black;",
238
+ "error": "background-color: red; color: white;",
239
+ "fatal": "background-color: maroon; color: white;",
240
+ };
241
+ /**
242
+ * The default console formatter.
243
+ *
244
+ * @param record The log record to format.
245
+ * @returns The formatted log record, as an array of arguments for
246
+ * {@link console.log}.
247
+ */
248
+ function defaultConsoleFormatter(record) {
249
+ let msg = "";
250
+ const values = [];
251
+ for (let i = 0; i < record.message.length; i++) {
252
+ if (i % 2 === 0)
253
+ msg += record.message[i];
254
+ else {
255
+ msg += "%o";
256
+ values.push(record.message[i]);
257
+ }
258
+ }
259
+ const date = new Date(record.timestamp);
260
+ 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")}`;
261
+ return [
262
+ `%c${time} %c${levelAbbreviations[record.level]}%c %c${record.category.join("\xb7")} %c${msg}`,
263
+ "color: gray;",
264
+ logLevelStyles[record.level],
265
+ "background-color: default;",
266
+ "color: gray;",
267
+ "color: default;",
268
+ ...values,
269
+ ];
270
+ }
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseLogLevel = parseLogLevel;
4
+ exports.isLogLevel = isLogLevel;
5
+ exports.compareLogLevel = compareLogLevel;
6
+ const logLevels = ["debug", "info", "warning", "error", "fatal"];
7
+ /**
8
+ * Parses a log level from a string.
9
+ *
10
+ * @param level The log level as a string. This is case-insensitive.
11
+ * @returns The log level.
12
+ * @throws {TypeError} If the log level is invalid.
13
+ */
14
+ function parseLogLevel(level) {
15
+ level = level.toLowerCase();
16
+ switch (level) {
17
+ case "debug":
18
+ case "info":
19
+ case "warning":
20
+ case "error":
21
+ case "fatal":
22
+ return level;
23
+ default:
24
+ throw new TypeError(`Invalid log level: ${level}.`);
25
+ }
26
+ }
27
+ /**
28
+ * Checks if a string is a valid log level. This function can be used as
29
+ * as a type guard to narrow the type of a string to a {@link LogLevel}.
30
+ *
31
+ * @param level The log level as a string. This is case-sensitive.
32
+ * @returns `true` if the string is a valid log level.
33
+ */
34
+ function isLogLevel(level) {
35
+ switch (level) {
36
+ case "debug":
37
+ case "info":
38
+ case "warning":
39
+ case "error":
40
+ case "fatal":
41
+ return true;
42
+ default:
43
+ return false;
44
+ }
45
+ }
46
+ /**
47
+ * Compares two log levels.
48
+ * @param a The first log level.
49
+ * @param b The second log level.
50
+ * @returns A negative number if `a` is less than `b`, a positive number if `a`
51
+ * is greater than `b`, or zero if they are equal.
52
+ * @since 0.8.0
53
+ */
54
+ function compareLogLevel(a, b) {
55
+ const aIndex = logLevels.indexOf(a);
56
+ if (aIndex < 0) {
57
+ throw new TypeError(`Invalid log level: ${JSON.stringify(a)}.`);
58
+ }
59
+ const bIndex = logLevels.indexOf(b);
60
+ if (bIndex < 0) {
61
+ throw new TypeError(`Invalid log level: ${JSON.stringify(b)}.`);
62
+ }
63
+ return aIndex - bIndex;
64
+ }