where-log 0.1.1 → 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 +90 -3
- package/dist/index.cjs +560 -37
- package/dist/index.d.cts +107 -4
- package/dist/index.d.ts +107 -4
- package/dist/index.mjs +544 -37
- package/package.json +5 -2
package/dist/index.cjs
CHANGED
|
@@ -31,21 +31,173 @@ 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);
|
|
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
|
+
|
|
81
|
+
// src/core/format.ts
|
|
37
82
|
var import_node_util = __toESM(require("util"), 1);
|
|
38
|
-
var import_node_path = __toESM(require("path"), 1);
|
|
39
83
|
function isNodeRuntime() {
|
|
40
84
|
return typeof process !== "undefined" && !!process.versions?.node;
|
|
41
85
|
}
|
|
86
|
+
function safeFastStringify(value) {
|
|
87
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
88
|
+
return JSON.stringify(value, (_k, v) => {
|
|
89
|
+
if (typeof v === "object" && v !== null) {
|
|
90
|
+
if (seen.has(v)) return "[Circular]";
|
|
91
|
+
seen.add(v);
|
|
92
|
+
}
|
|
93
|
+
return v;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
function formatFastValue(value) {
|
|
97
|
+
if (typeof value === "string") return value;
|
|
98
|
+
if (typeof value === "number") return String(value);
|
|
99
|
+
if (typeof value === "boolean") return String(value);
|
|
100
|
+
if (typeof value === "bigint") return value.toString();
|
|
101
|
+
if (typeof value === "undefined") return "undefined";
|
|
102
|
+
if (value === null) return "null";
|
|
103
|
+
if (typeof value === "symbol") return value.toString();
|
|
104
|
+
if (typeof value === "function") {
|
|
105
|
+
return `[Function ${value.name || "anonymous"}]`;
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
return safeFastStringify(value);
|
|
109
|
+
} catch {
|
|
110
|
+
return "[Unserializable]";
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function formatValue(value, options) {
|
|
114
|
+
const mode = options?.mode ?? "pretty";
|
|
115
|
+
if (mode === "fast") {
|
|
116
|
+
return formatFastValue(value);
|
|
117
|
+
}
|
|
118
|
+
if (!isNodeRuntime()) {
|
|
119
|
+
return value;
|
|
120
|
+
}
|
|
121
|
+
return import_node_util.default.inspect(value, {
|
|
122
|
+
depth: options?.inspectDepth ?? null,
|
|
123
|
+
colors: options?.colors ?? true,
|
|
124
|
+
compact: false
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
function formatLabeledValue(label, formattedValue) {
|
|
128
|
+
if (!label) return formattedValue;
|
|
129
|
+
if (typeof formattedValue === "string") {
|
|
130
|
+
return `${label}: ${formattedValue}`;
|
|
131
|
+
}
|
|
132
|
+
return `${label}: ${String(formattedValue)}`;
|
|
133
|
+
}
|
|
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
|
+
|
|
191
|
+
// src/core/stack.ts
|
|
42
192
|
function safeToInt(input) {
|
|
43
193
|
const n = Number.parseInt(input, 10);
|
|
44
194
|
return Number.isFinite(n) ? n : 0;
|
|
45
195
|
}
|
|
46
|
-
function
|
|
196
|
+
function basename(filePath) {
|
|
47
197
|
const clean = filePath.replace(/^file:\/\//, "");
|
|
48
|
-
|
|
198
|
+
const normalized = clean.replace(/\\/g, "/");
|
|
199
|
+
const last = normalized.lastIndexOf("/");
|
|
200
|
+
return last >= 0 ? normalized.slice(last + 1) : normalized;
|
|
49
201
|
}
|
|
50
202
|
function parseFrameLine(frame) {
|
|
51
203
|
const cleaned = frame.trim();
|
|
@@ -53,14 +205,14 @@ function parseFrameLine(frame) {
|
|
|
53
205
|
const v8Match = cleaned.match(/(?:at\s+)?(?:.+\s+\()?(.+):(\d+):(\d+)\)?$/);
|
|
54
206
|
if (v8Match) {
|
|
55
207
|
return {
|
|
56
|
-
file:
|
|
208
|
+
file: basename(v8Match[1]),
|
|
57
209
|
line: safeToInt(v8Match[2])
|
|
58
210
|
};
|
|
59
211
|
}
|
|
60
212
|
const ffMatch = cleaned.match(/@(.+):(\d+):(\d+)$/);
|
|
61
213
|
if (ffMatch) {
|
|
62
214
|
return {
|
|
63
|
-
file:
|
|
215
|
+
file: basename(ffMatch[1]),
|
|
64
216
|
line: safeToInt(ffMatch[2])
|
|
65
217
|
};
|
|
66
218
|
}
|
|
@@ -68,41 +220,105 @@ function parseFrameLine(frame) {
|
|
|
68
220
|
}
|
|
69
221
|
function getCallerFromStack(stack) {
|
|
70
222
|
if (!stack) return { file: "unknown", line: 0 };
|
|
71
|
-
const frames = stack.split("\n")
|
|
72
|
-
for (
|
|
223
|
+
const frames = stack.split("\n");
|
|
224
|
+
for (let i = 0; i < frames.length; i += 1) {
|
|
225
|
+
const frame = frames[i].trim();
|
|
226
|
+
if (!frame) continue;
|
|
73
227
|
if (frame.includes("getCallerFromStack")) continue;
|
|
74
228
|
if (frame.includes("at log")) continue;
|
|
75
229
|
if (frame.includes("at Object.log")) continue;
|
|
76
230
|
if (frame.includes("/src/index.ts")) continue;
|
|
77
231
|
if (frame.includes("\\src\\index.ts")) continue;
|
|
232
|
+
if (frame.includes("/src/core/")) continue;
|
|
233
|
+
if (frame.includes("\\src\\core\\")) continue;
|
|
78
234
|
const parsed = parseFrameLine(frame);
|
|
79
235
|
if (parsed) return parsed;
|
|
80
236
|
}
|
|
81
237
|
return { file: "unknown", line: 0 };
|
|
82
238
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
return
|
|
239
|
+
|
|
240
|
+
// src/core/transform.ts
|
|
241
|
+
function isObjectLike(value) {
|
|
242
|
+
return typeof value === "object" && value !== null;
|
|
87
243
|
}
|
|
88
|
-
function
|
|
89
|
-
if (!
|
|
90
|
-
|
|
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;
|
|
91
254
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
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;
|
|
97
261
|
}
|
|
98
|
-
function
|
|
99
|
-
if (!
|
|
100
|
-
|
|
101
|
-
|
|
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;
|
|
102
274
|
}
|
|
103
|
-
|
|
275
|
+
if (!(head in target)) return;
|
|
276
|
+
if (rest.length === 0) {
|
|
277
|
+
target[head] = "[REDACTED]";
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
redactPath(target[head], rest);
|
|
104
281
|
}
|
|
105
|
-
function
|
|
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
|
+
|
|
315
|
+
// src/index.ts
|
|
316
|
+
function isLogOptions(value) {
|
|
317
|
+
if (!value || typeof value !== "object") return false;
|
|
318
|
+
const record = value;
|
|
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;
|
|
320
|
+
}
|
|
321
|
+
function resolveLogArgs(arg1, arg2, arg3) {
|
|
106
322
|
let label;
|
|
107
323
|
let value;
|
|
108
324
|
let options;
|
|
@@ -114,32 +330,339 @@ function log(arg1, arg2, arg3) {
|
|
|
114
330
|
value = arg1;
|
|
115
331
|
options = isLogOptions(arg2) ? arg2 : void 0;
|
|
116
332
|
}
|
|
117
|
-
|
|
118
|
-
|
|
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);
|
|
387
|
+
const includeLocation = options?.includeLocation ?? true;
|
|
388
|
+
const stack = includeLocation ? new Error().stack : void 0;
|
|
389
|
+
const caller = includeLocation ? getCallerFromStack(stack) : { file: "disabled", line: 0 };
|
|
119
390
|
const location = `${caller.file}:${caller.line}`;
|
|
120
|
-
const
|
|
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
|
+
});
|
|
397
|
+
const formatOptions = {
|
|
398
|
+
colors: options?.colors,
|
|
399
|
+
mode: options?.mode,
|
|
400
|
+
inspectDepth: options?.inspectDepth
|
|
401
|
+
};
|
|
402
|
+
const formattedValue = formatValue(transformedValue, formatOptions);
|
|
403
|
+
const levelTag = level ? levelToTag(level) : void 0;
|
|
121
404
|
if (options?.formatter) {
|
|
122
405
|
const formatted = options.formatter({
|
|
123
406
|
location,
|
|
124
407
|
label,
|
|
125
|
-
value,
|
|
126
|
-
formattedValue
|
|
408
|
+
value: transformedValue,
|
|
409
|
+
formattedValue,
|
|
410
|
+
level,
|
|
411
|
+
levelTag
|
|
127
412
|
});
|
|
128
|
-
|
|
129
|
-
|
|
413
|
+
writeLine(consoleMethod, formatted.locationLine);
|
|
414
|
+
writeLine(consoleMethod, formatted.valueLine);
|
|
130
415
|
return;
|
|
131
416
|
}
|
|
132
|
-
|
|
133
|
-
|
|
417
|
+
if (includeLocation) {
|
|
418
|
+
writeLine(consoleMethod, location);
|
|
419
|
+
}
|
|
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 }));
|
|
134
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");
|
|
135
638
|
var __internal = {
|
|
136
639
|
getCallerFromStack,
|
|
137
640
|
parseFrameLine,
|
|
138
641
|
formatLabeledValue,
|
|
139
|
-
isLogOptions
|
|
642
|
+
isLogOptions,
|
|
643
|
+
resolveLogArgs,
|
|
644
|
+
mergeOptions,
|
|
645
|
+
writeLine,
|
|
646
|
+
resolveOnceArgs,
|
|
647
|
+
resolveTimeEndArgs,
|
|
648
|
+
resolveTimingLevel
|
|
140
649
|
};
|
|
141
650
|
// Annotate the CommonJS export names for ESM import in node:
|
|
142
651
|
0 && (module.exports = {
|
|
143
652
|
__internal,
|
|
144
|
-
|
|
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
|
|
145
668
|
});
|