where-log 0.2.0 → 0.2.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.
- package/README.md +74 -0
- package/dist/index.cjs +485 -12
- package/dist/index.d.cts +98 -1
- package/dist/index.d.ts +98 -1
- package/dist/index.mjs +470 -11
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -45,6 +45,47 @@ log("user", user, {
|
|
|
45
45
|
log(user, { mode: "fast", includeLocation: false });
|
|
46
46
|
```
|
|
47
47
|
|
|
48
|
+
Preset APIs:
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { createLogger, logDev, logProd } from "where-log";
|
|
52
|
+
|
|
53
|
+
logDev("user", user); // pretty + location
|
|
54
|
+
logProd("user", user); // fast + no location
|
|
55
|
+
|
|
56
|
+
const appLog = createLogger({ redact: ["user.token"], includeLocation: false });
|
|
57
|
+
appLog("request", { user: { token: "secret" } });
|
|
58
|
+
appLog.info("ready", { status: "ok" });
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Level APIs:
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { debug, error, info, success, warn } from "where-log";
|
|
65
|
+
|
|
66
|
+
info("user", user);
|
|
67
|
+
success("saved", { id: 1 });
|
|
68
|
+
warn("quota", { remaining: 2 });
|
|
69
|
+
error("request", { status: 500 });
|
|
70
|
+
debug("trace", { step: "auth" });
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Once / Timer / Context APIs:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { once, time, timeEnd, withContext } from "where-log";
|
|
77
|
+
|
|
78
|
+
once("boot:db", "db connected");
|
|
79
|
+
once("boot:db", "db connected again"); // ignored
|
|
80
|
+
|
|
81
|
+
time("fetch-users");
|
|
82
|
+
// ... work
|
|
83
|
+
timeEnd("fetch-users", { total: 42 }, { warnThresholdMs: 200, errorThresholdMs: 800 });
|
|
84
|
+
|
|
85
|
+
const reqLog = withContext({ requestId: "req_123", userId: 7 }, { includeLocation: false });
|
|
86
|
+
reqLog.info("request", { path: "/api/users" });
|
|
87
|
+
```
|
|
88
|
+
|
|
48
89
|
## Notes
|
|
49
90
|
|
|
50
91
|
- Node.js: call-site detection is reliable with V8 stack traces.
|
|
@@ -57,16 +98,49 @@ log(user, { mode: "fast", includeLocation: false });
|
|
|
57
98
|
- `mode: "fast"`: lower overhead formatting for hot paths.
|
|
58
99
|
- `includeLocation: false`: skips stack capture/parsing.
|
|
59
100
|
- `inspectDepth`: limit inspect depth in pretty mode.
|
|
101
|
+
- `enabled: false`: disables log output quickly.
|
|
102
|
+
|
|
103
|
+
## Safety / Readability
|
|
104
|
+
|
|
105
|
+
- `redact`: mask sensitive values by path, e.g. `["password", "user.token"]`.
|
|
106
|
+
- `maxArrayLength`: trim large arrays and append a summary item.
|
|
107
|
+
- level helpers prepend compact tags by default (`[INFO]`, `[ERROR]`, etc.).
|
|
108
|
+
|
|
109
|
+
## Session Helpers
|
|
110
|
+
|
|
111
|
+
- `once(key, ...)`: log only first occurrence per runtime session.
|
|
112
|
+
- `time(key)` + `timeEnd(key, ...)`: duration logging with optional thresholds.
|
|
113
|
+
- `resetOnce(keys?)` and `resetTimers(keys?)`: clear in-memory runtime state.
|
|
114
|
+
- `withContext(context)`: create logger with injected structured context.
|
|
60
115
|
|
|
61
116
|
## API
|
|
62
117
|
|
|
63
118
|
- `log(value: unknown): void`
|
|
64
119
|
- `log(value: unknown, options?: LogOptions): void`
|
|
65
120
|
- `log(label: string, value: unknown, options?: LogOptions): void`
|
|
121
|
+
- `info(...)`, `success(...)`, `warn(...)`, `error(...)`, `debug(...)`
|
|
122
|
+
- `once(...)`, `time(...)`, `timeEnd(...)`
|
|
123
|
+
- `logDev(...)`: dev preset (`pretty`, `includeLocation: true`, `colors: true`)
|
|
124
|
+
- `logProd(...)`: prod preset (`fast`, `includeLocation: false`, `colors: false`)
|
|
125
|
+
- `createLogger(presetOptions?: LogOptions): LoggerFn`
|
|
126
|
+
- `withContext(context, presetOptions?): LoggerFn`
|
|
127
|
+
- `resetOnce(keys?)`, `resetTimers(keys?)`
|
|
66
128
|
- `LogMode = "pretty" | "fast"`
|
|
129
|
+
- `LogLevel = "info" | "success" | "warn" | "error" | "debug"`
|
|
67
130
|
- `LogOptions`
|
|
131
|
+
- `enabled?: boolean`
|
|
68
132
|
- `mode?: LogMode` (default `"pretty"`)
|
|
69
133
|
- `includeLocation?: boolean` (default `true`)
|
|
70
134
|
- `inspectDepth?: number` (pretty mode only)
|
|
135
|
+
- `maxArrayLength?: number`
|
|
136
|
+
- `redact?: string[]`
|
|
137
|
+
- `level?: LogLevel`
|
|
138
|
+
- `showLevelTag?: boolean`
|
|
139
|
+
- `consoleMethod?: "log" | "info" | "warn" | "error" | "debug"`
|
|
140
|
+
- `context?: Record<string, unknown>`
|
|
141
|
+
- `clockNow?: () => number`
|
|
142
|
+
- `warnThresholdMs?: number`
|
|
143
|
+
- `errorThresholdMs?: number`
|
|
144
|
+
- `includeDurationOnly?: boolean`
|
|
71
145
|
- `colors?: boolean` (Node only, default `true`)
|
|
72
146
|
- `formatter?: (input) => { locationLine: unknown; valueLine: unknown }`
|
package/dist/index.cjs
CHANGED
|
@@ -31,10 +31,53 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
__internal: () => __internal,
|
|
34
|
-
|
|
34
|
+
createLogger: () => createLogger,
|
|
35
|
+
debug: () => debug,
|
|
36
|
+
error: () => error,
|
|
37
|
+
info: () => info,
|
|
38
|
+
log: () => log,
|
|
39
|
+
logDev: () => logDev,
|
|
40
|
+
logProd: () => logProd,
|
|
41
|
+
once: () => once,
|
|
42
|
+
resetOnce: () => resetOnce,
|
|
43
|
+
resetTimers: () => resetTimers,
|
|
44
|
+
success: () => success,
|
|
45
|
+
time: () => time,
|
|
46
|
+
timeEnd: () => timeEnd,
|
|
47
|
+
warn: () => warn,
|
|
48
|
+
withContext: () => withContext
|
|
35
49
|
});
|
|
36
50
|
module.exports = __toCommonJS(index_exports);
|
|
37
51
|
|
|
52
|
+
// src/core/context.ts
|
|
53
|
+
function isRecord(value) {
|
|
54
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
55
|
+
}
|
|
56
|
+
function mergeContexts(base, incoming) {
|
|
57
|
+
if (!base && !incoming) return void 0;
|
|
58
|
+
return {
|
|
59
|
+
...base ?? {},
|
|
60
|
+
...incoming ?? {}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function injectContext(value, context) {
|
|
64
|
+
if (!context) return value;
|
|
65
|
+
if (isRecord(value)) {
|
|
66
|
+
const existing = isRecord(value.context) ? value.context : void 0;
|
|
67
|
+
return {
|
|
68
|
+
...value,
|
|
69
|
+
context: {
|
|
70
|
+
...existing ?? {},
|
|
71
|
+
...context
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
value,
|
|
77
|
+
context
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
38
81
|
// src/core/format.ts
|
|
39
82
|
var import_node_util = __toESM(require("util"), 1);
|
|
40
83
|
function isNodeRuntime() {
|
|
@@ -89,6 +132,62 @@ function formatLabeledValue(label, formattedValue) {
|
|
|
89
132
|
return `${label}: ${String(formattedValue)}`;
|
|
90
133
|
}
|
|
91
134
|
|
|
135
|
+
// src/core/level.ts
|
|
136
|
+
var LEVEL_METHOD_MAP = {
|
|
137
|
+
info: "info",
|
|
138
|
+
success: "log",
|
|
139
|
+
warn: "warn",
|
|
140
|
+
error: "error",
|
|
141
|
+
debug: "debug"
|
|
142
|
+
};
|
|
143
|
+
function levelToConsoleMethod(level) {
|
|
144
|
+
return LEVEL_METHOD_MAP[level];
|
|
145
|
+
}
|
|
146
|
+
function levelToTag(level) {
|
|
147
|
+
return `[${level.toUpperCase()}]`;
|
|
148
|
+
}
|
|
149
|
+
function resolveConsoleMethod(level, override) {
|
|
150
|
+
if (override) return override;
|
|
151
|
+
if (!level) return "log";
|
|
152
|
+
return levelToConsoleMethod(level);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// src/core/session.ts
|
|
156
|
+
var onceKeys = /* @__PURE__ */ new Set();
|
|
157
|
+
var timers = /* @__PURE__ */ new Map();
|
|
158
|
+
function checkAndMarkOnce(key) {
|
|
159
|
+
if (onceKeys.has(key)) return false;
|
|
160
|
+
onceKeys.add(key);
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
function clearOnce(keys) {
|
|
164
|
+
if (!keys || keys.length === 0) {
|
|
165
|
+
onceKeys.clear();
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
for (const key of keys) {
|
|
169
|
+
onceKeys.delete(key);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function startTimer(key, now) {
|
|
173
|
+
timers.set(key, { startedAt: now });
|
|
174
|
+
}
|
|
175
|
+
function endTimer(key, now) {
|
|
176
|
+
const entry = timers.get(key);
|
|
177
|
+
if (!entry) return null;
|
|
178
|
+
timers.delete(key);
|
|
179
|
+
return Math.max(0, now - entry.startedAt);
|
|
180
|
+
}
|
|
181
|
+
function clearTimers(keys) {
|
|
182
|
+
if (!keys || keys.length === 0) {
|
|
183
|
+
timers.clear();
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
for (const key of keys) {
|
|
187
|
+
timers.delete(key);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
92
191
|
// src/core/stack.ts
|
|
93
192
|
function safeToInt(input) {
|
|
94
193
|
const n = Number.parseInt(input, 10);
|
|
@@ -138,13 +237,88 @@ function getCallerFromStack(stack) {
|
|
|
138
237
|
return { file: "unknown", line: 0 };
|
|
139
238
|
}
|
|
140
239
|
|
|
240
|
+
// src/core/transform.ts
|
|
241
|
+
function isObjectLike(value) {
|
|
242
|
+
return typeof value === "object" && value !== null;
|
|
243
|
+
}
|
|
244
|
+
function cloneDeep(value, seen = /* @__PURE__ */ new WeakMap()) {
|
|
245
|
+
if (!isObjectLike(value)) return value;
|
|
246
|
+
if (seen.has(value)) return seen.get(value);
|
|
247
|
+
if (Array.isArray(value)) {
|
|
248
|
+
const arr = [];
|
|
249
|
+
seen.set(value, arr);
|
|
250
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
251
|
+
arr.push(cloneDeep(value[i], seen));
|
|
252
|
+
}
|
|
253
|
+
return arr;
|
|
254
|
+
}
|
|
255
|
+
const obj = {};
|
|
256
|
+
seen.set(value, obj);
|
|
257
|
+
for (const [k, v] of Object.entries(value)) {
|
|
258
|
+
obj[k] = cloneDeep(v, seen);
|
|
259
|
+
}
|
|
260
|
+
return obj;
|
|
261
|
+
}
|
|
262
|
+
function redactPath(target, path) {
|
|
263
|
+
if (!isObjectLike(target) || path.length === 0) return;
|
|
264
|
+
const [head, ...rest] = path;
|
|
265
|
+
if (Array.isArray(target)) {
|
|
266
|
+
const idx = Number.parseInt(head, 10);
|
|
267
|
+
if (!Number.isFinite(idx) || idx < 0 || idx >= target.length) return;
|
|
268
|
+
if (rest.length === 0) {
|
|
269
|
+
target[idx] = "[REDACTED]";
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
redactPath(target[idx], rest);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
if (!(head in target)) return;
|
|
276
|
+
if (rest.length === 0) {
|
|
277
|
+
target[head] = "[REDACTED]";
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
redactPath(target[head], rest);
|
|
281
|
+
}
|
|
282
|
+
function truncateArrays(target, maxArrayLength) {
|
|
283
|
+
if (!isObjectLike(target)) return target;
|
|
284
|
+
if (Array.isArray(target)) {
|
|
285
|
+
const sliced = target.slice(0, maxArrayLength).map((item) => truncateArrays(item, maxArrayLength));
|
|
286
|
+
if (target.length > maxArrayLength) {
|
|
287
|
+
sliced.push(`[... ${target.length - maxArrayLength} more items]`);
|
|
288
|
+
}
|
|
289
|
+
return sliced;
|
|
290
|
+
}
|
|
291
|
+
const out = {};
|
|
292
|
+
for (const [k, v] of Object.entries(target)) {
|
|
293
|
+
out[k] = truncateArrays(v, maxArrayLength);
|
|
294
|
+
}
|
|
295
|
+
return out;
|
|
296
|
+
}
|
|
297
|
+
function transformValue(value, options) {
|
|
298
|
+
if (!options?.redact?.length && options?.maxArrayLength == null) {
|
|
299
|
+
return value;
|
|
300
|
+
}
|
|
301
|
+
const cloned = cloneDeep(value);
|
|
302
|
+
if (options.redact?.length) {
|
|
303
|
+
for (const rawPath of options.redact) {
|
|
304
|
+
const path = rawPath.split(".").map((part) => part.trim()).filter(Boolean);
|
|
305
|
+
if (path.length === 0) continue;
|
|
306
|
+
redactPath(cloned, path);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
if (typeof options.maxArrayLength === "number" && options.maxArrayLength >= 0) {
|
|
310
|
+
return truncateArrays(cloned, options.maxArrayLength);
|
|
311
|
+
}
|
|
312
|
+
return cloned;
|
|
313
|
+
}
|
|
314
|
+
|
|
141
315
|
// src/index.ts
|
|
142
316
|
function isLogOptions(value) {
|
|
143
317
|
if (!value || typeof value !== "object") return false;
|
|
144
318
|
const record = value;
|
|
145
|
-
return "colors" in record || "formatter" in record || "mode" in record || "includeLocation" in record || "inspectDepth" in record;
|
|
319
|
+
return "colors" in record || "formatter" in record || "enabled" in record || "mode" in record || "includeLocation" in record || "inspectDepth" in record || "maxArrayLength" in record || "redact" in record || "level" in record || "showLevelTag" in record || "levelTagStyle" in record || "consoleMethod" in record || "context" in record || "clockNow" in record || "warnThresholdMs" in record || "errorThresholdMs" in record || "includeDurationOnly" in record;
|
|
146
320
|
}
|
|
147
|
-
function
|
|
321
|
+
function resolveLogArgs(arg1, arg2, arg3) {
|
|
148
322
|
let label;
|
|
149
323
|
let value;
|
|
150
324
|
let options;
|
|
@@ -156,40 +330,339 @@ function log(arg1, arg2, arg3) {
|
|
|
156
330
|
value = arg1;
|
|
157
331
|
options = isLogOptions(arg2) ? arg2 : void 0;
|
|
158
332
|
}
|
|
333
|
+
return { label, value, options };
|
|
334
|
+
}
|
|
335
|
+
function resolveOnceArgs(key, arg2, arg3, arg4) {
|
|
336
|
+
if (typeof arg2 === "string" && arg3 !== void 0 && !isLogOptions(arg3)) {
|
|
337
|
+
return {
|
|
338
|
+
key,
|
|
339
|
+
label: arg2,
|
|
340
|
+
value: arg3,
|
|
341
|
+
options: arg4
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
return {
|
|
345
|
+
key,
|
|
346
|
+
value: arg2,
|
|
347
|
+
options: isLogOptions(arg3) ? arg3 : void 0
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
function resolveTimeEndArgs(key, arg2, arg3, arg4) {
|
|
351
|
+
if (arg2 === void 0) {
|
|
352
|
+
return { key };
|
|
353
|
+
}
|
|
354
|
+
if (isLogOptions(arg2)) {
|
|
355
|
+
return { key, options: arg2 };
|
|
356
|
+
}
|
|
357
|
+
if (typeof arg2 === "string" && arg3 !== void 0 && !isLogOptions(arg3)) {
|
|
358
|
+
return {
|
|
359
|
+
key,
|
|
360
|
+
label: arg2,
|
|
361
|
+
value: arg3,
|
|
362
|
+
options: arg4
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
return {
|
|
366
|
+
key,
|
|
367
|
+
value: arg2,
|
|
368
|
+
options: isLogOptions(arg3) ? arg3 : void 0
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
function writeLine(method, line) {
|
|
372
|
+
const sink = console[method];
|
|
373
|
+
if (typeof sink === "function") {
|
|
374
|
+
sink(line);
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
console.log(line);
|
|
378
|
+
}
|
|
379
|
+
function runLog(args, ctx) {
|
|
380
|
+
const { label, value, options } = args;
|
|
381
|
+
if (options?.enabled === false) {
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
const level = options?.level ?? ctx?.forceLevel;
|
|
385
|
+
const consoleMethod = resolveConsoleMethod(level, options?.consoleMethod);
|
|
386
|
+
const showLevelTag = options?.showLevelTag ?? (ctx?.defaultShowLevelTag ?? false);
|
|
159
387
|
const includeLocation = options?.includeLocation ?? true;
|
|
160
388
|
const stack = includeLocation ? new Error().stack : void 0;
|
|
161
389
|
const caller = includeLocation ? getCallerFromStack(stack) : { file: "disabled", line: 0 };
|
|
162
390
|
const location = `${caller.file}:${caller.line}`;
|
|
391
|
+
const mergedContext = mergeContexts(ctx?.defaultContext, options?.context);
|
|
392
|
+
const contextualValue = injectContext(value, mergedContext);
|
|
393
|
+
const transformedValue = transformValue(contextualValue, {
|
|
394
|
+
redact: options?.redact,
|
|
395
|
+
maxArrayLength: options?.maxArrayLength
|
|
396
|
+
});
|
|
163
397
|
const formatOptions = {
|
|
164
398
|
colors: options?.colors,
|
|
165
399
|
mode: options?.mode,
|
|
166
400
|
inspectDepth: options?.inspectDepth
|
|
167
401
|
};
|
|
168
|
-
const formattedValue = formatValue(
|
|
402
|
+
const formattedValue = formatValue(transformedValue, formatOptions);
|
|
403
|
+
const levelTag = level ? levelToTag(level) : void 0;
|
|
169
404
|
if (options?.formatter) {
|
|
170
405
|
const formatted = options.formatter({
|
|
171
406
|
location,
|
|
172
407
|
label,
|
|
173
|
-
value,
|
|
174
|
-
formattedValue
|
|
408
|
+
value: transformedValue,
|
|
409
|
+
formattedValue,
|
|
410
|
+
level,
|
|
411
|
+
levelTag
|
|
175
412
|
});
|
|
176
|
-
|
|
177
|
-
|
|
413
|
+
writeLine(consoleMethod, formatted.locationLine);
|
|
414
|
+
writeLine(consoleMethod, formatted.valueLine);
|
|
178
415
|
return;
|
|
179
416
|
}
|
|
180
417
|
if (includeLocation) {
|
|
181
|
-
|
|
418
|
+
writeLine(consoleMethod, location);
|
|
182
419
|
}
|
|
183
|
-
|
|
420
|
+
const labeledValue = formatLabeledValue(label, formattedValue);
|
|
421
|
+
const valueLine = showLevelTag && levelTag ? `${levelTag} ${String(labeledValue)}` : labeledValue;
|
|
422
|
+
writeLine(consoleMethod, valueLine);
|
|
423
|
+
}
|
|
424
|
+
var DEV_PRESET = {
|
|
425
|
+
mode: "pretty",
|
|
426
|
+
includeLocation: true,
|
|
427
|
+
colors: true
|
|
428
|
+
};
|
|
429
|
+
var PROD_PRESET = {
|
|
430
|
+
mode: "fast",
|
|
431
|
+
includeLocation: false,
|
|
432
|
+
colors: false
|
|
433
|
+
};
|
|
434
|
+
function mergeOptions(base, incoming) {
|
|
435
|
+
if (!base && !incoming) return void 0;
|
|
436
|
+
const merged = {
|
|
437
|
+
...base ?? {},
|
|
438
|
+
...incoming ?? {}
|
|
439
|
+
};
|
|
440
|
+
merged.context = mergeContexts(base?.context, incoming?.context);
|
|
441
|
+
return merged;
|
|
442
|
+
}
|
|
443
|
+
function resolveTimingLevel(durationMs, options) {
|
|
444
|
+
const errorThreshold = options?.errorThresholdMs;
|
|
445
|
+
const warnThreshold = options?.warnThresholdMs;
|
|
446
|
+
if (typeof errorThreshold === "number" && durationMs >= errorThreshold) return "error";
|
|
447
|
+
if (typeof warnThreshold === "number" && durationMs >= warnThreshold) return "warn";
|
|
448
|
+
return "info";
|
|
449
|
+
}
|
|
450
|
+
function log(arg1, arg2, arg3) {
|
|
451
|
+
runLog(resolveLogArgs(arg1, arg2, arg3));
|
|
452
|
+
}
|
|
453
|
+
function once(key, arg2, arg3, arg4) {
|
|
454
|
+
const resolved = resolveOnceArgs(key, arg2, arg3, arg4);
|
|
455
|
+
if (resolved.options?.enabled === false) return;
|
|
456
|
+
if (!checkAndMarkOnce(key)) return;
|
|
457
|
+
runLog({ label: resolved.label, value: resolved.value, options: resolved.options });
|
|
458
|
+
}
|
|
459
|
+
function resetOnce(keys) {
|
|
460
|
+
clearOnce(keys);
|
|
461
|
+
}
|
|
462
|
+
function time(key, options) {
|
|
463
|
+
if (options?.enabled === false) return;
|
|
464
|
+
const now = options?.clockNow?.() ?? Date.now();
|
|
465
|
+
startTimer(key, now);
|
|
466
|
+
}
|
|
467
|
+
function timeEnd(key, arg2, arg3, arg4) {
|
|
468
|
+
const resolved = resolveTimeEndArgs(key, arg2, arg3, arg4);
|
|
469
|
+
if (resolved.options?.enabled === false) return;
|
|
470
|
+
const now = resolved.options?.clockNow?.() ?? Date.now();
|
|
471
|
+
const durationMs = endTimer(key, now);
|
|
472
|
+
if (durationMs == null) {
|
|
473
|
+
runLog(
|
|
474
|
+
{
|
|
475
|
+
label: resolved.label ?? "timer",
|
|
476
|
+
value: {
|
|
477
|
+
key,
|
|
478
|
+
error: "timer_not_started"
|
|
479
|
+
},
|
|
480
|
+
options: resolved.options
|
|
481
|
+
},
|
|
482
|
+
{
|
|
483
|
+
forceLevel: "warn",
|
|
484
|
+
defaultShowLevelTag: true
|
|
485
|
+
}
|
|
486
|
+
);
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
const timedPayload = resolved.options?.includeDurationOnly ? { durationMs } : {
|
|
490
|
+
key,
|
|
491
|
+
durationMs,
|
|
492
|
+
...resolved.value !== void 0 ? { value: resolved.value } : {}
|
|
493
|
+
};
|
|
494
|
+
const timingLevel = resolveTimingLevel(durationMs, resolved.options);
|
|
495
|
+
runLog(
|
|
496
|
+
{
|
|
497
|
+
label: resolved.label ?? "timer",
|
|
498
|
+
value: timedPayload,
|
|
499
|
+
options: resolved.options
|
|
500
|
+
},
|
|
501
|
+
{
|
|
502
|
+
forceLevel: timingLevel,
|
|
503
|
+
defaultShowLevelTag: true
|
|
504
|
+
}
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
function resetTimers(keys) {
|
|
508
|
+
clearTimers(keys);
|
|
509
|
+
}
|
|
510
|
+
function logDev(arg1, arg2, arg3) {
|
|
511
|
+
const resolved = resolveLogArgs(arg1, arg2, arg3);
|
|
512
|
+
runLog({
|
|
513
|
+
...resolved,
|
|
514
|
+
options: mergeOptions(DEV_PRESET, resolved.options)
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
function logProd(arg1, arg2, arg3) {
|
|
518
|
+
const resolved = resolveLogArgs(arg1, arg2, arg3);
|
|
519
|
+
runLog({
|
|
520
|
+
...resolved,
|
|
521
|
+
options: mergeOptions(PROD_PRESET, resolved.options)
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
function makeLevelMethod(level, presetOptions, defaultContext) {
|
|
525
|
+
const method = (arg1, arg2, arg3) => {
|
|
526
|
+
const resolved = resolveLogArgs(arg1, arg2, arg3);
|
|
527
|
+
runLog(
|
|
528
|
+
{
|
|
529
|
+
...resolved,
|
|
530
|
+
options: mergeOptions(presetOptions, resolved.options)
|
|
531
|
+
},
|
|
532
|
+
{
|
|
533
|
+
forceLevel: level,
|
|
534
|
+
defaultShowLevelTag: true,
|
|
535
|
+
defaultContext
|
|
536
|
+
}
|
|
537
|
+
);
|
|
538
|
+
};
|
|
539
|
+
return method;
|
|
540
|
+
}
|
|
541
|
+
function createLogger(presetOptions) {
|
|
542
|
+
const baseContext = presetOptions?.context;
|
|
543
|
+
const fn = (arg1, arg2, arg3) => {
|
|
544
|
+
const resolved = resolveLogArgs(arg1, arg2, arg3);
|
|
545
|
+
runLog(
|
|
546
|
+
{
|
|
547
|
+
...resolved,
|
|
548
|
+
options: mergeOptions(presetOptions, resolved.options)
|
|
549
|
+
},
|
|
550
|
+
{
|
|
551
|
+
defaultContext: baseContext
|
|
552
|
+
}
|
|
553
|
+
);
|
|
554
|
+
};
|
|
555
|
+
const logger = fn;
|
|
556
|
+
logger.info = makeLevelMethod("info", presetOptions, baseContext);
|
|
557
|
+
logger.success = makeLevelMethod("success", presetOptions, baseContext);
|
|
558
|
+
logger.warn = makeLevelMethod("warn", presetOptions, baseContext);
|
|
559
|
+
logger.error = makeLevelMethod("error", presetOptions, baseContext);
|
|
560
|
+
logger.debug = makeLevelMethod("debug", presetOptions, baseContext);
|
|
561
|
+
logger.once = ((key, arg2, arg3, arg4) => {
|
|
562
|
+
const resolved = resolveOnceArgs(key, arg2, arg3, arg4);
|
|
563
|
+
const merged = mergeOptions(presetOptions, resolved.options);
|
|
564
|
+
if (merged?.enabled === false) return;
|
|
565
|
+
if (!checkAndMarkOnce(key)) return;
|
|
566
|
+
runLog(
|
|
567
|
+
{
|
|
568
|
+
label: resolved.label,
|
|
569
|
+
value: resolved.value,
|
|
570
|
+
options: merged
|
|
571
|
+
},
|
|
572
|
+
{
|
|
573
|
+
defaultContext: baseContext
|
|
574
|
+
}
|
|
575
|
+
);
|
|
576
|
+
});
|
|
577
|
+
logger.time = ((key, options) => {
|
|
578
|
+
const merged = mergeOptions(presetOptions, options);
|
|
579
|
+
if (merged?.enabled === false) return;
|
|
580
|
+
const now = merged?.clockNow?.() ?? Date.now();
|
|
581
|
+
startTimer(key, now);
|
|
582
|
+
});
|
|
583
|
+
logger.timeEnd = ((key, arg2, arg3, arg4) => {
|
|
584
|
+
const resolved = resolveTimeEndArgs(key, arg2, arg3, arg4);
|
|
585
|
+
const merged = mergeOptions(presetOptions, resolved.options);
|
|
586
|
+
if (merged?.enabled === false) return;
|
|
587
|
+
const now = merged?.clockNow?.() ?? Date.now();
|
|
588
|
+
const durationMs = endTimer(key, now);
|
|
589
|
+
if (durationMs == null) {
|
|
590
|
+
runLog(
|
|
591
|
+
{
|
|
592
|
+
label: resolved.label ?? "timer",
|
|
593
|
+
value: { key, error: "timer_not_started" },
|
|
594
|
+
options: merged
|
|
595
|
+
},
|
|
596
|
+
{
|
|
597
|
+
forceLevel: "warn",
|
|
598
|
+
defaultShowLevelTag: true,
|
|
599
|
+
defaultContext: baseContext
|
|
600
|
+
}
|
|
601
|
+
);
|
|
602
|
+
return;
|
|
603
|
+
}
|
|
604
|
+
const timedPayload = merged?.includeDurationOnly ? { durationMs } : {
|
|
605
|
+
key,
|
|
606
|
+
durationMs,
|
|
607
|
+
...resolved.value !== void 0 ? { value: resolved.value } : {}
|
|
608
|
+
};
|
|
609
|
+
const level = resolveTimingLevel(durationMs, merged);
|
|
610
|
+
runLog(
|
|
611
|
+
{
|
|
612
|
+
label: resolved.label ?? "timer",
|
|
613
|
+
value: timedPayload,
|
|
614
|
+
options: merged
|
|
615
|
+
},
|
|
616
|
+
{
|
|
617
|
+
forceLevel: level,
|
|
618
|
+
defaultShowLevelTag: true,
|
|
619
|
+
defaultContext: baseContext
|
|
620
|
+
}
|
|
621
|
+
);
|
|
622
|
+
});
|
|
623
|
+
logger.withContext = (context) => {
|
|
624
|
+
return createLogger(mergeOptions(presetOptions, { context }));
|
|
625
|
+
};
|
|
626
|
+
logger.resetOnce = (keys) => clearOnce(keys);
|
|
627
|
+
logger.resetTimers = (keys) => clearTimers(keys);
|
|
628
|
+
return logger;
|
|
629
|
+
}
|
|
630
|
+
function withContext(context, presetOptions) {
|
|
631
|
+
return createLogger(mergeOptions(presetOptions, { context }));
|
|
184
632
|
}
|
|
633
|
+
var info = makeLevelMethod("info");
|
|
634
|
+
var success = makeLevelMethod("success");
|
|
635
|
+
var warn = makeLevelMethod("warn");
|
|
636
|
+
var error = makeLevelMethod("error");
|
|
637
|
+
var debug = makeLevelMethod("debug");
|
|
185
638
|
var __internal = {
|
|
186
639
|
getCallerFromStack,
|
|
187
640
|
parseFrameLine,
|
|
188
641
|
formatLabeledValue,
|
|
189
|
-
isLogOptions
|
|
642
|
+
isLogOptions,
|
|
643
|
+
resolveLogArgs,
|
|
644
|
+
mergeOptions,
|
|
645
|
+
writeLine,
|
|
646
|
+
resolveOnceArgs,
|
|
647
|
+
resolveTimeEndArgs,
|
|
648
|
+
resolveTimingLevel
|
|
190
649
|
};
|
|
191
650
|
// Annotate the CommonJS export names for ESM import in node:
|
|
192
651
|
0 && (module.exports = {
|
|
193
652
|
__internal,
|
|
194
|
-
|
|
653
|
+
createLogger,
|
|
654
|
+
debug,
|
|
655
|
+
error,
|
|
656
|
+
info,
|
|
657
|
+
log,
|
|
658
|
+
logDev,
|
|
659
|
+
logProd,
|
|
660
|
+
once,
|
|
661
|
+
resetOnce,
|
|
662
|
+
resetTimers,
|
|
663
|
+
success,
|
|
664
|
+
time,
|
|
665
|
+
timeEnd,
|
|
666
|
+
warn,
|
|
667
|
+
withContext
|
|
195
668
|
});
|