@visulima/pail 3.2.2 → 4.0.0-alpha.10
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/CHANGELOG.md +188 -0
- package/LICENSE.md +434 -6
- package/README.md +355 -24
- package/dist/error.d.ts +104 -0
- package/dist/error.js +76 -0
- package/dist/index.browser.d.ts +3 -1
- package/dist/index.browser.js +1490 -3
- package/dist/index.server.d.ts +3 -1
- package/dist/index.server.js +2668 -14
- package/dist/interactive/index.js +2 -2
- package/dist/middleware/elysia.d.ts +71 -0
- package/dist/middleware/elysia.js +70 -0
- package/dist/middleware/express.d.ts +86 -0
- package/dist/middleware/express.js +29 -0
- package/dist/middleware/fastify.d.ts +81 -0
- package/dist/middleware/fastify.js +46 -0
- package/dist/middleware/hono.d.ts +85 -0
- package/dist/middleware/hono.js +33 -0
- package/dist/middleware/next/handler.d.ts +36 -0
- package/dist/middleware/next/handler.js +53 -0
- package/dist/middleware/next/middleware.d.ts +59 -0
- package/dist/middleware/next/storage.d.ts +14 -0
- package/dist/middleware/shared/create-middleware-logger.d.ts +82 -0
- package/dist/middleware/shared/headers.d.ts +14 -0
- package/dist/middleware/shared/routes.d.ts +30 -0
- package/dist/middleware/shared/storage.d.ts +29 -0
- package/dist/middleware/sveltekit.d.ts +123 -0
- package/dist/middleware/sveltekit.js +43 -0
- package/dist/object-tree.d.ts +2 -2
- package/dist/object-tree.js +7 -7
- package/dist/packem_shared/{AbstractJsonReporter-intFdT_A.js → AbstractJsonReporter-CjtVgHbU.js} +112 -28
- package/dist/packem_shared/AbstractJsonReporter-DlugSJpY.js +288 -0
- package/dist/packem_shared/{InteractiveManager-CZ85hGNW.js → InteractiveManager-CowYA3Hx.js} +17 -11
- package/dist/packem_shared/{interactive-stream-hook-DG4BtN12.js → InteractiveStreamHook-BypRlYTX.js} +3 -11
- package/dist/packem_shared/{JsonReporter-C0AXk99i.js → JsonReporter-BgPvIyC2.js} +2 -2
- package/dist/packem_shared/{JsonReporter-DcM2LBX9.js → JsonReporter-Dbw82ewj.js} +2 -2
- package/dist/packem_shared/{PrettyReporter-CuLLKr6-.js → PrettyReporter-C2dCzIaf.js} +54 -8
- package/dist/packem_shared/{format-label-Btft2KGP.js → PrettyReporter-gMqa7j_m.js} +372 -78
- package/dist/packem_shared/Spinner-Cokext9b.js +2183 -0
- package/dist/packem_shared/abstract-pretty-reporter-szQO-IgK.js +2635 -0
- package/dist/packem_shared/constants-B1RjD_ps.js +99 -0
- package/dist/packem_shared/{constants-DfDr4MHC.js → constants-omsTHUWB.js} +1 -1
- package/dist/packem_shared/createPailError-B_sgL0nF.js +76 -0
- package/dist/packem_shared/getBarChar-D7JfmdTr.js +459 -0
- package/dist/packem_shared/headers-BxHWM6KI.js +127 -0
- package/dist/packem_shared/{index-DqKWykfa.js → index-BEfVUy9P.js} +174 -64
- package/dist/packem_shared/{index-BomQ3E6J.js → index-Bx3-C0j9.js} +29 -21
- package/dist/packem_shared/pailMiddleware-Ci88geIF.js +24 -0
- package/dist/packem_shared/storage-D0vqz8OX.js +36 -0
- package/dist/packem_shared/{InteractiveStreamHook-DiSubbJ1.js → useLogger-D0rU3lcX.js} +13 -1
- package/dist/packem_shared/{write-console-log-based-on-level-DBmRYXpj.js → write-console-log-based-on-level-ree2lDPw.js} +5 -4
- package/dist/packem_shared/{write-stream-BG8fhcs3.js → write-stream-BuFtjATz.js} +1 -1
- package/dist/pail.browser.d.ts +1 -1
- package/dist/processor/environment-processor.d.ts +124 -0
- package/dist/processor/environment-processor.js +82 -0
- package/dist/processor/message-formatter-processor.d.ts +2 -2
- package/dist/processor/message-formatter-processor.js +654 -6
- package/dist/processor/opentelemetry-processor.js +4 -4
- package/dist/processor/redact-processor.d.ts +1 -1
- package/dist/processor/redact-processor.js +2 -1
- package/dist/processor/sampling-processor.d.ts +111 -0
- package/dist/processor/sampling-processor.js +59 -0
- package/dist/progress-bar.d.ts +10 -1
- package/dist/progress-bar.js +75 -20
- package/dist/reporter/file/json-file-reporter.js +1 -1
- package/dist/reporter/http/abstract-http-reporter.js +23 -26
- package/dist/reporter/http/http-reporter.edge-light.js +134 -53
- package/dist/reporter/json/abstract-json-reporter.d.ts +1 -1
- package/dist/reporter/json/index.browser.js +2 -2
- package/dist/reporter/json/index.js +2 -2
- package/dist/reporter/pretty/index.browser.js +1 -1
- package/dist/reporter/pretty/index.js +1 -1
- package/dist/reporter/simple/simple-reporter.server.js +8 -12
- package/dist/spinner.js +37 -4
- package/dist/types.d.ts +4 -4
- package/dist/wide-event.d.ts +300 -0
- package/dist/wide-event.js +283 -0
- package/package.json +73 -8
- package/dist/packem_shared/PrettyReporter-BFWaYP_J.js +0 -222
- package/dist/packem_shared/abstract-pretty-reporter-DMPDCslJ.js +0 -50
- package/dist/packem_shared/get-longest-label-C9PWeyKq.js +0 -9
- package/dist/packem_shared/pail.browser-CPjQrsyy.js +0 -1427
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
const getLongestLabel = (types) => {
|
|
2
|
+
const labels = Object.keys(types).map((x) => types[x].label ?? "");
|
|
3
|
+
if (labels.length === 0) {
|
|
4
|
+
return "";
|
|
5
|
+
}
|
|
6
|
+
return labels.reduce((x, y) => x.length > y.length ? x : y, "");
|
|
7
|
+
};
|
|
8
|
+
|
|
1
9
|
const colorKeywords = /* @__PURE__ */ new Map([
|
|
2
10
|
["aliceblue", "#f0f8ff"],
|
|
3
11
|
["antiquewhite", "#faebd7"],
|
|
@@ -163,7 +171,7 @@ const getDefaultCss = () => {
|
|
|
163
171
|
textDecorationLine: []
|
|
164
172
|
};
|
|
165
173
|
};
|
|
166
|
-
const SPACE_PATTERN = /\s
|
|
174
|
+
const SPACE_PATTERN = /\s+/;
|
|
167
175
|
const parseCssColor = (colorString) => {
|
|
168
176
|
if (colorKeywords.has(colorString)) {
|
|
169
177
|
colorString = colorKeywords.get(colorString);
|
|
@@ -229,10 +237,10 @@ const parseCss = (cssString) => {
|
|
|
229
237
|
let currentPart = "";
|
|
230
238
|
for (const c of cssString) {
|
|
231
239
|
if (c === "(") {
|
|
232
|
-
parenthesesDepth
|
|
240
|
+
parenthesesDepth += 1;
|
|
233
241
|
} else if (parenthesesDepth > 0) {
|
|
234
242
|
if (c === ")") {
|
|
235
|
-
parenthesesDepth
|
|
243
|
+
parenthesesDepth -= 1;
|
|
236
244
|
}
|
|
237
245
|
} else if (inValue) {
|
|
238
246
|
if (c === ";") {
|
|
@@ -365,14 +373,14 @@ const cssToAnsi = (css, previousCss = null) => {
|
|
|
365
373
|
default: {
|
|
366
374
|
if (Array.isArray(css.backgroundColor)) {
|
|
367
375
|
const { 0: r, 1: g, 2: b } = css.backgroundColor;
|
|
368
|
-
ansi += `\x1B[48;2;${r};${g};${b}m`;
|
|
376
|
+
ansi += `\x1B[48;2;${String(r)};${String(g)};${String(b)}m`;
|
|
369
377
|
} else {
|
|
370
378
|
const parsed = parseCssColor(css.backgroundColor);
|
|
371
379
|
if (parsed === null) {
|
|
372
380
|
ansi += "\x1B[49m";
|
|
373
381
|
} else {
|
|
374
382
|
const { 0: r, 1: g, 2: b } = parsed;
|
|
375
|
-
ansi += `\x1B[48;2;${r};${g};${b}m`;
|
|
383
|
+
ansi += `\x1B[48;2;${String(r)};${String(g)};${String(b)}m`;
|
|
376
384
|
}
|
|
377
385
|
}
|
|
378
386
|
}
|
|
@@ -419,14 +427,14 @@ const cssToAnsi = (css, previousCss = null) => {
|
|
|
419
427
|
default: {
|
|
420
428
|
if (Array.isArray(css.color)) {
|
|
421
429
|
const { 0: r, 1: g, 2: b } = css.color;
|
|
422
|
-
ansi += `\x1B[38;2;${r};${g};${b}m`;
|
|
430
|
+
ansi += `\x1B[38;2;${String(r)};${String(g)};${String(b)}m`;
|
|
423
431
|
} else {
|
|
424
432
|
const parsed = parseCssColor(css.color);
|
|
425
433
|
if (parsed === null) {
|
|
426
434
|
ansi += "\x1B[39m";
|
|
427
435
|
} else {
|
|
428
436
|
const { 0: r, 1: g, 2: b } = parsed;
|
|
429
|
-
ansi += `\x1B[38;2;${r};${g};${b}m`;
|
|
437
|
+
ansi += `\x1B[38;2;${String(r)};${String(g)};${String(b)}m`;
|
|
430
438
|
}
|
|
431
439
|
}
|
|
432
440
|
}
|
|
@@ -444,7 +452,7 @@ const cssToAnsi = (css, previousCss = null) => {
|
|
|
444
452
|
ansi += "\x1B[59m";
|
|
445
453
|
} else {
|
|
446
454
|
const { 0: r, 1: g, 2: b } = css.textDecorationColor;
|
|
447
|
-
ansi += `\x1B[58;2;${r};${g};${b}m`;
|
|
455
|
+
ansi += `\x1B[58;2;${String(r)};${String(g)};${String(b)}m`;
|
|
448
456
|
}
|
|
449
457
|
}
|
|
450
458
|
if (css.textDecorationLine.includes("line-through") !== previousCss.textDecorationLine.includes("line-through")) {
|
|
@@ -487,7 +495,7 @@ const format = (fmt, arguments_ = [], options = {}) => {
|
|
|
487
495
|
}
|
|
488
496
|
const objects = Array.from({ length: argumentsLength });
|
|
489
497
|
objects[0] = stringify(fmt);
|
|
490
|
-
for (let index = 1; index < argumentsLength; index
|
|
498
|
+
for (let index = 1; index < argumentsLength; index += 1) {
|
|
491
499
|
objects[index] = stringify(arguments_[index - offset]);
|
|
492
500
|
}
|
|
493
501
|
return objects.join(" ");
|
|
@@ -505,7 +513,7 @@ const format = (fmt, arguments_ = [], options = {}) => {
|
|
|
505
513
|
lastPosition = lastPosition > -1 ? lastPosition : 0;
|
|
506
514
|
const c = fmt.codePointAt(index + 1);
|
|
507
515
|
if (c === void 0) {
|
|
508
|
-
|
|
516
|
+
a += 1;
|
|
509
517
|
break;
|
|
510
518
|
}
|
|
511
519
|
switch (c) {
|
|
@@ -522,7 +530,7 @@ const format = (fmt, arguments_ = [], options = {}) => {
|
|
|
522
530
|
}
|
|
523
531
|
}
|
|
524
532
|
lastPosition = index + 2;
|
|
525
|
-
index
|
|
533
|
+
index += 1;
|
|
526
534
|
break;
|
|
527
535
|
}
|
|
528
536
|
case CHAR_d:
|
|
@@ -535,7 +543,7 @@ const format = (fmt, arguments_ = [], options = {}) => {
|
|
|
535
543
|
}
|
|
536
544
|
result += Number(arguments_[a]).toString();
|
|
537
545
|
lastPosition = index + 2;
|
|
538
|
-
index
|
|
546
|
+
index += 1;
|
|
539
547
|
break;
|
|
540
548
|
}
|
|
541
549
|
case CHAR_i: {
|
|
@@ -547,7 +555,7 @@ const format = (fmt, arguments_ = [], options = {}) => {
|
|
|
547
555
|
}
|
|
548
556
|
result += Math.floor(Number(arguments_[a])).toString();
|
|
549
557
|
lastPosition = index + 2;
|
|
550
|
-
index
|
|
558
|
+
index += 1;
|
|
551
559
|
break;
|
|
552
560
|
}
|
|
553
561
|
case CHAR_j:
|
|
@@ -573,7 +581,7 @@ const format = (fmt, arguments_ = [], options = {}) => {
|
|
|
573
581
|
}
|
|
574
582
|
result += stringify(temporaryArgument);
|
|
575
583
|
lastPosition = index + 2;
|
|
576
|
-
index
|
|
584
|
+
index += 1;
|
|
577
585
|
break;
|
|
578
586
|
}
|
|
579
587
|
case CHAR_PERCENT: {
|
|
@@ -582,8 +590,8 @@ const format = (fmt, arguments_ = [], options = {}) => {
|
|
|
582
590
|
}
|
|
583
591
|
result += "%";
|
|
584
592
|
lastPosition = index + 2;
|
|
585
|
-
index
|
|
586
|
-
a
|
|
593
|
+
index += 1;
|
|
594
|
+
a -= 1;
|
|
587
595
|
break;
|
|
588
596
|
}
|
|
589
597
|
case CHAR_s: {
|
|
@@ -595,7 +603,7 @@ const format = (fmt, arguments_ = [], options = {}) => {
|
|
|
595
603
|
}
|
|
596
604
|
result += typeof arguments_[a] === "object" ? stringify(arguments_[a]) : String(arguments_[a]);
|
|
597
605
|
lastPosition = index + 2;
|
|
598
|
-
index
|
|
606
|
+
index += 1;
|
|
599
607
|
break;
|
|
600
608
|
}
|
|
601
609
|
default: {
|
|
@@ -605,13 +613,13 @@ const format = (fmt, arguments_ = [], options = {}) => {
|
|
|
605
613
|
}
|
|
606
614
|
result += options.formatters[c](arguments_[a]);
|
|
607
615
|
lastPosition = index + 2;
|
|
608
|
-
index
|
|
616
|
+
index += 1;
|
|
609
617
|
}
|
|
610
618
|
}
|
|
611
619
|
}
|
|
612
|
-
|
|
620
|
+
a += 1;
|
|
613
621
|
}
|
|
614
|
-
|
|
622
|
+
index += 1;
|
|
615
623
|
}
|
|
616
624
|
if (lastPosition === -1) {
|
|
617
625
|
return fmt;
|
|
@@ -647,4 +655,4 @@ const build = (options = {}) => {
|
|
|
647
655
|
return (f, arguments_ = [], formatOptions = {}) => format(f, arguments_, { ...formatOptions, formatters });
|
|
648
656
|
};
|
|
649
657
|
|
|
650
|
-
export { build as b, format as f };
|
|
658
|
+
export { build as b, format as f, getLongestLabel as g };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const GLOB_STRIP_RE = /\*+/g;
|
|
2
|
+
const pailMiddleware = (NextResponseClass, options) => {
|
|
3
|
+
const { exclude, include } = options ?? {};
|
|
4
|
+
return (request) => {
|
|
5
|
+
const path = request.nextUrl.pathname;
|
|
6
|
+
if (exclude?.some((p) => path.startsWith(p.replaceAll(GLOB_STRIP_RE, "")))) {
|
|
7
|
+
return NextResponseClass.next();
|
|
8
|
+
}
|
|
9
|
+
if (include?.length && !include.some((p) => path.startsWith(p.replaceAll(GLOB_STRIP_RE, "")))) {
|
|
10
|
+
return NextResponseClass.next();
|
|
11
|
+
}
|
|
12
|
+
const requestId = request.headers.get("x-request-id") ?? crypto.randomUUID();
|
|
13
|
+
const requestHeaders = new Headers(request.headers);
|
|
14
|
+
requestHeaders.set("x-request-id", requestId);
|
|
15
|
+
requestHeaders.set("x-pail-start", String(Date.now()));
|
|
16
|
+
const response = NextResponseClass.next({
|
|
17
|
+
request: { headers: requestHeaders }
|
|
18
|
+
});
|
|
19
|
+
response.headers.set("x-request-id", requestId);
|
|
20
|
+
return response;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export { pailMiddleware };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { createRequire as __cjs_createRequire } from "node:module";
|
|
2
|
+
|
|
3
|
+
const __cjs_require = __cjs_createRequire(import.meta.url);
|
|
4
|
+
|
|
5
|
+
const __cjs_getProcess = typeof globalThis !== "undefined" && typeof globalThis.process !== "undefined" ? globalThis.process : process;
|
|
6
|
+
|
|
7
|
+
const __cjs_getBuiltinModule = (module) => {
|
|
8
|
+
// Check if we're in Node.js and version supports getBuiltinModule
|
|
9
|
+
if (typeof __cjs_getProcess !== "undefined" && __cjs_getProcess.versions && __cjs_getProcess.versions.node) {
|
|
10
|
+
const [major, minor] = __cjs_getProcess.versions.node.split(".").map(Number);
|
|
11
|
+
// Node.js 20.16.0+ and 22.3.0+
|
|
12
|
+
if (major > 22 || (major === 22 && minor >= 3) || (major === 20 && minor >= 16)) {
|
|
13
|
+
return __cjs_getProcess.getBuiltinModule(module);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
// Fallback to createRequire
|
|
17
|
+
return __cjs_require(module);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const {
|
|
21
|
+
AsyncLocalStorage
|
|
22
|
+
} = __cjs_getBuiltinModule("node:async_hooks");
|
|
23
|
+
|
|
24
|
+
const createLoggerStorage = (contextHint) => {
|
|
25
|
+
const storage = new AsyncLocalStorage();
|
|
26
|
+
const useLogger = () => {
|
|
27
|
+
const logger = storage.getStore();
|
|
28
|
+
if (!logger) {
|
|
29
|
+
throw new Error(`[pail] useLogger() called outside of ${contextHint}`);
|
|
30
|
+
}
|
|
31
|
+
return logger;
|
|
32
|
+
};
|
|
33
|
+
return { storage, useLogger };
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export { createLoggerStorage as c };
|
|
@@ -17,5 +17,17 @@ const __cjs_getBuiltinModule = (module) => {
|
|
|
17
17
|
return __cjs_require(module);
|
|
18
18
|
};
|
|
19
19
|
|
|
20
|
+
const {
|
|
21
|
+
AsyncLocalStorage
|
|
22
|
+
} = __cjs_getBuiltinModule("node:async_hooks");
|
|
20
23
|
|
|
21
|
-
|
|
24
|
+
const pailStorage = new AsyncLocalStorage();
|
|
25
|
+
const useLogger = () => {
|
|
26
|
+
const logger = pailStorage.getStore();
|
|
27
|
+
if (!logger) {
|
|
28
|
+
throw new Error("[pail] useLogger() called outside of withPail() context. Wrap your route handler or server action with withPail().");
|
|
29
|
+
}
|
|
30
|
+
return logger;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export { pailStorage, useLogger };
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
const writeConsoleLogBasedOnLevel = (level) => {
|
|
2
|
+
const c = console;
|
|
2
3
|
if (level === "error") {
|
|
3
|
-
return
|
|
4
|
+
return c.__error ?? console.error;
|
|
4
5
|
}
|
|
5
6
|
if (level === "warn") {
|
|
6
|
-
return
|
|
7
|
+
return c.__warn ?? console.warn;
|
|
7
8
|
}
|
|
8
9
|
if (level === "trace") {
|
|
9
|
-
return
|
|
10
|
+
return c.__trace ?? console.trace;
|
|
10
11
|
}
|
|
11
|
-
return
|
|
12
|
+
return c.__log ?? console.log;
|
|
12
13
|
};
|
|
13
14
|
|
|
14
15
|
export { writeConsoleLogBasedOnLevel as w };
|
package/dist/pail.browser.d.ts
CHANGED
|
@@ -403,7 +403,7 @@ export declare class PailBrowserImpl<T extends string = string, L extends string
|
|
|
403
403
|
protected extendReporter(reporter: Reporter<L>): Reporter<L>;
|
|
404
404
|
protected registerReporters(reporters: Reporter<L>[]): void;
|
|
405
405
|
protected registerProcessors(processors: Processor<L>[]): void;
|
|
406
|
-
protected logger(type: LiteralUnion<DefaultLogTypes, T>, raw: boolean, force: boolean, ...messageObject:
|
|
406
|
+
protected logger(type: LiteralUnion<DefaultLogTypes, T>, raw: boolean, force: boolean, ...messageObject: unknown[]): void;
|
|
407
407
|
}
|
|
408
408
|
export type PailBrowserType<T extends string = string, L extends string = string> = Console & (new <TC extends string = string, LC extends string = string>(options?: ConstructorOptions<TC, LC>) => PailBrowserType<TC, LC>) & PailBrowserImpl<T, L> & Record<DefaultLogTypes, LoggerFunction> & Record<T, LoggerFunction> & {
|
|
409
409
|
force: Record<DefaultLogTypes, LoggerFunction> & Record<T, LoggerFunction>;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { Meta, Processor } from "../types.d.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Detected environment information.
|
|
4
|
+
*
|
|
5
|
+
* Contains runtime environment details that are automatically detected
|
|
6
|
+
* from environment variables and process information.
|
|
7
|
+
*/
|
|
8
|
+
interface EnvironmentInfo {
|
|
9
|
+
/** Git commit hash or short SHA */
|
|
10
|
+
commit?: string;
|
|
11
|
+
/** Runtime environment (e.g., "production", "development", "test") */
|
|
12
|
+
environment?: string;
|
|
13
|
+
/** Hostname of the machine */
|
|
14
|
+
hostname?: string;
|
|
15
|
+
/** Process ID */
|
|
16
|
+
pid?: number;
|
|
17
|
+
/** Cloud region (e.g., "us-east-1") */
|
|
18
|
+
region?: string;
|
|
19
|
+
/** Application/service name */
|
|
20
|
+
service?: string;
|
|
21
|
+
/** Application version */
|
|
22
|
+
version?: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Environment processor configuration options.
|
|
26
|
+
*/
|
|
27
|
+
interface EnvironmentProcessorOptions {
|
|
28
|
+
/**
|
|
29
|
+
* Whether to include the hostname. Defaults to false.
|
|
30
|
+
*/
|
|
31
|
+
includeHostname?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Whether to include the process ID. Defaults to false.
|
|
34
|
+
*/
|
|
35
|
+
includePid?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Static environment info to use instead of or in addition to auto-detection.
|
|
38
|
+
* Values provided here take precedence over auto-detected values.
|
|
39
|
+
*/
|
|
40
|
+
overrides?: Partial<EnvironmentInfo>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Detects the runtime environment from environment variables.
|
|
44
|
+
*
|
|
45
|
+
* Checks common environment variable patterns used by various hosting
|
|
46
|
+
* platforms (Vercel, AWS, GCP, Heroku, Railway, Fly.io, Render, etc.)
|
|
47
|
+
* to automatically determine service name, version, environment, region,
|
|
48
|
+
* and commit hash.
|
|
49
|
+
* @returns Detected environment information
|
|
50
|
+
*/
|
|
51
|
+
declare const detectEnvironment: () => EnvironmentInfo;
|
|
52
|
+
/**
|
|
53
|
+
* Environment Enrichment Processor.
|
|
54
|
+
*
|
|
55
|
+
* Inspired by evlog's automatic environment detection, this processor
|
|
56
|
+
* enriches log metadata with runtime environment information. It auto-detects
|
|
57
|
+
* details like service name, version, environment, region, and commit hash
|
|
58
|
+
* from common environment variables used by popular hosting platforms.
|
|
59
|
+
*
|
|
60
|
+
* The detected info is added to the log's context, making it easier to
|
|
61
|
+
* correlate logs across services and deployments in production.
|
|
62
|
+
* @template L - The log level type
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* import { createPail } from "@visulima/pail";
|
|
66
|
+
* import EnvironmentProcessor from "@visulima/pail/processor/environment";
|
|
67
|
+
*
|
|
68
|
+
* const logger = createPail({
|
|
69
|
+
* processors: [
|
|
70
|
+
* new EnvironmentProcessor({
|
|
71
|
+
* overrides: { service: "my-api" },
|
|
72
|
+
* includePid: true,
|
|
73
|
+
* }),
|
|
74
|
+
* ],
|
|
75
|
+
* });
|
|
76
|
+
*
|
|
77
|
+
* logger.info("Server started");
|
|
78
|
+
* // Log metadata will include: { __env: { service: "my-api", environment: "production", ... } }
|
|
79
|
+
* ```
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* // With static configuration only (no auto-detection)
|
|
83
|
+
* new EnvironmentProcessor({
|
|
84
|
+
* overrides: {
|
|
85
|
+
* service: "payment-service",
|
|
86
|
+
* version: "2.1.0",
|
|
87
|
+
* environment: "staging",
|
|
88
|
+
* },
|
|
89
|
+
* });
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
declare class EnvironmentProcessor<L extends string = string> implements Processor<L> {
|
|
93
|
+
#private;
|
|
94
|
+
/**
|
|
95
|
+
* Creates a new EnvironmentProcessor instance.
|
|
96
|
+
*
|
|
97
|
+
* Auto-detects environment information on construction and merges
|
|
98
|
+
* with any provided overrides. Undefined values in overrides are
|
|
99
|
+
* filtered out so they don't overwrite detected values.
|
|
100
|
+
* @param options Processor configuration options
|
|
101
|
+
*/
|
|
102
|
+
constructor(options?: EnvironmentProcessorOptions);
|
|
103
|
+
/**
|
|
104
|
+
* Processes log metadata to add environment information.
|
|
105
|
+
*
|
|
106
|
+
* Adds a `__env` property to the meta containing detected and
|
|
107
|
+
* configured environment details. Each call receives a shallow
|
|
108
|
+
* clone of the environment info to prevent cross-record mutation.
|
|
109
|
+
* @param meta The log metadata to process
|
|
110
|
+
* @returns The processed metadata with environment info added
|
|
111
|
+
*/
|
|
112
|
+
process(meta: Meta<L>): Meta<L>;
|
|
113
|
+
/**
|
|
114
|
+
* Returns the detected environment information.
|
|
115
|
+
*
|
|
116
|
+
* Useful for inspecting what environment details were auto-detected.
|
|
117
|
+
* Returns a shallow clone to prevent external mutation.
|
|
118
|
+
* @returns The environment information object
|
|
119
|
+
*/
|
|
120
|
+
getEnvironmentInfo(): Readonly<EnvironmentInfo>;
|
|
121
|
+
}
|
|
122
|
+
export { detectEnvironment };
|
|
123
|
+
export default EnvironmentProcessor;
|
|
124
|
+
export type { EnvironmentInfo, EnvironmentProcessorOptions };
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
const detectEnvironment = () => {
|
|
2
|
+
if (!process.env) {
|
|
3
|
+
return {};
|
|
4
|
+
}
|
|
5
|
+
const { env } = process;
|
|
6
|
+
const info = {
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-unnecessary-condition, @typescript-eslint/prefer-nullish-coalescing -- env vars may be undefined or empty at runtime */
|
|
8
|
+
// Commit hash
|
|
9
|
+
commit: env.COMMIT_SHA || env.GIT_COMMIT || env.VERCEL_GIT_COMMIT_SHA?.slice(0, 7) || env.RAILWAY_GIT_COMMIT_SHA?.slice(0, 7) || env.RENDER_GIT_COMMIT?.slice(0, 7) || env.HEROKU_SLUG_COMMIT?.slice(0, 7) || env.CF_PAGES_COMMIT_SHA?.slice(0, 7) || void 0,
|
|
10
|
+
// Environment / Node env
|
|
11
|
+
environment: env.NODE_ENV || env.ENVIRONMENT || env.APP_ENV || void 0,
|
|
12
|
+
// Hostname
|
|
13
|
+
hostname: env.HOSTNAME || env.HOST || void 0,
|
|
14
|
+
// PID
|
|
15
|
+
pid: process.pid,
|
|
16
|
+
// Region (including GCP Cloud Functions FUNCTION_REGION and GOOGLE_CLOUD_REGION)
|
|
17
|
+
region: env.AWS_REGION || env.VERCEL_REGION || env.FLY_REGION || env.RENDER_REGION || env.CF_REGION || env.GOOGLE_CLOUD_REGION || env.FUNCTION_REGION || void 0,
|
|
18
|
+
// Service name - check common platform variables (including GCP Cloud Run / App Engine)
|
|
19
|
+
service: env.SERVICE_NAME || env.APP_NAME || env.K_SERVICE || env.GAE_SERVICE || env.FUNCTION_TARGET || env.VERCEL_PROJECT_PRODUCTION_URL || env.FLY_APP_NAME || env.RAILWAY_SERVICE_NAME || env.RENDER_SERVICE_NAME || env.HEROKU_APP_NAME || void 0,
|
|
20
|
+
// Version (including GCP Cloud Run K_REVISION and App Engine GAE_VERSION)
|
|
21
|
+
version: env.APP_VERSION || env.npm_package_version || env.K_REVISION || env.GAE_VERSION || env.RAILWAY_GIT_COMMIT_SHA?.slice(0, 7) || env.RENDER_GIT_COMMIT?.slice(0, 7) || void 0
|
|
22
|
+
/* eslint-enable @typescript-eslint/no-unnecessary-condition, @typescript-eslint/prefer-nullish-coalescing */
|
|
23
|
+
};
|
|
24
|
+
return Object.fromEntries(Object.entries(info).filter(([, v]) => v !== void 0));
|
|
25
|
+
};
|
|
26
|
+
class EnvironmentProcessor {
|
|
27
|
+
#envInfo;
|
|
28
|
+
/**
|
|
29
|
+
* Creates a new EnvironmentProcessor instance.
|
|
30
|
+
*
|
|
31
|
+
* Auto-detects environment information on construction and merges
|
|
32
|
+
* with any provided overrides. Undefined values in overrides are
|
|
33
|
+
* filtered out so they don't overwrite detected values.
|
|
34
|
+
* @param options Processor configuration options
|
|
35
|
+
*/
|
|
36
|
+
constructor(options = {}) {
|
|
37
|
+
const detected = detectEnvironment();
|
|
38
|
+
const cleanOverrides = {};
|
|
39
|
+
if (options.overrides) {
|
|
40
|
+
const overrideEntries = Object.entries(options.overrides);
|
|
41
|
+
for (let i = 0; i < overrideEntries.length; i += 1) {
|
|
42
|
+
const [key, value] = overrideEntries[i];
|
|
43
|
+
if (value !== void 0) {
|
|
44
|
+
cleanOverrides[key] = value;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const merged = { ...detected, ...cleanOverrides };
|
|
49
|
+
if (!options.includePid && cleanOverrides.pid === void 0) {
|
|
50
|
+
delete merged.pid;
|
|
51
|
+
}
|
|
52
|
+
if (!options.includeHostname && cleanOverrides.hostname === void 0) {
|
|
53
|
+
delete merged.hostname;
|
|
54
|
+
}
|
|
55
|
+
this.#envInfo = merged;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Processes log metadata to add environment information.
|
|
59
|
+
*
|
|
60
|
+
* Adds a `__env` property to the meta containing detected and
|
|
61
|
+
* configured environment details. Each call receives a shallow
|
|
62
|
+
* clone of the environment info to prevent cross-record mutation.
|
|
63
|
+
* @param meta The log metadata to process
|
|
64
|
+
* @returns The processed metadata with environment info added
|
|
65
|
+
*/
|
|
66
|
+
process(meta) {
|
|
67
|
+
const enriched = { ...meta, envStorage: { ...this.#envInfo } };
|
|
68
|
+
return enriched;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Returns the detected environment information.
|
|
72
|
+
*
|
|
73
|
+
* Useful for inspecting what environment details were auto-detected.
|
|
74
|
+
* Returns a shallow clone to prevent external mutation.
|
|
75
|
+
* @returns The environment information object
|
|
76
|
+
*/
|
|
77
|
+
getEnvironmentInfo() {
|
|
78
|
+
return { ...this.#envInfo };
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export { EnvironmentProcessor as default, detectEnvironment };
|
|
@@ -3,7 +3,7 @@ import type { Meta, StringifyAwareProcessor } from "../types.d.ts";
|
|
|
3
3
|
/**
|
|
4
4
|
* Message Formatter Processor.
|
|
5
5
|
*
|
|
6
|
-
* A processor that formats log messages using the {@link https://
|
|
6
|
+
* A processor that formats log messages using the {@link https://visulima.com/packages/fmt/|@visulima/fmt} library.
|
|
7
7
|
* Supports custom formatters, string interpolation, and complex object formatting.
|
|
8
8
|
* Processes both the main message and contextual data.
|
|
9
9
|
* @template L - The log level type
|
|
@@ -38,7 +38,7 @@ declare class MessageFormatterProcessor<L extends string = string> implements St
|
|
|
38
38
|
* Sets the stringify function for object serialization.
|
|
39
39
|
* @param function_ The stringify function to use for serializing objects
|
|
40
40
|
*/
|
|
41
|
-
setStringify(function_:
|
|
41
|
+
setStringify(function_: typeof JSON.stringify): void;
|
|
42
42
|
/**
|
|
43
43
|
* Processes log metadata to format messages.
|
|
44
44
|
*
|