@logtape/file 0.9.0-dev.134 → 0.9.1-dev.151
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/esm/file/filesink.base.js +1 -1
- package/package.json +4 -1
- package/script/file/filesink.base.js +3 -3
- package/types/file/filesink.base.d.ts +1 -1
- package/types/file/filesink.base.d.ts.map +1 -1
- package/types/file/filesink.node.d.ts +1 -1
- package/types/file/filesink.node.d.ts.map +1 -1
- package/esm/_dnt.shims.js +0 -57
- package/esm/logtape/config.js +0 -287
- package/esm/logtape/context.js +0 -23
- package/esm/logtape/filter.js +0 -42
- package/esm/logtape/formatter.js +0 -261
- package/esm/logtape/level.js +0 -59
- package/esm/logtape/logger.js +0 -480
- package/esm/logtape/mod.js +0 -8
- package/esm/logtape/nodeUtil.js +0 -2
- package/esm/logtape/record.js +0 -1
- package/esm/logtape/sink.js +0 -96
- package/script/_dnt.shims.js +0 -60
- package/script/logtape/config.js +0 -321
- package/script/logtape/context.js +0 -26
- package/script/logtape/filter.js +0 -46
- package/script/logtape/formatter.js +0 -270
- package/script/logtape/level.js +0 -64
- package/script/logtape/logger.js +0 -511
- package/script/logtape/mod.js +0 -34
- package/script/logtape/nodeUtil.js +0 -7
- package/script/logtape/record.js +0 -2
- package/script/logtape/sink.js +0 -101
- package/types/_dnt.shims.d.ts +0 -2
- package/types/_dnt.shims.d.ts.map +0 -1
- package/types/logtape/config.d.ts +0 -183
- package/types/logtape/config.d.ts.map +0 -1
- package/types/logtape/context.d.ts +0 -35
- package/types/logtape/context.d.ts.map +0 -1
- package/types/logtape/filter.d.ts +0 -31
- package/types/logtape/filter.d.ts.map +0 -1
- package/types/logtape/formatter.d.ts +0 -260
- package/types/logtape/formatter.d.ts.map +0 -1
- package/types/logtape/level.d.ts +0 -32
- package/types/logtape/logger.d.ts +0 -423
- package/types/logtape/logger.d.ts.map +0 -1
- package/types/logtape/mod.d.ts +0 -9
- package/types/logtape/mod.d.ts.map +0 -1
- package/types/logtape/nodeUtil.d.ts +0 -3
- package/types/logtape/nodeUtil.d.ts.map +0 -1
- package/types/logtape/record.d.ts +0 -44
- package/types/logtape/sink.d.ts +0 -108
- package/types/logtape/sink.d.ts.map +0 -1
package/script/logtape/config.js
DELETED
|
@@ -1,321 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.ConfigError = void 0;
|
|
27
|
-
exports.configure = configure;
|
|
28
|
-
exports.configureSync = configureSync;
|
|
29
|
-
exports.getConfig = getConfig;
|
|
30
|
-
exports.reset = reset;
|
|
31
|
-
exports.resetSync = resetSync;
|
|
32
|
-
exports.dispose = dispose;
|
|
33
|
-
exports.disposeSync = disposeSync;
|
|
34
|
-
const dntShim = __importStar(require("../_dnt.shims.js"));
|
|
35
|
-
const filter_js_1 = require("./filter.js");
|
|
36
|
-
const logger_js_1 = require("./logger.js");
|
|
37
|
-
const sink_js_1 = require("./sink.js");
|
|
38
|
-
/**
|
|
39
|
-
* The current configuration, if any. Otherwise, `null`.
|
|
40
|
-
*/
|
|
41
|
-
let currentConfig = null;
|
|
42
|
-
/**
|
|
43
|
-
* Strong references to the loggers.
|
|
44
|
-
* This is to prevent the loggers from being garbage collected so that their
|
|
45
|
-
* sinks and filters are not removed.
|
|
46
|
-
*/
|
|
47
|
-
const strongRefs = new Set();
|
|
48
|
-
/**
|
|
49
|
-
* Disposables to dispose when resetting the configuration.
|
|
50
|
-
*/
|
|
51
|
-
const disposables = new Set();
|
|
52
|
-
/**
|
|
53
|
-
* Async disposables to dispose when resetting the configuration.
|
|
54
|
-
*/
|
|
55
|
-
const asyncDisposables = new Set();
|
|
56
|
-
/**
|
|
57
|
-
* Check if a config is for the meta logger.
|
|
58
|
-
*/
|
|
59
|
-
function isLoggerConfigMeta(cfg) {
|
|
60
|
-
return cfg.category.length === 0 ||
|
|
61
|
-
(cfg.category.length === 1 && cfg.category[0] === "logtape") ||
|
|
62
|
-
(cfg.category.length === 2 &&
|
|
63
|
-
cfg.category[0] === "logtape" &&
|
|
64
|
-
cfg.category[1] === "meta");
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Configure the loggers with the specified configuration.
|
|
68
|
-
*
|
|
69
|
-
* Note that if the given sinks or filters are disposable, they will be
|
|
70
|
-
* disposed when the configuration is reset, or when the process exits.
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
* ```typescript
|
|
74
|
-
* await configure({
|
|
75
|
-
* sinks: {
|
|
76
|
-
* console: getConsoleSink(),
|
|
77
|
-
* },
|
|
78
|
-
* filters: {
|
|
79
|
-
* slow: (log) =>
|
|
80
|
-
* "duration" in log.properties &&
|
|
81
|
-
* log.properties.duration as number > 1000,
|
|
82
|
-
* },
|
|
83
|
-
* loggers: [
|
|
84
|
-
* {
|
|
85
|
-
* category: "my-app",
|
|
86
|
-
* sinks: ["console"],
|
|
87
|
-
* level: "info",
|
|
88
|
-
* },
|
|
89
|
-
* {
|
|
90
|
-
* category: ["my-app", "sql"],
|
|
91
|
-
* filters: ["slow"],
|
|
92
|
-
* level: "debug",
|
|
93
|
-
* },
|
|
94
|
-
* {
|
|
95
|
-
* category: "logtape",
|
|
96
|
-
* sinks: ["console"],
|
|
97
|
-
* level: "error",
|
|
98
|
-
* },
|
|
99
|
-
* ],
|
|
100
|
-
* });
|
|
101
|
-
* ```
|
|
102
|
-
*
|
|
103
|
-
* @param config The configuration.
|
|
104
|
-
*/
|
|
105
|
-
async function configure(config) {
|
|
106
|
-
if (currentConfig != null && !config.reset) {
|
|
107
|
-
throw new ConfigError("Already configured; if you want to reset, turn on the reset flag.");
|
|
108
|
-
}
|
|
109
|
-
await reset();
|
|
110
|
-
try {
|
|
111
|
-
configureInternal(config, true);
|
|
112
|
-
}
|
|
113
|
-
catch (e) {
|
|
114
|
-
if (e instanceof ConfigError)
|
|
115
|
-
await reset();
|
|
116
|
-
throw e;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Configure sync loggers with the specified configuration.
|
|
121
|
-
*
|
|
122
|
-
* Note that if the given sinks or filters are disposable, they will be
|
|
123
|
-
* disposed when the configuration is reset, or when the process exits.
|
|
124
|
-
*
|
|
125
|
-
* Also note that passing async sinks or filters will throw. If
|
|
126
|
-
* necessary use {@link resetSync} or {@link disposeSync}.
|
|
127
|
-
*
|
|
128
|
-
* @example
|
|
129
|
-
* ```typescript
|
|
130
|
-
* configureSync({
|
|
131
|
-
* sinks: {
|
|
132
|
-
* console: getConsoleSink(),
|
|
133
|
-
* },
|
|
134
|
-
* loggers: [
|
|
135
|
-
* {
|
|
136
|
-
* category: "my-app",
|
|
137
|
-
* sinks: ["console"],
|
|
138
|
-
* level: "info",
|
|
139
|
-
* },
|
|
140
|
-
* {
|
|
141
|
-
* category: "logtape",
|
|
142
|
-
* sinks: ["console"],
|
|
143
|
-
* level: "error",
|
|
144
|
-
* },
|
|
145
|
-
* ],
|
|
146
|
-
* });
|
|
147
|
-
* ```
|
|
148
|
-
*
|
|
149
|
-
* @param config The configuration.
|
|
150
|
-
* @since 0.9.0
|
|
151
|
-
*/
|
|
152
|
-
function configureSync(config) {
|
|
153
|
-
if (currentConfig != null && !config.reset) {
|
|
154
|
-
throw new ConfigError("Already configured; if you want to reset, turn on the reset flag.");
|
|
155
|
-
}
|
|
156
|
-
if (asyncDisposables.size > 0) {
|
|
157
|
-
throw new ConfigError("Previously configured async disposables are still active. " +
|
|
158
|
-
"Use configure() instead or explicitly dispose them using dispose().");
|
|
159
|
-
}
|
|
160
|
-
resetSync();
|
|
161
|
-
try {
|
|
162
|
-
configureInternal(config, false);
|
|
163
|
-
}
|
|
164
|
-
catch (e) {
|
|
165
|
-
if (e instanceof ConfigError)
|
|
166
|
-
resetSync();
|
|
167
|
-
throw e;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
function configureInternal(config, allowAsync) {
|
|
171
|
-
currentConfig = config;
|
|
172
|
-
let metaConfigured = false;
|
|
173
|
-
let levelUsed = false;
|
|
174
|
-
for (const cfg of config.loggers) {
|
|
175
|
-
if (isLoggerConfigMeta(cfg)) {
|
|
176
|
-
metaConfigured = true;
|
|
177
|
-
}
|
|
178
|
-
const logger = logger_js_1.LoggerImpl.getLogger(cfg.category);
|
|
179
|
-
for (const sinkId of cfg.sinks ?? []) {
|
|
180
|
-
const sink = config.sinks[sinkId];
|
|
181
|
-
if (!sink) {
|
|
182
|
-
throw new ConfigError(`Sink not found: ${sinkId}.`);
|
|
183
|
-
}
|
|
184
|
-
logger.sinks.push(sink);
|
|
185
|
-
}
|
|
186
|
-
logger.parentSinks = cfg.parentSinks ?? "inherit";
|
|
187
|
-
if (cfg.lowestLevel !== undefined) {
|
|
188
|
-
logger.lowestLevel = cfg.lowestLevel;
|
|
189
|
-
}
|
|
190
|
-
if (cfg.level !== undefined) {
|
|
191
|
-
levelUsed = true;
|
|
192
|
-
logger.filters.push((0, filter_js_1.toFilter)(cfg.level));
|
|
193
|
-
}
|
|
194
|
-
for (const filterId of cfg.filters ?? []) {
|
|
195
|
-
const filter = config.filters?.[filterId];
|
|
196
|
-
if (filter === undefined) {
|
|
197
|
-
throw new ConfigError(`Filter not found: ${filterId}.`);
|
|
198
|
-
}
|
|
199
|
-
logger.filters.push((0, filter_js_1.toFilter)(filter));
|
|
200
|
-
}
|
|
201
|
-
strongRefs.add(logger);
|
|
202
|
-
}
|
|
203
|
-
logger_js_1.LoggerImpl.getLogger().contextLocalStorage = config.contextLocalStorage;
|
|
204
|
-
for (const sink of Object.values(config.sinks)) {
|
|
205
|
-
if (Symbol.asyncDispose in sink) {
|
|
206
|
-
if (allowAsync)
|
|
207
|
-
asyncDisposables.add(sink);
|
|
208
|
-
else {
|
|
209
|
-
throw new ConfigError("Async disposables cannot be used with configureSync().");
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
if (Symbol.dispose in sink)
|
|
213
|
-
disposables.add(sink);
|
|
214
|
-
}
|
|
215
|
-
for (const filter of Object.values(config.filters ?? {})) {
|
|
216
|
-
if (filter == null || typeof filter === "string")
|
|
217
|
-
continue;
|
|
218
|
-
if (Symbol.asyncDispose in filter) {
|
|
219
|
-
if (allowAsync)
|
|
220
|
-
asyncDisposables.add(filter);
|
|
221
|
-
else {
|
|
222
|
-
throw new ConfigError("Async disposables cannot be used with configureSync().");
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
if (Symbol.dispose in filter)
|
|
226
|
-
disposables.add(filter);
|
|
227
|
-
}
|
|
228
|
-
if ("process" in dntShim.dntGlobalThis && !("Deno" in dntShim.dntGlobalThis)) {
|
|
229
|
-
// @ts-ignore: It's fine to use process in Node
|
|
230
|
-
// deno-lint-ignore no-process-global
|
|
231
|
-
process.on("exit", allowAsync ? dispose : disposeSync);
|
|
232
|
-
}
|
|
233
|
-
else {
|
|
234
|
-
// @ts-ignore: It's fine to addEventListener() on the browser/Deno
|
|
235
|
-
addEventListener("unload", allowAsync ? dispose : disposeSync);
|
|
236
|
-
}
|
|
237
|
-
const meta = logger_js_1.LoggerImpl.getLogger(["logtape", "meta"]);
|
|
238
|
-
if (!metaConfigured) {
|
|
239
|
-
meta.sinks.push((0, sink_js_1.getConsoleSink)());
|
|
240
|
-
}
|
|
241
|
-
meta.info("LogTape loggers are configured. Note that LogTape itself uses the meta " +
|
|
242
|
-
"logger, which has category {metaLoggerCategory}. The meta logger " +
|
|
243
|
-
"purposes to log internal errors such as sink exceptions. If you " +
|
|
244
|
-
"are seeing this message, the meta logger is automatically configured. " +
|
|
245
|
-
"It's recommended to configure the meta logger with a separate sink " +
|
|
246
|
-
"so that you can easily notice if logging itself fails or is " +
|
|
247
|
-
"misconfigured. To turn off this message, configure the meta logger " +
|
|
248
|
-
"with higher log levels than {dismissLevel}. See also " +
|
|
249
|
-
"<https://logtape.org/manual/categories#meta-logger>.", { metaLoggerCategory: ["logtape", "meta"], dismissLevel: "info" });
|
|
250
|
-
if (levelUsed) {
|
|
251
|
-
meta.warn("The level option is deprecated in favor of lowestLevel option. " +
|
|
252
|
-
"Please update your configuration. See also " +
|
|
253
|
-
"<https://logtape.org/manual/levels#configuring-severity-levels>.");
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
/**
|
|
257
|
-
* Get the current configuration, if any. Otherwise, `null`.
|
|
258
|
-
* @returns The current configuration, if any. Otherwise, `null`.
|
|
259
|
-
*/
|
|
260
|
-
function getConfig() {
|
|
261
|
-
return currentConfig;
|
|
262
|
-
}
|
|
263
|
-
/**
|
|
264
|
-
* Reset the configuration. Mostly for testing purposes.
|
|
265
|
-
*/
|
|
266
|
-
async function reset() {
|
|
267
|
-
await dispose();
|
|
268
|
-
resetInternal();
|
|
269
|
-
}
|
|
270
|
-
/**
|
|
271
|
-
* Reset the configuration. Mostly for testing purposes. Will not clear async
|
|
272
|
-
* sinks, only use with sync sinks. Use {@link reset} if you have async sinks.
|
|
273
|
-
* @since 0.9.0
|
|
274
|
-
*/
|
|
275
|
-
function resetSync() {
|
|
276
|
-
disposeSync();
|
|
277
|
-
resetInternal();
|
|
278
|
-
}
|
|
279
|
-
function resetInternal() {
|
|
280
|
-
const rootLogger = logger_js_1.LoggerImpl.getLogger([]);
|
|
281
|
-
rootLogger.resetDescendants();
|
|
282
|
-
delete rootLogger.contextLocalStorage;
|
|
283
|
-
strongRefs.clear();
|
|
284
|
-
currentConfig = null;
|
|
285
|
-
}
|
|
286
|
-
/**
|
|
287
|
-
* Dispose of the disposables.
|
|
288
|
-
*/
|
|
289
|
-
async function dispose() {
|
|
290
|
-
disposeSync();
|
|
291
|
-
const promises = [];
|
|
292
|
-
for (const disposable of asyncDisposables) {
|
|
293
|
-
promises.push(disposable[Symbol.asyncDispose]());
|
|
294
|
-
asyncDisposables.delete(disposable);
|
|
295
|
-
}
|
|
296
|
-
await Promise.all(promises);
|
|
297
|
-
}
|
|
298
|
-
/**
|
|
299
|
-
* Dispose of the sync disposables. Async disposables will be untouched,
|
|
300
|
-
* use {@link dispose} if you have async sinks.
|
|
301
|
-
* @since 0.9.0
|
|
302
|
-
*/
|
|
303
|
-
function disposeSync() {
|
|
304
|
-
for (const disposable of disposables)
|
|
305
|
-
disposable[Symbol.dispose]();
|
|
306
|
-
disposables.clear();
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* A configuration error.
|
|
310
|
-
*/
|
|
311
|
-
class ConfigError extends Error {
|
|
312
|
-
/**
|
|
313
|
-
* Constructs a new configuration error.
|
|
314
|
-
* @param message The error message.
|
|
315
|
-
*/
|
|
316
|
-
constructor(message) {
|
|
317
|
-
super(message);
|
|
318
|
-
this.name = "ConfigureError";
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
exports.ConfigError = ConfigError;
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.withContext = withContext;
|
|
4
|
-
const logger_js_1 = require("./logger.js");
|
|
5
|
-
/**
|
|
6
|
-
* Runs a callback with the given implicit context. Every single log record
|
|
7
|
-
* in the callback will have the given context.
|
|
8
|
-
*
|
|
9
|
-
* If no `contextLocalStorage` is configured, this function does nothing and
|
|
10
|
-
* just returns the return value of the callback. It also logs a warning to
|
|
11
|
-
* the `["logtape", "meta"]` logger in this case.
|
|
12
|
-
* @param context The context to inject.
|
|
13
|
-
* @param callback The callback to run.
|
|
14
|
-
* @returns The return value of the callback.
|
|
15
|
-
* @since 0.7.0
|
|
16
|
-
*/
|
|
17
|
-
function withContext(context, callback) {
|
|
18
|
-
const rootLogger = logger_js_1.LoggerImpl.getLogger();
|
|
19
|
-
if (rootLogger.contextLocalStorage == null) {
|
|
20
|
-
logger_js_1.LoggerImpl.getLogger(["logtape", "meta"]).warn("Context-local storage is not configured. " +
|
|
21
|
-
"Specify contextLocalStorage option in the configure() function.");
|
|
22
|
-
return callback();
|
|
23
|
-
}
|
|
24
|
-
const parentContext = rootLogger.contextLocalStorage.getStore() ?? {};
|
|
25
|
-
return rootLogger.contextLocalStorage.run({ ...parentContext, ...context }, callback);
|
|
26
|
-
}
|
package/script/logtape/filter.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toFilter = toFilter;
|
|
4
|
-
exports.getLevelFilter = getLevelFilter;
|
|
5
|
-
/**
|
|
6
|
-
* Converts a {@link FilterLike} value to an actual {@link Filter}.
|
|
7
|
-
*
|
|
8
|
-
* @param filter The filter-like value to convert.
|
|
9
|
-
* @returns The actual filter.
|
|
10
|
-
*/
|
|
11
|
-
function toFilter(filter) {
|
|
12
|
-
if (typeof filter === "function")
|
|
13
|
-
return filter;
|
|
14
|
-
return getLevelFilter(filter);
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Returns a filter that accepts log records with the specified level.
|
|
18
|
-
*
|
|
19
|
-
* @param level The level to filter by. If `null`, the filter will reject all
|
|
20
|
-
* records.
|
|
21
|
-
* @returns The filter.
|
|
22
|
-
*/
|
|
23
|
-
function getLevelFilter(level) {
|
|
24
|
-
if (level == null)
|
|
25
|
-
return () => false;
|
|
26
|
-
if (level === "fatal") {
|
|
27
|
-
return (record) => record.level === "fatal";
|
|
28
|
-
}
|
|
29
|
-
else if (level === "error") {
|
|
30
|
-
return (record) => record.level === "fatal" || record.level === "error";
|
|
31
|
-
}
|
|
32
|
-
else if (level === "warning") {
|
|
33
|
-
return (record) => record.level === "fatal" ||
|
|
34
|
-
record.level === "error" ||
|
|
35
|
-
record.level === "warning";
|
|
36
|
-
}
|
|
37
|
-
else if (level === "info") {
|
|
38
|
-
return (record) => record.level === "fatal" ||
|
|
39
|
-
record.level === "error" ||
|
|
40
|
-
record.level === "warning" ||
|
|
41
|
-
record.level === "info";
|
|
42
|
-
}
|
|
43
|
-
else if (level === "debug")
|
|
44
|
-
return () => true;
|
|
45
|
-
throw new TypeError(`Invalid log level: ${level}.`);
|
|
46
|
-
}
|
|
@@ -1,270 +0,0 @@
|
|
|
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
|
-
* 
|
|
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
|
-
* 
|
|
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
|
-
}
|