@logtape/logtape 0.11.0 → 0.12.0-dev.181
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/config.test.ts +591 -0
- package/config.ts +421 -0
- package/context.test.ts +187 -0
- package/context.ts +55 -0
- package/deno.json +36 -0
- package/dist/_virtual/rolldown_runtime.cjs +30 -0
- package/dist/config.cjs +247 -0
- package/dist/config.d.cts +189 -0
- package/dist/config.d.cts.map +1 -0
- package/dist/config.d.ts +189 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +241 -0
- package/dist/config.js.map +1 -0
- package/dist/context.cjs +30 -0
- package/dist/context.d.cts +39 -0
- package/dist/context.d.cts.map +1 -0
- package/dist/context.d.ts +39 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +31 -0
- package/dist/context.js.map +1 -0
- package/dist/filter.cjs +32 -0
- package/dist/filter.d.cts +37 -0
- package/dist/filter.d.cts.map +1 -0
- package/{types → dist}/filter.d.ts +12 -6
- package/dist/filter.d.ts.map +1 -0
- package/dist/filter.js +31 -0
- package/dist/filter.js.map +1 -0
- package/dist/formatter.cjs +281 -0
- package/dist/formatter.d.cts +338 -0
- package/dist/formatter.d.cts.map +1 -0
- package/dist/formatter.d.ts +338 -0
- package/dist/formatter.d.ts.map +1 -0
- package/dist/formatter.js +275 -0
- package/dist/formatter.js.map +1 -0
- package/dist/level.cjs +64 -0
- package/dist/level.d.cts +34 -0
- package/dist/level.d.cts.map +1 -0
- package/{types → dist}/level.d.ts +7 -5
- package/dist/level.d.ts.map +1 -0
- package/dist/level.js +62 -0
- package/dist/level.js.map +1 -0
- package/dist/logger.cjs +351 -0
- package/dist/logger.d.cts +501 -0
- package/dist/logger.d.cts.map +1 -0
- package/dist/logger.d.ts +501 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +351 -0
- package/dist/logger.js.map +1 -0
- package/dist/mod.cjs +33 -0
- package/dist/mod.d.cts +9 -0
- package/dist/mod.d.ts +9 -0
- package/dist/mod.js +9 -0
- package/dist/record.d.cts +50 -0
- package/dist/record.d.cts.map +1 -0
- package/dist/record.d.ts +50 -0
- package/dist/record.d.ts.map +1 -0
- package/dist/sink.cjs +95 -0
- package/dist/sink.d.cts +112 -0
- package/dist/sink.d.cts.map +1 -0
- package/{types → dist}/sink.d.ts +49 -45
- package/dist/sink.d.ts.map +1 -0
- package/dist/sink.js +94 -0
- package/dist/sink.js.map +1 -0
- package/dist/util.cjs +9 -0
- package/dist/util.d.cts +12 -0
- package/dist/util.d.cts.map +1 -0
- package/dist/util.d.ts +12 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.deno.cjs +16 -0
- package/dist/util.deno.d.cts +12 -0
- package/dist/util.deno.d.cts.map +1 -0
- package/dist/util.deno.d.ts +12 -0
- package/dist/util.deno.d.ts.map +1 -0
- package/dist/util.deno.js +16 -0
- package/dist/util.deno.js.map +1 -0
- package/dist/util.js +9 -0
- package/dist/util.js.map +1 -0
- package/dist/util.node.cjs +10 -0
- package/dist/util.node.d.cts +12 -0
- package/dist/util.node.d.cts.map +1 -0
- package/dist/util.node.d.ts +12 -0
- package/dist/util.node.d.ts.map +1 -0
- package/dist/util.node.js +10 -0
- package/dist/util.node.js.map +1 -0
- package/filter.test.ts +70 -0
- package/filter.ts +57 -0
- package/fixtures.ts +30 -0
- package/formatter.test.ts +530 -0
- package/formatter.ts +724 -0
- package/level.test.ts +47 -0
- package/level.ts +67 -0
- package/logger.test.ts +823 -0
- package/logger.ts +1124 -0
- package/mod.ts +54 -0
- package/package.json +35 -23
- package/record.ts +49 -0
- package/sink.test.ts +219 -0
- package/sink.ts +167 -0
- package/tsdown.config.ts +24 -0
- package/util.deno.ts +19 -0
- package/util.node.ts +12 -0
- package/util.ts +11 -0
- package/esm/_dnt.shims.js +0 -57
- package/esm/config.js +0 -297
- package/esm/context.js +0 -23
- package/esm/filter.js +0 -42
- package/esm/formatter.js +0 -370
- package/esm/level.js +0 -59
- package/esm/logger.js +0 -517
- package/esm/mod.js +0 -8
- package/esm/nodeUtil.cjs +0 -20
- package/esm/nodeUtil.js +0 -2
- package/esm/package.json +0 -3
- package/esm/record.js +0 -1
- package/esm/sink.js +0 -96
- package/script/_dnt.shims.js +0 -60
- package/script/config.js +0 -331
- package/script/context.js +0 -26
- package/script/filter.js +0 -46
- package/script/formatter.js +0 -380
- package/script/level.js +0 -64
- package/script/logger.js +0 -548
- package/script/mod.js +0 -36
- package/script/nodeUtil.js +0 -20
- package/script/package.json +0 -3
- package/script/record.js +0 -2
- package/script/sink.js +0 -101
- package/types/_dnt.shims.d.ts +0 -2
- package/types/_dnt.shims.d.ts.map +0 -1
- package/types/_dnt.test_shims.d.ts.map +0 -1
- package/types/config.d.ts +0 -183
- package/types/config.d.ts.map +0 -1
- package/types/config.test.d.ts.map +0 -1
- package/types/context.d.ts +0 -35
- package/types/context.d.ts.map +0 -1
- package/types/context.test.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/_constants.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/_diff.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/_format.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert_equals.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert_false.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert_greater_or_equal.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert_is_error.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert_less_or_equal.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert_rejects.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert_strict_equals.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert_throws.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assertion_error.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/equal.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/async/0.222.1/delay.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/fmt/0.222.1/colors.d.ts.map +0 -1
- package/types/filter.d.ts.map +0 -1
- package/types/filter.test.d.ts.map +0 -1
- package/types/fixtures.d.ts.map +0 -1
- package/types/formatter.d.ts +0 -332
- package/types/formatter.d.ts.map +0 -1
- package/types/formatter.test.d.ts.map +0 -1
- package/types/level.d.ts.map +0 -1
- package/types/level.test.d.ts.map +0 -1
- package/types/logger.d.ts +0 -573
- package/types/logger.d.ts.map +0 -1
- package/types/logger.test.d.ts.map +0 -1
- package/types/mod.d.ts +0 -9
- package/types/mod.d.ts.map +0 -1
- package/types/nodeUtil.d.ts +0 -12
- package/types/nodeUtil.d.ts.map +0 -1
- package/types/record.d.ts +0 -44
- package/types/record.d.ts.map +0 -1
- package/types/sink.d.ts.map +0 -1
- package/types/sink.test.d.ts.map +0 -1
package/dist/config.js
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { toFilter } from "./filter.js";
|
|
2
|
+
import { LoggerImpl } from "./logger.js";
|
|
3
|
+
import { getConsoleSink } from "./sink.js";
|
|
4
|
+
|
|
5
|
+
//#region config.ts
|
|
6
|
+
/**
|
|
7
|
+
* The current configuration, if any. Otherwise, `null`.
|
|
8
|
+
*/
|
|
9
|
+
let currentConfig = null;
|
|
10
|
+
/**
|
|
11
|
+
* Strong references to the loggers.
|
|
12
|
+
* This is to prevent the loggers from being garbage collected so that their
|
|
13
|
+
* sinks and filters are not removed.
|
|
14
|
+
*/
|
|
15
|
+
const strongRefs = /* @__PURE__ */ new Set();
|
|
16
|
+
/**
|
|
17
|
+
* Disposables to dispose when resetting the configuration.
|
|
18
|
+
*/
|
|
19
|
+
const disposables = /* @__PURE__ */ new Set();
|
|
20
|
+
/**
|
|
21
|
+
* Async disposables to dispose when resetting the configuration.
|
|
22
|
+
*/
|
|
23
|
+
const asyncDisposables = /* @__PURE__ */ new Set();
|
|
24
|
+
/**
|
|
25
|
+
* Check if a config is for the meta logger.
|
|
26
|
+
*/
|
|
27
|
+
function isLoggerConfigMeta(cfg) {
|
|
28
|
+
return cfg.category.length === 0 || cfg.category.length === 1 && cfg.category[0] === "logtape" || cfg.category.length === 2 && cfg.category[0] === "logtape" && cfg.category[1] === "meta";
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Configure the loggers with the specified configuration.
|
|
32
|
+
*
|
|
33
|
+
* Note that if the given sinks or filters are disposable, they will be
|
|
34
|
+
* disposed when the configuration is reset, or when the process exits.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* await configure({
|
|
39
|
+
* sinks: {
|
|
40
|
+
* console: getConsoleSink(),
|
|
41
|
+
* },
|
|
42
|
+
* filters: {
|
|
43
|
+
* slow: (log) =>
|
|
44
|
+
* "duration" in log.properties &&
|
|
45
|
+
* log.properties.duration as number > 1000,
|
|
46
|
+
* },
|
|
47
|
+
* loggers: [
|
|
48
|
+
* {
|
|
49
|
+
* category: "my-app",
|
|
50
|
+
* sinks: ["console"],
|
|
51
|
+
* level: "info",
|
|
52
|
+
* },
|
|
53
|
+
* {
|
|
54
|
+
* category: ["my-app", "sql"],
|
|
55
|
+
* filters: ["slow"],
|
|
56
|
+
* level: "debug",
|
|
57
|
+
* },
|
|
58
|
+
* {
|
|
59
|
+
* category: "logtape",
|
|
60
|
+
* sinks: ["console"],
|
|
61
|
+
* level: "error",
|
|
62
|
+
* },
|
|
63
|
+
* ],
|
|
64
|
+
* });
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* @param config The configuration.
|
|
68
|
+
*/
|
|
69
|
+
async function configure(config) {
|
|
70
|
+
if (currentConfig != null && !config.reset) throw new ConfigError("Already configured; if you want to reset, turn on the reset flag.");
|
|
71
|
+
await reset();
|
|
72
|
+
try {
|
|
73
|
+
configureInternal(config, true);
|
|
74
|
+
} catch (e) {
|
|
75
|
+
if (e instanceof ConfigError) await reset();
|
|
76
|
+
throw e;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Configure sync loggers with the specified configuration.
|
|
81
|
+
*
|
|
82
|
+
* Note that if the given sinks or filters are disposable, they will be
|
|
83
|
+
* disposed when the configuration is reset, or when the process exits.
|
|
84
|
+
*
|
|
85
|
+
* Also note that passing async sinks or filters will throw. If
|
|
86
|
+
* necessary use {@link resetSync} or {@link disposeSync}.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```typescript
|
|
90
|
+
* configureSync({
|
|
91
|
+
* sinks: {
|
|
92
|
+
* console: getConsoleSink(),
|
|
93
|
+
* },
|
|
94
|
+
* loggers: [
|
|
95
|
+
* {
|
|
96
|
+
* category: "my-app",
|
|
97
|
+
* sinks: ["console"],
|
|
98
|
+
* level: "info",
|
|
99
|
+
* },
|
|
100
|
+
* {
|
|
101
|
+
* category: "logtape",
|
|
102
|
+
* sinks: ["console"],
|
|
103
|
+
* level: "error",
|
|
104
|
+
* },
|
|
105
|
+
* ],
|
|
106
|
+
* });
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* @param config The configuration.
|
|
110
|
+
* @since 0.9.0
|
|
111
|
+
*/
|
|
112
|
+
function configureSync(config) {
|
|
113
|
+
if (currentConfig != null && !config.reset) throw new ConfigError("Already configured; if you want to reset, turn on the reset flag.");
|
|
114
|
+
if (asyncDisposables.size > 0) throw new ConfigError("Previously configured async disposables are still active. Use configure() instead or explicitly dispose them using dispose().");
|
|
115
|
+
resetSync();
|
|
116
|
+
try {
|
|
117
|
+
configureInternal(config, false);
|
|
118
|
+
} catch (e) {
|
|
119
|
+
if (e instanceof ConfigError) resetSync();
|
|
120
|
+
throw e;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function configureInternal(config, allowAsync) {
|
|
124
|
+
currentConfig = config;
|
|
125
|
+
let metaConfigured = false;
|
|
126
|
+
let levelUsed = false;
|
|
127
|
+
const configuredCategories = /* @__PURE__ */ new Set();
|
|
128
|
+
for (const cfg of config.loggers) {
|
|
129
|
+
if (isLoggerConfigMeta(cfg)) metaConfigured = true;
|
|
130
|
+
const categoryKey = Array.isArray(cfg.category) ? JSON.stringify(cfg.category) : JSON.stringify([cfg.category]);
|
|
131
|
+
if (configuredCategories.has(categoryKey)) throw new ConfigError(`Duplicate logger configuration for category: ${categoryKey}. Each category can only be configured once.`);
|
|
132
|
+
configuredCategories.add(categoryKey);
|
|
133
|
+
const logger = LoggerImpl.getLogger(cfg.category);
|
|
134
|
+
for (const sinkId of cfg.sinks ?? []) {
|
|
135
|
+
const sink = config.sinks[sinkId];
|
|
136
|
+
if (!sink) throw new ConfigError(`Sink not found: ${sinkId}.`);
|
|
137
|
+
logger.sinks.push(sink);
|
|
138
|
+
}
|
|
139
|
+
logger.parentSinks = cfg.parentSinks ?? "inherit";
|
|
140
|
+
if (cfg.lowestLevel !== void 0) logger.lowestLevel = cfg.lowestLevel;
|
|
141
|
+
if (cfg.level !== void 0) {
|
|
142
|
+
levelUsed = true;
|
|
143
|
+
logger.filters.push(toFilter(cfg.level));
|
|
144
|
+
}
|
|
145
|
+
for (const filterId of cfg.filters ?? []) {
|
|
146
|
+
const filter = config.filters?.[filterId];
|
|
147
|
+
if (filter === void 0) throw new ConfigError(`Filter not found: ${filterId}.`);
|
|
148
|
+
logger.filters.push(toFilter(filter));
|
|
149
|
+
}
|
|
150
|
+
strongRefs.add(logger);
|
|
151
|
+
}
|
|
152
|
+
LoggerImpl.getLogger().contextLocalStorage = config.contextLocalStorage;
|
|
153
|
+
for (const sink of Object.values(config.sinks)) {
|
|
154
|
+
if (Symbol.asyncDispose in sink) if (allowAsync) asyncDisposables.add(sink);
|
|
155
|
+
else throw new ConfigError("Async disposables cannot be used with configureSync().");
|
|
156
|
+
if (Symbol.dispose in sink) disposables.add(sink);
|
|
157
|
+
}
|
|
158
|
+
for (const filter of Object.values(config.filters ?? {})) {
|
|
159
|
+
if (filter == null || typeof filter === "string") continue;
|
|
160
|
+
if (Symbol.asyncDispose in filter) if (allowAsync) asyncDisposables.add(filter);
|
|
161
|
+
else throw new ConfigError("Async disposables cannot be used with configureSync().");
|
|
162
|
+
if (Symbol.dispose in filter) disposables.add(filter);
|
|
163
|
+
}
|
|
164
|
+
if ("process" in globalThis && !("Deno" in globalThis)) process.on("exit", allowAsync ? dispose : disposeSync);
|
|
165
|
+
else addEventListener("unload", allowAsync ? dispose : disposeSync);
|
|
166
|
+
const meta = LoggerImpl.getLogger(["logtape", "meta"]);
|
|
167
|
+
if (!metaConfigured) meta.sinks.push(getConsoleSink());
|
|
168
|
+
meta.info("LogTape loggers are configured. Note that LogTape itself uses the meta logger, which has category {metaLoggerCategory}. The meta logger purposes to log internal errors such as sink exceptions. If you are seeing this message, the meta logger is automatically configured. It's recommended to configure the meta logger with a separate sink so that you can easily notice if logging itself fails or is misconfigured. To turn off this message, configure the meta logger with higher log levels than {dismissLevel}. See also <https://logtape.org/manual/categories#meta-logger>.", {
|
|
169
|
+
metaLoggerCategory: ["logtape", "meta"],
|
|
170
|
+
dismissLevel: "info"
|
|
171
|
+
});
|
|
172
|
+
if (levelUsed) meta.warn("The level option is deprecated in favor of lowestLevel option. Please update your configuration. See also <https://logtape.org/manual/levels#configuring-severity-levels>.");
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Get the current configuration, if any. Otherwise, `null`.
|
|
176
|
+
* @returns The current configuration, if any. Otherwise, `null`.
|
|
177
|
+
*/
|
|
178
|
+
function getConfig() {
|
|
179
|
+
return currentConfig;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Reset the configuration. Mostly for testing purposes.
|
|
183
|
+
*/
|
|
184
|
+
async function reset() {
|
|
185
|
+
await dispose();
|
|
186
|
+
resetInternal();
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Reset the configuration. Mostly for testing purposes. Will not clear async
|
|
190
|
+
* sinks, only use with sync sinks. Use {@link reset} if you have async sinks.
|
|
191
|
+
* @since 0.9.0
|
|
192
|
+
*/
|
|
193
|
+
function resetSync() {
|
|
194
|
+
disposeSync();
|
|
195
|
+
resetInternal();
|
|
196
|
+
}
|
|
197
|
+
function resetInternal() {
|
|
198
|
+
const rootLogger = LoggerImpl.getLogger([]);
|
|
199
|
+
rootLogger.resetDescendants();
|
|
200
|
+
delete rootLogger.contextLocalStorage;
|
|
201
|
+
strongRefs.clear();
|
|
202
|
+
currentConfig = null;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Dispose of the disposables.
|
|
206
|
+
*/
|
|
207
|
+
async function dispose() {
|
|
208
|
+
disposeSync();
|
|
209
|
+
const promises = [];
|
|
210
|
+
for (const disposable of asyncDisposables) {
|
|
211
|
+
promises.push(disposable[Symbol.asyncDispose]());
|
|
212
|
+
asyncDisposables.delete(disposable);
|
|
213
|
+
}
|
|
214
|
+
await Promise.all(promises);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Dispose of the sync disposables. Async disposables will be untouched,
|
|
218
|
+
* use {@link dispose} if you have async sinks.
|
|
219
|
+
* @since 0.9.0
|
|
220
|
+
*/
|
|
221
|
+
function disposeSync() {
|
|
222
|
+
for (const disposable of disposables) disposable[Symbol.dispose]();
|
|
223
|
+
disposables.clear();
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* A configuration error.
|
|
227
|
+
*/
|
|
228
|
+
var ConfigError = class extends Error {
|
|
229
|
+
/**
|
|
230
|
+
* Constructs a new configuration error.
|
|
231
|
+
* @param message The error message.
|
|
232
|
+
*/
|
|
233
|
+
constructor(message) {
|
|
234
|
+
super(message);
|
|
235
|
+
this.name = "ConfigureError";
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
//#endregion
|
|
240
|
+
export { ConfigError, configure, configureSync, dispose, disposeSync, getConfig, reset, resetSync };
|
|
241
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","names":["currentConfig: Config<string, string> | null","strongRefs: Set<LoggerImpl>","disposables: Set<Disposable>","asyncDisposables: Set<AsyncDisposable>","cfg: LoggerConfig<TSinkId, TFilterId>","config: Config<TSinkId, TFilterId>","allowAsync: boolean","promises: PromiseLike<void>[]","message: string"],"sources":["../config.ts"],"sourcesContent":["import type { ContextLocalStorage } from \"./context.ts\";\nimport { type FilterLike, toFilter } from \"./filter.ts\";\nimport type { LogLevel } from \"./level.ts\";\nimport { LoggerImpl } from \"./logger.ts\";\nimport { getConsoleSink, type Sink } from \"./sink.ts\";\n\n/**\n * A configuration for the loggers.\n */\nexport interface Config<TSinkId extends string, TFilterId extends string> {\n /**\n * The sinks to use. The keys are the sink identifiers, and the values are\n * {@link Sink}s.\n */\n sinks: Record<TSinkId, Sink>;\n /**\n * The filters to use. The keys are the filter identifiers, and the values\n * are either {@link Filter}s or {@link LogLevel}s.\n */\n filters?: Record<TFilterId, FilterLike>;\n\n /**\n * The loggers to configure.\n */\n loggers: LoggerConfig<TSinkId, TFilterId>[];\n\n /**\n * The context-local storage to use for implicit contexts.\n * @since 0.7.0\n */\n contextLocalStorage?: ContextLocalStorage<Record<string, unknown>>;\n\n /**\n * Whether to reset the configuration before applying this one.\n */\n reset?: boolean;\n}\n\n/**\n * A logger configuration.\n */\nexport interface LoggerConfig<\n TSinkId extends string,\n TFilterId extends string,\n> {\n /**\n * The category of the logger. If a string, it is equivalent to an array\n * with one element.\n */\n category: string | string[];\n\n /**\n * The sink identifiers to use.\n */\n sinks?: TSinkId[];\n\n /**\n * Whether to inherit the parent's sinks. If `inherit`, the parent's sinks\n * are used along with the specified sinks. If `override`, the parent's\n * sinks are not used, and only the specified sinks are used.\n *\n * The default is `inherit`.\n * @default `\"inherit\"\n * @since 0.6.0\n */\n parentSinks?: \"inherit\" | \"override\";\n\n /**\n * The filter identifiers to use.\n */\n filters?: TFilterId[];\n\n /**\n * The log level to filter by. If `null`, the logger will reject all\n * records.\n * @deprecated Use `filters` instead for backward compatibility, or use\n * `lowestLevel` for less-misleading behavior.\n */\n level?: LogLevel | null;\n\n /**\n * The lowest log level to accept. If `null`, the logger will reject all\n * records.\n * @since 0.8.0\n */\n lowestLevel?: LogLevel | null;\n}\n\n/**\n * The current configuration, if any. Otherwise, `null`.\n */\nlet currentConfig: Config<string, string> | null = null;\n\n/**\n * Strong references to the loggers.\n * This is to prevent the loggers from being garbage collected so that their\n * sinks and filters are not removed.\n */\nconst strongRefs: Set<LoggerImpl> = new Set();\n\n/**\n * Disposables to dispose when resetting the configuration.\n */\nconst disposables: Set<Disposable> = new Set();\n\n/**\n * Async disposables to dispose when resetting the configuration.\n */\nconst asyncDisposables: Set<AsyncDisposable> = new Set();\n\n/**\n * Check if a config is for the meta logger.\n */\nfunction isLoggerConfigMeta<TSinkId extends string, TFilterId extends string>(\n cfg: LoggerConfig<TSinkId, TFilterId>,\n): boolean {\n return cfg.category.length === 0 ||\n (cfg.category.length === 1 && cfg.category[0] === \"logtape\") ||\n (cfg.category.length === 2 &&\n cfg.category[0] === \"logtape\" &&\n cfg.category[1] === \"meta\");\n}\n\n/**\n * Configure the loggers with the specified configuration.\n *\n * Note that if the given sinks or filters are disposable, they will be\n * disposed when the configuration is reset, or when the process exits.\n *\n * @example\n * ```typescript\n * await configure({\n * sinks: {\n * console: getConsoleSink(),\n * },\n * filters: {\n * slow: (log) =>\n * \"duration\" in log.properties &&\n * log.properties.duration as number > 1000,\n * },\n * loggers: [\n * {\n * category: \"my-app\",\n * sinks: [\"console\"],\n * level: \"info\",\n * },\n * {\n * category: [\"my-app\", \"sql\"],\n * filters: [\"slow\"],\n * level: \"debug\",\n * },\n * {\n * category: \"logtape\",\n * sinks: [\"console\"],\n * level: \"error\",\n * },\n * ],\n * });\n * ```\n *\n * @param config The configuration.\n */\nexport async function configure<\n TSinkId extends string,\n TFilterId extends string,\n>(config: Config<TSinkId, TFilterId>): Promise<void> {\n if (currentConfig != null && !config.reset) {\n throw new ConfigError(\n \"Already configured; if you want to reset, turn on the reset flag.\",\n );\n }\n await reset();\n try {\n configureInternal(config, true);\n } catch (e) {\n if (e instanceof ConfigError) await reset();\n throw e;\n }\n}\n\n/**\n * Configure sync loggers with the specified configuration.\n *\n * Note that if the given sinks or filters are disposable, they will be\n * disposed when the configuration is reset, or when the process exits.\n *\n * Also note that passing async sinks or filters will throw. If\n * necessary use {@link resetSync} or {@link disposeSync}.\n *\n * @example\n * ```typescript\n * configureSync({\n * sinks: {\n * console: getConsoleSink(),\n * },\n * loggers: [\n * {\n * category: \"my-app\",\n * sinks: [\"console\"],\n * level: \"info\",\n * },\n * {\n * category: \"logtape\",\n * sinks: [\"console\"],\n * level: \"error\",\n * },\n * ],\n * });\n * ```\n *\n * @param config The configuration.\n * @since 0.9.0\n */\nexport function configureSync<TSinkId extends string, TFilterId extends string>(\n config: Config<TSinkId, TFilterId>,\n): void {\n if (currentConfig != null && !config.reset) {\n throw new ConfigError(\n \"Already configured; if you want to reset, turn on the reset flag.\",\n );\n }\n if (asyncDisposables.size > 0) {\n throw new ConfigError(\n \"Previously configured async disposables are still active. \" +\n \"Use configure() instead or explicitly dispose them using dispose().\",\n );\n }\n resetSync();\n try {\n configureInternal(config, false);\n } catch (e) {\n if (e instanceof ConfigError) resetSync();\n throw e;\n }\n}\n\nfunction configureInternal<\n TSinkId extends string,\n TFilterId extends string,\n>(config: Config<TSinkId, TFilterId>, allowAsync: boolean): void {\n currentConfig = config;\n\n let metaConfigured = false;\n let levelUsed = false;\n const configuredCategories = new Set<string>();\n\n for (const cfg of config.loggers) {\n if (isLoggerConfigMeta(cfg)) {\n metaConfigured = true;\n }\n\n // Check for duplicate logger categories\n const categoryKey = Array.isArray(cfg.category)\n ? JSON.stringify(cfg.category)\n : JSON.stringify([cfg.category]);\n if (configuredCategories.has(categoryKey)) {\n throw new ConfigError(\n `Duplicate logger configuration for category: ${categoryKey}. ` +\n `Each category can only be configured once.`,\n );\n }\n configuredCategories.add(categoryKey);\n\n const logger = LoggerImpl.getLogger(cfg.category);\n for (const sinkId of cfg.sinks ?? []) {\n const sink = config.sinks[sinkId];\n if (!sink) {\n throw new ConfigError(`Sink not found: ${sinkId}.`);\n }\n logger.sinks.push(sink);\n }\n logger.parentSinks = cfg.parentSinks ?? \"inherit\";\n if (cfg.lowestLevel !== undefined) {\n logger.lowestLevel = cfg.lowestLevel;\n }\n if (cfg.level !== undefined) {\n levelUsed = true;\n logger.filters.push(toFilter(cfg.level));\n }\n for (const filterId of cfg.filters ?? []) {\n const filter = config.filters?.[filterId];\n if (filter === undefined) {\n throw new ConfigError(`Filter not found: ${filterId}.`);\n }\n logger.filters.push(toFilter(filter));\n }\n strongRefs.add(logger);\n }\n\n LoggerImpl.getLogger().contextLocalStorage = config.contextLocalStorage;\n\n for (const sink of Object.values<Sink>(config.sinks)) {\n if (Symbol.asyncDispose in sink) {\n if (allowAsync) asyncDisposables.add(sink as AsyncDisposable);\n else {\n throw new ConfigError(\n \"Async disposables cannot be used with configureSync().\",\n );\n }\n }\n if (Symbol.dispose in sink) disposables.add(sink as Disposable);\n }\n\n for (const filter of Object.values<FilterLike>(config.filters ?? {})) {\n if (filter == null || typeof filter === \"string\") continue;\n if (Symbol.asyncDispose in filter) {\n if (allowAsync) asyncDisposables.add(filter as AsyncDisposable);\n else {\n throw new ConfigError(\n \"Async disposables cannot be used with configureSync().\",\n );\n }\n }\n if (Symbol.dispose in filter) disposables.add(filter as Disposable);\n }\n\n if (\"process\" in globalThis && !(\"Deno\" in globalThis)) {\n // @ts-ignore: It's fine to use process in Node\n // deno-lint-ignore no-process-global\n process.on(\"exit\", allowAsync ? dispose : disposeSync);\n } else {\n // @ts-ignore: It's fine to addEventListener() on the browser/Deno\n addEventListener(\"unload\", allowAsync ? dispose : disposeSync);\n }\n const meta = LoggerImpl.getLogger([\"logtape\", \"meta\"]);\n if (!metaConfigured) {\n meta.sinks.push(getConsoleSink());\n }\n\n meta.info(\n \"LogTape loggers are configured. Note that LogTape itself uses the meta \" +\n \"logger, which has category {metaLoggerCategory}. The meta logger \" +\n \"purposes to log internal errors such as sink exceptions. If you \" +\n \"are seeing this message, the meta logger is automatically configured. \" +\n \"It's recommended to configure the meta logger with a separate sink \" +\n \"so that you can easily notice if logging itself fails or is \" +\n \"misconfigured. To turn off this message, configure the meta logger \" +\n \"with higher log levels than {dismissLevel}. See also \" +\n \"<https://logtape.org/manual/categories#meta-logger>.\",\n { metaLoggerCategory: [\"logtape\", \"meta\"], dismissLevel: \"info\" },\n );\n\n if (levelUsed) {\n meta.warn(\n \"The level option is deprecated in favor of lowestLevel option. \" +\n \"Please update your configuration. See also \" +\n \"<https://logtape.org/manual/levels#configuring-severity-levels>.\",\n );\n }\n}\n\n/**\n * Get the current configuration, if any. Otherwise, `null`.\n * @returns The current configuration, if any. Otherwise, `null`.\n */\nexport function getConfig(): Config<string, string> | null {\n return currentConfig;\n}\n\n/**\n * Reset the configuration. Mostly for testing purposes.\n */\nexport async function reset(): Promise<void> {\n await dispose();\n resetInternal();\n}\n\n/**\n * Reset the configuration. Mostly for testing purposes. Will not clear async\n * sinks, only use with sync sinks. Use {@link reset} if you have async sinks.\n * @since 0.9.0\n */\nexport function resetSync(): void {\n disposeSync();\n resetInternal();\n}\n\nfunction resetInternal(): void {\n const rootLogger = LoggerImpl.getLogger([]);\n rootLogger.resetDescendants();\n delete rootLogger.contextLocalStorage;\n strongRefs.clear();\n currentConfig = null;\n}\n\n/**\n * Dispose of the disposables.\n */\nexport async function dispose(): Promise<void> {\n disposeSync();\n const promises: PromiseLike<void>[] = [];\n for (const disposable of asyncDisposables) {\n promises.push(disposable[Symbol.asyncDispose]());\n asyncDisposables.delete(disposable);\n }\n await Promise.all(promises);\n}\n\n/**\n * Dispose of the sync disposables. Async disposables will be untouched,\n * use {@link dispose} if you have async sinks.\n * @since 0.9.0\n */\nexport function disposeSync(): void {\n for (const disposable of disposables) disposable[Symbol.dispose]();\n disposables.clear();\n}\n\n/**\n * A configuration error.\n */\nexport class ConfigError extends Error {\n /**\n * Constructs a new configuration error.\n * @param message The error message.\n */\n constructor(message: string) {\n super(message);\n this.name = \"ConfigureError\";\n }\n}\n"],"mappings":"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"}
|
package/dist/context.cjs
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const require_logger = require('./logger.cjs');
|
|
2
|
+
|
|
3
|
+
//#region context.ts
|
|
4
|
+
/**
|
|
5
|
+
* Runs a callback with the given implicit context. Every single log record
|
|
6
|
+
* in the callback will have the given context.
|
|
7
|
+
*
|
|
8
|
+
* If no `contextLocalStorage` is configured, this function does nothing and
|
|
9
|
+
* just returns the return value of the callback. It also logs a warning to
|
|
10
|
+
* the `["logtape", "meta"]` logger in this case.
|
|
11
|
+
* @param context The context to inject.
|
|
12
|
+
* @param callback The callback to run.
|
|
13
|
+
* @returns The return value of the callback.
|
|
14
|
+
* @since 0.7.0
|
|
15
|
+
*/
|
|
16
|
+
function withContext(context, callback) {
|
|
17
|
+
const rootLogger = require_logger.LoggerImpl.getLogger();
|
|
18
|
+
if (rootLogger.contextLocalStorage == null) {
|
|
19
|
+
require_logger.LoggerImpl.getLogger(["logtape", "meta"]).warn("Context-local storage is not configured. Specify contextLocalStorage option in the configure() function.");
|
|
20
|
+
return callback();
|
|
21
|
+
}
|
|
22
|
+
const parentContext = rootLogger.contextLocalStorage.getStore() ?? {};
|
|
23
|
+
return rootLogger.contextLocalStorage.run({
|
|
24
|
+
...parentContext,
|
|
25
|
+
...context
|
|
26
|
+
}, callback);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
exports.withContext = withContext;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
//#region context.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A generic interface for a context-local storage. It resembles
|
|
4
|
+
* the {@link AsyncLocalStorage} API from Node.js.
|
|
5
|
+
* @typeParam T The type of the context-local store.
|
|
6
|
+
* @since 0.7.0
|
|
7
|
+
*/
|
|
8
|
+
interface ContextLocalStorage<T> {
|
|
9
|
+
/**
|
|
10
|
+
* Runs a callback with the given store as the context-local store.
|
|
11
|
+
* @param store The store to use as the context-local store.
|
|
12
|
+
* @param callback The callback to run.
|
|
13
|
+
* @returns The return value of the callback.
|
|
14
|
+
*/
|
|
15
|
+
run<R>(store: T, callback: () => R): R;
|
|
16
|
+
/**
|
|
17
|
+
* Returns the current context-local store.
|
|
18
|
+
* @returns The current context-local store, or `undefined` if there is no
|
|
19
|
+
* store.
|
|
20
|
+
*/
|
|
21
|
+
getStore(): T | undefined;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Runs a callback with the given implicit context. Every single log record
|
|
25
|
+
* in the callback will have the given context.
|
|
26
|
+
*
|
|
27
|
+
* If no `contextLocalStorage` is configured, this function does nothing and
|
|
28
|
+
* just returns the return value of the callback. It also logs a warning to
|
|
29
|
+
* the `["logtape", "meta"]` logger in this case.
|
|
30
|
+
* @param context The context to inject.
|
|
31
|
+
* @param callback The callback to run.
|
|
32
|
+
* @returns The return value of the callback.
|
|
33
|
+
* @since 0.7.0
|
|
34
|
+
*/
|
|
35
|
+
declare function withContext<T>(context: Record<string, unknown>, callback: () => T): T;
|
|
36
|
+
//# sourceMappingURL=context.d.ts.map
|
|
37
|
+
//#endregion
|
|
38
|
+
export { ContextLocalStorage, withContext };
|
|
39
|
+
//# sourceMappingURL=context.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.cts","names":[],"sources":["../context.ts"],"sourcesContent":[],"mappings":";;AAQA;;;;;AAcc,UAdG,mBAcH,CAAA,CAAA,CAAA,CAAA;EAAC;AAef;;;;;EAGI,GAAA,CAAA,CAAA,CAAA,CAAA,KAAA,EAzBY,CAyBZ,EAAA,QAAA,EAAA,GAAA,GAzB+B,CAyB/B,CAAA,EAzBmC,CAyBnC;;;;;;cAlBU;;;;;;;;;;;;;;iBAeE,wBACL,yCACO,IACf"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
//#region context.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A generic interface for a context-local storage. It resembles
|
|
4
|
+
* the {@link AsyncLocalStorage} API from Node.js.
|
|
5
|
+
* @typeParam T The type of the context-local store.
|
|
6
|
+
* @since 0.7.0
|
|
7
|
+
*/
|
|
8
|
+
interface ContextLocalStorage<T> {
|
|
9
|
+
/**
|
|
10
|
+
* Runs a callback with the given store as the context-local store.
|
|
11
|
+
* @param store The store to use as the context-local store.
|
|
12
|
+
* @param callback The callback to run.
|
|
13
|
+
* @returns The return value of the callback.
|
|
14
|
+
*/
|
|
15
|
+
run<R>(store: T, callback: () => R): R;
|
|
16
|
+
/**
|
|
17
|
+
* Returns the current context-local store.
|
|
18
|
+
* @returns The current context-local store, or `undefined` if there is no
|
|
19
|
+
* store.
|
|
20
|
+
*/
|
|
21
|
+
getStore(): T | undefined;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Runs a callback with the given implicit context. Every single log record
|
|
25
|
+
* in the callback will have the given context.
|
|
26
|
+
*
|
|
27
|
+
* If no `contextLocalStorage` is configured, this function does nothing and
|
|
28
|
+
* just returns the return value of the callback. It also logs a warning to
|
|
29
|
+
* the `["logtape", "meta"]` logger in this case.
|
|
30
|
+
* @param context The context to inject.
|
|
31
|
+
* @param callback The callback to run.
|
|
32
|
+
* @returns The return value of the callback.
|
|
33
|
+
* @since 0.7.0
|
|
34
|
+
*/
|
|
35
|
+
declare function withContext<T>(context: Record<string, unknown>, callback: () => T): T;
|
|
36
|
+
//# sourceMappingURL=context.d.ts.map
|
|
37
|
+
//#endregion
|
|
38
|
+
export { ContextLocalStorage, withContext };
|
|
39
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","names":[],"sources":["../context.ts"],"sourcesContent":[],"mappings":";;AAQA;;;;;AAcc,UAdG,mBAcH,CAAA,CAAA,CAAA,CAAA;EAAC;AAef;;;;;EAGI,GAAA,CAAA,CAAA,CAAA,CAAA,KAAA,EAzBY,CAyBZ,EAAA,QAAA,EAAA,GAAA,GAzB+B,CAyB/B,CAAA,EAzBmC,CAyBnC;;;;;;cAlBU;;;;;;;;;;;;;;iBAeE,wBACL,yCACO,IACf"}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { LoggerImpl } from "./logger.js";
|
|
2
|
+
|
|
3
|
+
//#region context.ts
|
|
4
|
+
/**
|
|
5
|
+
* Runs a callback with the given implicit context. Every single log record
|
|
6
|
+
* in the callback will have the given context.
|
|
7
|
+
*
|
|
8
|
+
* If no `contextLocalStorage` is configured, this function does nothing and
|
|
9
|
+
* just returns the return value of the callback. It also logs a warning to
|
|
10
|
+
* the `["logtape", "meta"]` logger in this case.
|
|
11
|
+
* @param context The context to inject.
|
|
12
|
+
* @param callback The callback to run.
|
|
13
|
+
* @returns The return value of the callback.
|
|
14
|
+
* @since 0.7.0
|
|
15
|
+
*/
|
|
16
|
+
function withContext(context, callback) {
|
|
17
|
+
const rootLogger = LoggerImpl.getLogger();
|
|
18
|
+
if (rootLogger.contextLocalStorage == null) {
|
|
19
|
+
LoggerImpl.getLogger(["logtape", "meta"]).warn("Context-local storage is not configured. Specify contextLocalStorage option in the configure() function.");
|
|
20
|
+
return callback();
|
|
21
|
+
}
|
|
22
|
+
const parentContext = rootLogger.contextLocalStorage.getStore() ?? {};
|
|
23
|
+
return rootLogger.contextLocalStorage.run({
|
|
24
|
+
...parentContext,
|
|
25
|
+
...context
|
|
26
|
+
}, callback);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
export { withContext };
|
|
31
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","names":["context: Record<string, unknown>","callback: () => T"],"sources":["../context.ts"],"sourcesContent":["import { LoggerImpl } from \"./logger.ts\";\n\n/**\n * A generic interface for a context-local storage. It resembles\n * the {@link AsyncLocalStorage} API from Node.js.\n * @typeParam T The type of the context-local store.\n * @since 0.7.0\n */\nexport interface ContextLocalStorage<T> {\n /**\n * Runs a callback with the given store as the context-local store.\n * @param store The store to use as the context-local store.\n * @param callback The callback to run.\n * @returns The return value of the callback.\n */\n run<R>(store: T, callback: () => R): R;\n\n /**\n * Returns the current context-local store.\n * @returns The current context-local store, or `undefined` if there is no\n * store.\n */\n getStore(): T | undefined;\n}\n\n/**\n * Runs a callback with the given implicit context. Every single log record\n * in the callback will have the given context.\n *\n * If no `contextLocalStorage` is configured, this function does nothing and\n * just returns the return value of the callback. It also logs a warning to\n * the `[\"logtape\", \"meta\"]` logger in this case.\n * @param context The context to inject.\n * @param callback The callback to run.\n * @returns The return value of the callback.\n * @since 0.7.0\n */\nexport function withContext<T>(\n context: Record<string, unknown>,\n callback: () => T,\n): T {\n const rootLogger = LoggerImpl.getLogger();\n if (rootLogger.contextLocalStorage == null) {\n LoggerImpl.getLogger([\"logtape\", \"meta\"]).warn(\n \"Context-local storage is not configured. \" +\n \"Specify contextLocalStorage option in the configure() function.\",\n );\n return callback();\n }\n const parentContext = rootLogger.contextLocalStorage.getStore() ?? {};\n return rootLogger.contextLocalStorage.run(\n { ...parentContext, ...context },\n callback,\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;AAqCA,SAAgB,YACdA,SACAC,UACG;CACH,MAAM,aAAa,WAAW,WAAW;AACzC,KAAI,WAAW,uBAAuB,MAAM;AAC1C,aAAW,UAAU,CAAC,WAAW,MAAO,EAAC,CAAC,KACxC,4GAED;AACD,SAAO,UAAU;CAClB;CACD,MAAM,gBAAgB,WAAW,oBAAoB,UAAU,IAAI,CAAE;AACrE,QAAO,WAAW,oBAAoB,IACpC;EAAE,GAAG;EAAe,GAAG;CAAS,GAChC,SACD;AACF"}
|
package/dist/filter.cjs
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
|
|
2
|
+
//#region filter.ts
|
|
3
|
+
/**
|
|
4
|
+
* Converts a {@link FilterLike} value to an actual {@link Filter}.
|
|
5
|
+
*
|
|
6
|
+
* @param filter The filter-like value to convert.
|
|
7
|
+
* @returns The actual filter.
|
|
8
|
+
*/
|
|
9
|
+
function toFilter(filter) {
|
|
10
|
+
if (typeof filter === "function") return filter;
|
|
11
|
+
return getLevelFilter(filter);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Returns a filter that accepts log records with the specified level.
|
|
15
|
+
*
|
|
16
|
+
* @param level The level to filter by. If `null`, the filter will reject all
|
|
17
|
+
* records.
|
|
18
|
+
* @returns The filter.
|
|
19
|
+
*/
|
|
20
|
+
function getLevelFilter(level) {
|
|
21
|
+
if (level == null) return () => false;
|
|
22
|
+
if (level === "fatal") return (record) => record.level === "fatal";
|
|
23
|
+
else if (level === "error") return (record) => record.level === "fatal" || record.level === "error";
|
|
24
|
+
else if (level === "warning") return (record) => record.level === "fatal" || record.level === "error" || record.level === "warning";
|
|
25
|
+
else if (level === "info") return (record) => record.level === "fatal" || record.level === "error" || record.level === "warning" || record.level === "info";
|
|
26
|
+
else if (level === "debug") return () => true;
|
|
27
|
+
throw new TypeError(`Invalid log level: ${level}.`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
31
|
+
exports.getLevelFilter = getLevelFilter;
|
|
32
|
+
exports.toFilter = toFilter;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { LogLevel } from "./level.cjs";
|
|
2
|
+
import { LogRecord } from "./record.cjs";
|
|
3
|
+
|
|
4
|
+
//#region filter.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A filter is a function that accepts a log record and returns `true` if the
|
|
8
|
+
* record should be passed to the sink.
|
|
9
|
+
*
|
|
10
|
+
* @param record The log record to filter.
|
|
11
|
+
* @returns `true` if the record should be passed to the sink.
|
|
12
|
+
*/
|
|
13
|
+
type Filter = (record: LogRecord) => boolean;
|
|
14
|
+
/**
|
|
15
|
+
* A filter-like value is either a {@link Filter} or a {@link LogLevel}.
|
|
16
|
+
* `null` is also allowed to represent a filter that rejects all records.
|
|
17
|
+
*/
|
|
18
|
+
type FilterLike = Filter | LogLevel | null;
|
|
19
|
+
/**
|
|
20
|
+
* Converts a {@link FilterLike} value to an actual {@link Filter}.
|
|
21
|
+
*
|
|
22
|
+
* @param filter The filter-like value to convert.
|
|
23
|
+
* @returns The actual filter.
|
|
24
|
+
*/
|
|
25
|
+
declare function toFilter(filter: FilterLike): Filter;
|
|
26
|
+
/**
|
|
27
|
+
* Returns a filter that accepts log records with the specified level.
|
|
28
|
+
*
|
|
29
|
+
* @param level The level to filter by. If `null`, the filter will reject all
|
|
30
|
+
* records.
|
|
31
|
+
* @returns The filter.
|
|
32
|
+
*/
|
|
33
|
+
declare function getLevelFilter(level: LogLevel | null): Filter;
|
|
34
|
+
//# sourceMappingURL=filter.d.ts.map
|
|
35
|
+
//#endregion
|
|
36
|
+
export { Filter, FilterLike, getLevelFilter, toFilter };
|
|
37
|
+
//# sourceMappingURL=filter.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filter.d.cts","names":[],"sources":["../filter.ts"],"sourcesContent":[],"mappings":";;;;;;;AAUA;AAMA;;;;AAA0C,KAN9B,MAAA,GAM8B,CAAA,MAAA,EANZ,SAMY,EAAA,GAAA,OAAA;AAQ1C;;;;AAAoD,KARxC,UAAA,GAAa,MAQ2B,GARlB,QAQkB,GAAA,IAAA;AAYpD;;;;AAA8D;;iBAZ9C,QAAA,SAAiB,aAAa;;;;;;;;iBAY9B,cAAA,QAAsB,kBAAkB"}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { LogLevel } from "./level.js";
|
|
2
|
+
import { LogRecord } from "./record.js";
|
|
3
|
+
|
|
4
|
+
//#region filter.d.ts
|
|
5
|
+
|
|
3
6
|
/**
|
|
4
7
|
* A filter is a function that accepts a log record and returns `true` if the
|
|
5
8
|
* record should be passed to the sink.
|
|
@@ -7,19 +10,19 @@ import type { LogRecord } from "./record.js";
|
|
|
7
10
|
* @param record The log record to filter.
|
|
8
11
|
* @returns `true` if the record should be passed to the sink.
|
|
9
12
|
*/
|
|
10
|
-
|
|
13
|
+
type Filter = (record: LogRecord) => boolean;
|
|
11
14
|
/**
|
|
12
15
|
* A filter-like value is either a {@link Filter} or a {@link LogLevel}.
|
|
13
16
|
* `null` is also allowed to represent a filter that rejects all records.
|
|
14
17
|
*/
|
|
15
|
-
|
|
18
|
+
type FilterLike = Filter | LogLevel | null;
|
|
16
19
|
/**
|
|
17
20
|
* Converts a {@link FilterLike} value to an actual {@link Filter}.
|
|
18
21
|
*
|
|
19
22
|
* @param filter The filter-like value to convert.
|
|
20
23
|
* @returns The actual filter.
|
|
21
24
|
*/
|
|
22
|
-
|
|
25
|
+
declare function toFilter(filter: FilterLike): Filter;
|
|
23
26
|
/**
|
|
24
27
|
* Returns a filter that accepts log records with the specified level.
|
|
25
28
|
*
|
|
@@ -27,5 +30,8 @@ export declare function toFilter(filter: FilterLike): Filter;
|
|
|
27
30
|
* records.
|
|
28
31
|
* @returns The filter.
|
|
29
32
|
*/
|
|
30
|
-
|
|
33
|
+
declare function getLevelFilter(level: LogLevel | null): Filter;
|
|
34
|
+
//# sourceMappingURL=filter.d.ts.map
|
|
35
|
+
//#endregion
|
|
36
|
+
export { Filter, FilterLike, getLevelFilter, toFilter };
|
|
31
37
|
//# sourceMappingURL=filter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filter.d.ts","names":[],"sources":["../filter.ts"],"sourcesContent":[],"mappings":";;;;;;;AAUA;AAMA;;;;AAA0C,KAN9B,MAAA,GAM8B,CAAA,MAAA,EANZ,SAMY,EAAA,GAAA,OAAA;AAQ1C;;;;AAAoD,KARxC,UAAA,GAAa,MAQ2B,GARlB,QAQkB,GAAA,IAAA;AAYpD;;;;AAA8D;;iBAZ9C,QAAA,SAAiB,aAAa;;;;;;;;iBAY9B,cAAA,QAAsB,kBAAkB"}
|
package/dist/filter.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
//#region filter.ts
|
|
2
|
+
/**
|
|
3
|
+
* Converts a {@link FilterLike} value to an actual {@link Filter}.
|
|
4
|
+
*
|
|
5
|
+
* @param filter The filter-like value to convert.
|
|
6
|
+
* @returns The actual filter.
|
|
7
|
+
*/
|
|
8
|
+
function toFilter(filter) {
|
|
9
|
+
if (typeof filter === "function") return filter;
|
|
10
|
+
return getLevelFilter(filter);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Returns a filter that accepts log records with the specified level.
|
|
14
|
+
*
|
|
15
|
+
* @param level The level to filter by. If `null`, the filter will reject all
|
|
16
|
+
* records.
|
|
17
|
+
* @returns The filter.
|
|
18
|
+
*/
|
|
19
|
+
function getLevelFilter(level) {
|
|
20
|
+
if (level == null) return () => false;
|
|
21
|
+
if (level === "fatal") return (record) => record.level === "fatal";
|
|
22
|
+
else if (level === "error") return (record) => record.level === "fatal" || record.level === "error";
|
|
23
|
+
else if (level === "warning") return (record) => record.level === "fatal" || record.level === "error" || record.level === "warning";
|
|
24
|
+
else if (level === "info") return (record) => record.level === "fatal" || record.level === "error" || record.level === "warning" || record.level === "info";
|
|
25
|
+
else if (level === "debug") return () => true;
|
|
26
|
+
throw new TypeError(`Invalid log level: ${level}.`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
export { getLevelFilter, toFilter };
|
|
31
|
+
//# sourceMappingURL=filter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filter.js","names":["filter: FilterLike","level: LogLevel | null","record: LogRecord"],"sources":["../filter.ts"],"sourcesContent":["import type { LogLevel } from \"./level.ts\";\nimport type { LogRecord } from \"./record.ts\";\n\n/**\n * A filter is a function that accepts a log record and returns `true` if the\n * record should be passed to the sink.\n *\n * @param record The log record to filter.\n * @returns `true` if the record should be passed to the sink.\n */\nexport type Filter = (record: LogRecord) => boolean;\n\n/**\n * A filter-like value is either a {@link Filter} or a {@link LogLevel}.\n * `null` is also allowed to represent a filter that rejects all records.\n */\nexport type FilterLike = Filter | LogLevel | null;\n\n/**\n * Converts a {@link FilterLike} value to an actual {@link Filter}.\n *\n * @param filter The filter-like value to convert.\n * @returns The actual filter.\n */\nexport function toFilter(filter: FilterLike): Filter {\n if (typeof filter === \"function\") return filter;\n return getLevelFilter(filter);\n}\n\n/**\n * Returns a filter that accepts log records with the specified level.\n *\n * @param level The level to filter by. If `null`, the filter will reject all\n * records.\n * @returns The filter.\n */\nexport function getLevelFilter(level: LogLevel | null): Filter {\n if (level == null) return () => false;\n if (level === \"fatal\") {\n return (record: LogRecord) => record.level === \"fatal\";\n } else if (level === \"error\") {\n return (record: LogRecord) =>\n record.level === \"fatal\" || record.level === \"error\";\n } else if (level === \"warning\") {\n return (record: LogRecord) =>\n record.level === \"fatal\" ||\n record.level === \"error\" ||\n record.level === \"warning\";\n } else if (level === \"info\") {\n return (record: LogRecord) =>\n record.level === \"fatal\" ||\n record.level === \"error\" ||\n record.level === \"warning\" ||\n record.level === \"info\";\n } else if (level === \"debug\") return () => true;\n throw new TypeError(`Invalid log level: ${level}.`);\n}\n"],"mappings":";;;;;;;AAwBA,SAAgB,SAASA,QAA4B;AACnD,YAAW,WAAW,WAAY,QAAO;AACzC,QAAO,eAAe,OAAO;AAC9B;;;;;;;;AASD,SAAgB,eAAeC,OAAgC;AAC7D,KAAI,SAAS,KAAM,QAAO,MAAM;AAChC,KAAI,UAAU,QACZ,QAAO,CAACC,WAAsB,OAAO,UAAU;UACtC,UAAU,QACnB,QAAO,CAACA,WACN,OAAO,UAAU,WAAW,OAAO,UAAU;UACtC,UAAU,UACnB,QAAO,CAACA,WACN,OAAO,UAAU,WACjB,OAAO,UAAU,WACjB,OAAO,UAAU;UACV,UAAU,OACnB,QAAO,CAACA,WACN,OAAO,UAAU,WACjB,OAAO,UAAU,WACjB,OAAO,UAAU,aACjB,OAAO,UAAU;UACV,UAAU,QAAS,QAAO,MAAM;AAC3C,OAAM,IAAI,WAAW,qBAAqB,MAAM;AACjD"}
|