@visulima/pail 4.0.0-alpha.1 → 4.0.0-alpha.11
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 +204 -0
- package/LICENSE.md +434 -52
- package/README.md +323 -0
- 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 -5
- package/dist/index.server.js +2380 -110
- 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-BaZ33PlE.js → AbstractJsonReporter-BO8Calb4.js} +112 -32
- package/dist/packem_shared/AbstractJsonReporter-nOj0Ft1F.js +284 -0
- package/dist/packem_shared/{JsonReporter-BRw4skd5.js → JsonReporter-CCmj7oYL.js} +2 -2
- package/dist/packem_shared/{JsonReporter-VzgyLEYz.js → JsonReporter-Ck2PIAEw.js} +2 -2
- package/dist/packem_shared/PrettyReporter-BCvyNzXO.js +2720 -0
- package/dist/packem_shared/{PrettyReporter-DySIXBjQ.js → PrettyReporter-BtTr13Ha.js} +55 -11
- package/dist/packem_shared/abstract-pretty-reporter-CXAKYCb8.js +2635 -0
- package/dist/packem_shared/constants-B1RjD_ps.js +99 -0
- package/dist/packem_shared/createPailError-B_sgL0nF.js +76 -0
- package/dist/packem_shared/headers-BxHWM6KI.js +127 -0
- 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-MDqyXmc_.js} +1 -1
- package/dist/pail.browser.d.ts +1 -1
- package/dist/pail.server.d.ts +1 -76
- package/dist/processor/caller/caller-processor.js +1 -1
- package/dist/processor/environment-processor.d.ts +124 -0
- package/dist/processor/environment-processor.js +89 -0
- package/dist/processor/message-formatter-processor.d.ts +2 -3
- package/dist/processor/message-formatter-processor.js +654 -5
- 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/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 -57
- 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/pretty/pretty-reporter.server.d.ts +1 -1
- package/dist/reporter/raw/raw-reporter.server.d.ts +1 -1
- package/dist/reporter/simple/simple-reporter.server.d.ts +1 -1
- package/dist/reporter/simple/simple-reporter.server.js +8 -12
- package/dist/types.d.ts +4 -4
- package/dist/utils/write-console-log-based-on-level.d.ts +1 -1
- package/dist/wide-event.d.ts +300 -0
- package/dist/wide-event.js +284 -0
- package/package.json +73 -20
- package/dist/interactive/index.d.ts +0 -2
- package/dist/interactive/index.js +0 -2
- package/dist/interactive/interactive-manager.d.ts +0 -108
- package/dist/interactive/interactive-stream-hook.d.ts +0 -68
- package/dist/packem_shared/InteractiveManager-CZ85hGNW.js +0 -172
- package/dist/packem_shared/PrettyReporter-DgZB2eBG.js +0 -222
- package/dist/packem_shared/abstract-pretty-reporter-Di_sdm2r.js +0 -50
- package/dist/packem_shared/format-label-De49vNPd.js +0 -1193
- package/dist/packem_shared/get-longest-label-C9PWeyKq.js +0 -9
- package/dist/packem_shared/index-DqKWykfa.js +0 -1146
- package/dist/packem_shared/interactive-stream-hook-DG4BtN12.js +0 -141
- package/dist/packem_shared/pail.browser-u2CSR_af.js +0 -1427
- package/dist/progress-bar.d.ts +0 -136
- package/dist/progress-bar.js +0 -404
- package/dist/spinner.d.ts +0 -220
- package/dist/spinner.js +0 -2150
- package/dist/utils/ansi-escapes.d.ts +0 -4
|
@@ -45,7 +45,7 @@ export declare abstract class AbstractJsonReporter<L extends string = string> im
|
|
|
45
45
|
* Sets a custom stringify function for object serialization.
|
|
46
46
|
* @param function_ The stringify function to use for serialization
|
|
47
47
|
*/
|
|
48
|
-
setStringify(function_:
|
|
48
|
+
setStringify(function_: typeof stringify): void;
|
|
49
49
|
log(meta: ReadonlyMeta<L>): void;
|
|
50
50
|
/**
|
|
51
51
|
* Template method for outputting the JSON log message.
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { AbstractJsonReporter } from '../../packem_shared/AbstractJsonReporter-
|
|
2
|
-
export { default as JsonReporter } from '../../packem_shared/JsonReporter-
|
|
1
|
+
export { AbstractJsonReporter } from '../../packem_shared/AbstractJsonReporter-BO8Calb4.js';
|
|
2
|
+
export { default as JsonReporter } from '../../packem_shared/JsonReporter-CCmj7oYL.js';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { AbstractJsonReporter } from '../../packem_shared/AbstractJsonReporter-
|
|
2
|
-
export { default as JsonReporter } from '../../packem_shared/JsonReporter-
|
|
1
|
+
export { AbstractJsonReporter } from '../../packem_shared/AbstractJsonReporter-nOj0Ft1F.js';
|
|
2
|
+
export { default as JsonReporter } from '../../packem_shared/JsonReporter-Ck2PIAEw.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { default as PrettyReporter } from '../../packem_shared/PrettyReporter-
|
|
1
|
+
export { default as PrettyReporter } from '../../packem_shared/PrettyReporter-BtTr13Ha.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { PrettyReporter } from '../../packem_shared/PrettyReporter-
|
|
1
|
+
export { PrettyReporter } from '../../packem_shared/PrettyReporter-BCvyNzXO.js';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { RenderErrorOptions } from "@visulima/error/error";
|
|
2
2
|
import type { Options as InspectorOptions } from "@visulima/inspector";
|
|
3
|
+
import type { InteractiveManager } from "@visulima/interactive-manager";
|
|
3
4
|
import type { LiteralUnion } from "type-fest";
|
|
4
|
-
import type InteractiveManager from "../../interactive/interactive-manager.d.ts";
|
|
5
5
|
import type { ExtendedRfc5424LogLevels, InteractiveStreamReporter, ReadonlyMeta } from "../../types.d.ts";
|
|
6
6
|
import type { PrettyStyleOptions } from "./abstract-pretty-reporter.d.ts";
|
|
7
7
|
import { AbstractPrettyReporter } from "./abstract-pretty-reporter.d.ts";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Options as InspectorOptions } from "@visulima/inspector";
|
|
2
|
-
import type InteractiveManager from "
|
|
2
|
+
import type { InteractiveManager } from "@visulima/interactive-manager";
|
|
3
3
|
import type { ReadonlyMeta, StreamAwareReporter } from "../../types.d.ts";
|
|
4
4
|
declare class RawReporter<L extends string = string> implements StreamAwareReporter<L> {
|
|
5
5
|
#private;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { RenderErrorOptions } from "@visulima/error";
|
|
2
2
|
import type { Options as InspectorOptions } from "@visulima/inspector";
|
|
3
|
-
import type InteractiveManager from "
|
|
3
|
+
import type { InteractiveManager } from "@visulima/interactive-manager";
|
|
4
4
|
import type { InteractiveStreamReporter, ReadonlyMeta } from "../../types.d.ts";
|
|
5
5
|
import type { PrettyStyleOptions } from "../pretty/abstract-pretty-reporter.d.ts";
|
|
6
6
|
import { AbstractPrettyReporter } from "../pretty/abstract-pretty-reporter.d.ts";
|
|
@@ -8,15 +8,11 @@ const {
|
|
|
8
8
|
stdout,
|
|
9
9
|
stderr
|
|
10
10
|
} = __cjs_getProcess;
|
|
11
|
-
import colorize, { red, greenBright, cyan, green, grey, bold,
|
|
12
|
-
import { d as defaultInspectorConfig, f as formatLabel, i as inspect, r as renderError } from '../../packem_shared/
|
|
13
|
-
import { t as terminalSize, g as getStringWidth, w as wordWrap, W as WrapMode } from '../../packem_shared/index-DqKWykfa.js';
|
|
14
|
-
import { E as EMPTY_SYMBOL } from '../../packem_shared/constants-omsTHUWB.js';
|
|
15
|
-
import { A as AbstractPrettyReporter, g as getLongestBadge } from '../../packem_shared/abstract-pretty-reporter-Di_sdm2r.js';
|
|
16
|
-
import { g as getLongestLabel } from '../../packem_shared/get-longest-label-C9PWeyKq.js';
|
|
17
|
-
import { w as writeStream } from '../../packem_shared/write-stream-BG8fhcs3.js';
|
|
11
|
+
import colorize, { red, greenBright, cyan, green, grey, bold, bgGrey, underline, white } from '@visulima/colorize';
|
|
12
|
+
import { A as AbstractPrettyReporter, d as defaultInspectorConfig, w as writeStream, t as terminalSize, b as getLongestBadge, g as getLongestLabel, c as getStringWidth, f as formatLabel, a as EMPTY_SYMBOL, i as inspect, e as wordWrap, r as renderError, W as WrapMode } from '../../packem_shared/abstract-pretty-reporter-CXAKYCb8.js';
|
|
18
13
|
|
|
19
|
-
const
|
|
14
|
+
const PAIL_DIST_REGEX = /[\\/]pail[\\/]dist/;
|
|
15
|
+
const pailFileFilter = (line) => !PAIL_DIST_REGEX.test(line);
|
|
20
16
|
class SimpleReporter extends AbstractPrettyReporter {
|
|
21
17
|
#stdout;
|
|
22
18
|
#stderr;
|
|
@@ -85,7 +81,7 @@ class SimpleReporter extends AbstractPrettyReporter {
|
|
|
85
81
|
const groupSpaces = groups.map(() => " ").join("");
|
|
86
82
|
const items = [];
|
|
87
83
|
if (groups.length > 0) {
|
|
88
|
-
items.push(`${groupSpaces + grey(`[${groups.at(-1)}]`)} `);
|
|
84
|
+
items.push(`${groupSpaces + grey(`[${groups.at(-1) ?? ""}]`)} `);
|
|
89
85
|
}
|
|
90
86
|
if (date) {
|
|
91
87
|
items.push(`${grey(this.styles.dateFormatter(typeof date === "string" ? new Date(date) : date))} `);
|
|
@@ -108,7 +104,7 @@ class SimpleReporter extends AbstractPrettyReporter {
|
|
|
108
104
|
items.push(" ".repeat(longestWidth + 1));
|
|
109
105
|
}
|
|
110
106
|
if (repeated) {
|
|
111
|
-
items.push(`${bgGrey.white(`[${repeated}x]`)} `);
|
|
107
|
+
items.push(`${bgGrey.white(`[${String(repeated)}x]`)} `);
|
|
112
108
|
}
|
|
113
109
|
if (Array.isArray(scope) && scope.length > 0) {
|
|
114
110
|
items.push(`${grey(`[${scope.join(" > ")}]`)} `);
|
|
@@ -144,7 +140,7 @@ ${renderError(value, {
|
|
|
144
140
|
if (typeof value === "object") {
|
|
145
141
|
return ` ${inspect(value, this.#inspectOptions)}`;
|
|
146
142
|
}
|
|
147
|
-
const newValue = (hasError ? "\n\n" : " ") + value;
|
|
143
|
+
const newValue = (hasError ? "\n\n" : " ") + String(value);
|
|
148
144
|
hasError = false;
|
|
149
145
|
return newValue;
|
|
150
146
|
})
|
|
@@ -178,7 +174,7 @@ ${renderError(traceError, {
|
|
|
178
174
|
items.push(` ${groupSpaces}${grey(this.styles.underline.suffix ? underline(suffix) : suffix)}`);
|
|
179
175
|
}
|
|
180
176
|
if (file) {
|
|
181
|
-
const fileMessage = file.name + (file.line ? `:${file.line}` : "");
|
|
177
|
+
const fileMessage = (file.name ?? "") + (file.line ? `:${String(file.line)}` : "");
|
|
182
178
|
const callerPad = Math.max(0, titleSize - getStringWidth("Caller: "));
|
|
183
179
|
items.push("\n", grey("Caller: "), " ".repeat(callerPad), fileMessage, "\n");
|
|
184
180
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { AnsiColors } from "@visulima/colorize";
|
|
2
|
+
import type { InteractiveManager } from "@visulima/interactive-manager";
|
|
2
3
|
import type { stringify } from "safe-stable-stringify";
|
|
3
4
|
import type { LiteralUnion, Primitive } from "type-fest";
|
|
4
|
-
import type InteractiveManager from "./interactive/interactive-manager.d.ts";
|
|
5
5
|
/**
|
|
6
6
|
* Global namespace for extending Pail's metadata interface.
|
|
7
7
|
*
|
|
@@ -43,7 +43,7 @@ export interface Meta<L> extends VisulimaPail.CustomMeta<L> {
|
|
|
43
43
|
label: string | undefined;
|
|
44
44
|
message: Primitive | ReadonlyArray<unknown> | Record<PropertyKey, unknown>;
|
|
45
45
|
prefix: string | undefined;
|
|
46
|
-
repeated?: number
|
|
46
|
+
repeated?: number;
|
|
47
47
|
scope: string[] | undefined;
|
|
48
48
|
suffix: string | undefined;
|
|
49
49
|
traceError: Error | undefined;
|
|
@@ -88,7 +88,7 @@ export interface LoggerFunction {
|
|
|
88
88
|
*/
|
|
89
89
|
export interface LoggerConfiguration<L extends string> {
|
|
90
90
|
badge?: string;
|
|
91
|
-
color?: AnsiColors
|
|
91
|
+
color?: AnsiColors;
|
|
92
92
|
label: string;
|
|
93
93
|
logLevel: LiteralUnion<ExtendedRfc5424LogLevels, L>;
|
|
94
94
|
}
|
|
@@ -217,7 +217,7 @@ export interface ServerConstructorOptions<T extends string, L extends string> ex
|
|
|
217
217
|
stdout: NodeJS.WriteStream;
|
|
218
218
|
}
|
|
219
219
|
export type Message = {
|
|
220
|
-
context?: any[]
|
|
220
|
+
context?: any[];
|
|
221
221
|
message: any;
|
|
222
222
|
prefix?: string;
|
|
223
223
|
suffix?: string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { LiteralUnion } from "type-fest";
|
|
2
2
|
import type { ExtendedRfc5424LogLevels } from "../types.d.ts";
|
|
3
|
-
declare const writeConsoleLogBasedOnLevel: <L extends string = string>(level: LiteralUnion<ExtendedRfc5424LogLevels, L>) => (...data: any[]) => void;
|
|
3
|
+
declare const writeConsoleLogBasedOnLevel: <L extends string = string>(level: LiteralUnion<ExtendedRfc5424LogLevels, L>) => ((...data: any[]) => void);
|
|
4
4
|
export default writeConsoleLogBasedOnLevel;
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import type { PailBrowserImpl } from "./pail.d.ts";
|
|
2
|
+
import type { DefaultLogTypes, LoggerFunction } from "./types.d.ts";
|
|
3
|
+
/**
|
|
4
|
+
* A pail instance with dynamically generated log methods.
|
|
5
|
+
* This is the minimal interface WideEvent needs from a pail logger.
|
|
6
|
+
*/
|
|
7
|
+
type PailLike<T extends string = string> = PailBrowserImpl<T> & Record<DefaultLogTypes | T, LoggerFunction>;
|
|
8
|
+
/**
|
|
9
|
+
* Makes all properties in T optional recursively.
|
|
10
|
+
*/
|
|
11
|
+
type DeepPartial<T> = {
|
|
12
|
+
[K in keyof T]?: T[K] extends Record<string, unknown> ? DeepPartial<T[K]> : T[K];
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Severity levels for wide events.
|
|
16
|
+
*/
|
|
17
|
+
export type WideEventLevel = "debug" | "error" | "info" | "warn";
|
|
18
|
+
/**
|
|
19
|
+
* An entry in the request lifecycle log.
|
|
20
|
+
*/
|
|
21
|
+
export interface RequestLogEntry {
|
|
22
|
+
/**
|
|
23
|
+
* Additional structured context for this log entry.
|
|
24
|
+
*/
|
|
25
|
+
context?: Record<string, unknown>;
|
|
26
|
+
/**
|
|
27
|
+
* Severity level of this entry.
|
|
28
|
+
*/
|
|
29
|
+
level: WideEventLevel;
|
|
30
|
+
/**
|
|
31
|
+
* Human-readable message describing what happened.
|
|
32
|
+
*/
|
|
33
|
+
message: string;
|
|
34
|
+
/**
|
|
35
|
+
* ISO 8601 timestamp of when this entry was recorded.
|
|
36
|
+
*/
|
|
37
|
+
timestamp: string;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Serialized error information included in the emitted wide event.
|
|
41
|
+
*/
|
|
42
|
+
export interface SerializedError {
|
|
43
|
+
cause?: SerializedError;
|
|
44
|
+
data?: unknown;
|
|
45
|
+
message: string;
|
|
46
|
+
name: string;
|
|
47
|
+
stack?: string;
|
|
48
|
+
status?: number;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Options for finishing a wide event with HTTP context.
|
|
52
|
+
*/
|
|
53
|
+
export interface WideEventFinishOptions {
|
|
54
|
+
/**
|
|
55
|
+
* An error that occurred during the operation.
|
|
56
|
+
* Will be serialized and included in the emitted event.
|
|
57
|
+
*/
|
|
58
|
+
error?: Error;
|
|
59
|
+
/**
|
|
60
|
+
* HTTP response status code.
|
|
61
|
+
*/
|
|
62
|
+
status?: number;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Options for creating a WideEvent instance.
|
|
66
|
+
* @template T - Custom logger type names
|
|
67
|
+
*/
|
|
68
|
+
export interface WideEventOptions<T extends string = string> {
|
|
69
|
+
/**
|
|
70
|
+
* Auto-emit the event when disposed via `Symbol.dispose`.
|
|
71
|
+
* Works with TC39 Explicit Resource Management (`using`).
|
|
72
|
+
* @default true
|
|
73
|
+
*/
|
|
74
|
+
autoEmit?: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Event name identifying this wide event, e.g. "api.checkout", "worker.send-email".
|
|
77
|
+
*/
|
|
78
|
+
name: string;
|
|
79
|
+
/**
|
|
80
|
+
* The pail logger instance to use for emission.
|
|
81
|
+
*/
|
|
82
|
+
pail: PailLike<T>;
|
|
83
|
+
/**
|
|
84
|
+
* Service name for this event. Overrides any service name from pail's scope.
|
|
85
|
+
*/
|
|
86
|
+
service?: string;
|
|
87
|
+
/**
|
|
88
|
+
* Base log type to use when emitting. Defaults to "info".
|
|
89
|
+
* The actual type may be escalated based on logged warnings/errors.
|
|
90
|
+
*/
|
|
91
|
+
type?: DefaultLogTypes | T;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* A wide event logger that accumulates context incrementally and emits
|
|
95
|
+
* a single comprehensive log event through pail.
|
|
96
|
+
*
|
|
97
|
+
* Instead of scattering multiple log calls throughout an operation,
|
|
98
|
+
* use `set()` to build up context as information becomes available,
|
|
99
|
+
* then emit once at the end. Lifecycle methods (`info()`, `warn()`, `error()`,
|
|
100
|
+
* `debug()`) record timestamped entries in a `requestLogs` array and
|
|
101
|
+
* automatically escalate the event's severity level.
|
|
102
|
+
*
|
|
103
|
+
* Implements `Disposable` for use with TC39 Explicit Resource Management.
|
|
104
|
+
* @template TData - Shape of the accumulated event data
|
|
105
|
+
* @template T - Custom logger type names from the pail instance
|
|
106
|
+
* @example
|
|
107
|
+
* ```typescript
|
|
108
|
+
* // Manual finish
|
|
109
|
+
* const ev = createWideEvent({ pail: logger, name: "api.checkout" });
|
|
110
|
+
* ev.set({ user: { id: 1 } });
|
|
111
|
+
* ev.info("Validated cart");
|
|
112
|
+
* ev.set({ cart: { items: 3, total: 9999 } });
|
|
113
|
+
* ev.finish({ status: 200 });
|
|
114
|
+
*
|
|
115
|
+
* // Auto-emit with Explicit Resource Management
|
|
116
|
+
* using ev = createWideEvent({ pail: logger, name: "api.checkout" });
|
|
117
|
+
* ev.set({ user: { id: 1 } });
|
|
118
|
+
* // emits automatically when scope exits
|
|
119
|
+
*
|
|
120
|
+
* // Typed data shape
|
|
121
|
+
* interface CheckoutData {
|
|
122
|
+
* user: { id: number; plan: string };
|
|
123
|
+
* cart: { items: number; total: number };
|
|
124
|
+
* }
|
|
125
|
+
* const ev = createWideEvent<CheckoutData>({ pail: logger, name: "api.checkout" });
|
|
126
|
+
* ev.set({ user: { id: 1, plan: "pro" } }); // fully typed
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
export declare class WideEvent<TData extends Record<string, unknown> = Record<string, unknown>, T extends string = string> implements Disposable {
|
|
130
|
+
[Symbol.dispose]: () => void;
|
|
131
|
+
readonly name: string;
|
|
132
|
+
private readonly autoEmit;
|
|
133
|
+
private data;
|
|
134
|
+
private emitted;
|
|
135
|
+
private attachedError?;
|
|
136
|
+
private level;
|
|
137
|
+
private readonly pail;
|
|
138
|
+
private readonly requestLogs;
|
|
139
|
+
private readonly service;
|
|
140
|
+
private readonly startTime;
|
|
141
|
+
private status;
|
|
142
|
+
private readonly timestamp;
|
|
143
|
+
private readonly _type;
|
|
144
|
+
constructor(options: WideEventOptions<T>);
|
|
145
|
+
/**
|
|
146
|
+
* Record a debug-level lifecycle log entry.
|
|
147
|
+
* Does not escalate the event level.
|
|
148
|
+
* @param message Description of what happened
|
|
149
|
+
* @param context Optional structured context
|
|
150
|
+
* @returns `this` for chaining
|
|
151
|
+
*/
|
|
152
|
+
debug(message: string, context?: Record<string, unknown>): this;
|
|
153
|
+
/**
|
|
154
|
+
* Emit the wide event through the pail logger. Can only be called once;
|
|
155
|
+
* subsequent calls are no-ops.
|
|
156
|
+
*
|
|
157
|
+
* Automatically calculates duration, determines the log type based on
|
|
158
|
+
* the highest severity level reached, and serializes any attached error.
|
|
159
|
+
*
|
|
160
|
+
* Prefer `finish()` for HTTP request contexts where you have a status code.
|
|
161
|
+
* @param typeOverride Override the log type for this emission
|
|
162
|
+
*/
|
|
163
|
+
emit(typeOverride?: DefaultLogTypes | T): void;
|
|
164
|
+
/**
|
|
165
|
+
* Record an error-level lifecycle log entry and attach the error.
|
|
166
|
+
* Escalates the event level to "error".
|
|
167
|
+
* @param message Description of what went wrong
|
|
168
|
+
* @param error The error that occurred
|
|
169
|
+
* @param context Optional structured context
|
|
170
|
+
* @returns `this` for chaining
|
|
171
|
+
*/
|
|
172
|
+
error(message: string, error?: Error, context?: Record<string, unknown>): this;
|
|
173
|
+
/**
|
|
174
|
+
* Finish and emit the wide event with HTTP context.
|
|
175
|
+
* Sets the response status and optional error before emitting.
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* ev.finish({ status: 200 });
|
|
179
|
+
* ev.finish({ status: 500, error: new Error("DB timeout") });
|
|
180
|
+
* ```
|
|
181
|
+
* @param options Status code and/or error
|
|
182
|
+
*/
|
|
183
|
+
finish(options?: WideEventFinishOptions): void;
|
|
184
|
+
/**
|
|
185
|
+
* Get a read-only snapshot of the accumulated data.
|
|
186
|
+
*/
|
|
187
|
+
getData(): Readonly<TData>;
|
|
188
|
+
/**
|
|
189
|
+
* Get the current severity level of the event.
|
|
190
|
+
* The level auto-escalates as `warn()` or `error()` entries are added.
|
|
191
|
+
*/
|
|
192
|
+
getLevel(): WideEventLevel;
|
|
193
|
+
/**
|
|
194
|
+
* Get a read-only copy of the request lifecycle log entries.
|
|
195
|
+
*/
|
|
196
|
+
getRequestLogs(): ReadonlyArray<RequestLogEntry>;
|
|
197
|
+
/**
|
|
198
|
+
* Record an info-level lifecycle log entry.
|
|
199
|
+
* Does not escalate the event level.
|
|
200
|
+
* @param message Description of what happened
|
|
201
|
+
* @param context Optional structured context
|
|
202
|
+
* @returns `this` for chaining
|
|
203
|
+
*/
|
|
204
|
+
info(message: string, context?: Record<string, unknown>): this;
|
|
205
|
+
/**
|
|
206
|
+
* Accumulate context into the wide event via deep merge.
|
|
207
|
+
* Call as many times as needed throughout the operation.
|
|
208
|
+
* @example
|
|
209
|
+
* ```typescript
|
|
210
|
+
* ev.set({ user: { id: 1 } });
|
|
211
|
+
* ev.set({ user: { plan: "pro" } });
|
|
212
|
+
* // data = { user: { id: 1, plan: "pro" } }
|
|
213
|
+
* ```
|
|
214
|
+
* @param data Partial data to merge into the event
|
|
215
|
+
* @returns `this` for chaining
|
|
216
|
+
*/
|
|
217
|
+
set(data: DeepPartial<TData>): this;
|
|
218
|
+
/**
|
|
219
|
+
* Attach an error to the event. Automatically escalates the event
|
|
220
|
+
* level to "error".
|
|
221
|
+
* @param error The error to attach
|
|
222
|
+
* @returns `this` for chaining
|
|
223
|
+
*/
|
|
224
|
+
setError(error: Error): this;
|
|
225
|
+
/**
|
|
226
|
+
* Set the HTTP response status code.
|
|
227
|
+
* @param status HTTP status code
|
|
228
|
+
* @returns `this` for chaining
|
|
229
|
+
*/
|
|
230
|
+
setStatus(status: number): this;
|
|
231
|
+
/**
|
|
232
|
+
* Record a warn-level lifecycle log entry.
|
|
233
|
+
* Escalates the event level to "warn" (unless already "error").
|
|
234
|
+
* @param message Description of the warning
|
|
235
|
+
* @param context Optional structured context
|
|
236
|
+
* @returns `this` for chaining
|
|
237
|
+
*/
|
|
238
|
+
warn(message: string, context?: Record<string, unknown>): this;
|
|
239
|
+
/**
|
|
240
|
+
* Disposable implementation. Auto-emits the event if `autoEmit` is true
|
|
241
|
+
* and the event hasn't been manually emitted yet.
|
|
242
|
+
*
|
|
243
|
+
* Enables usage with TC39 Explicit Resource Management:
|
|
244
|
+
* ```typescript
|
|
245
|
+
* using ev = createWideEvent({ pail: logger, name: "api.checkout" });
|
|
246
|
+
* ev.set({ user: { id: 1 } });
|
|
247
|
+
* // auto-emits here when scope exits
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
[Symbol.dispose](): void;
|
|
251
|
+
/**
|
|
252
|
+
* Add an entry to the request lifecycle log and escalate level if needed.
|
|
253
|
+
*/
|
|
254
|
+
private addLogEntry;
|
|
255
|
+
/**
|
|
256
|
+
* Escalate the event level if the new level has higher severity.
|
|
257
|
+
*/
|
|
258
|
+
private escalateLevel;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Create a wide event logger that accumulates context and emits a single
|
|
262
|
+
* comprehensive log event through pail.
|
|
263
|
+
* @template TData - Shape of the accumulated event data
|
|
264
|
+
* @template T - Custom logger type names from the pail instance
|
|
265
|
+
* @param options Configuration options
|
|
266
|
+
* @returns A new WideEvent instance
|
|
267
|
+
* @example
|
|
268
|
+
* ```typescript
|
|
269
|
+
* import { createPail } from "@visulima/pail";
|
|
270
|
+
* import { createWideEvent } from "@visulima/pail/wide-event";
|
|
271
|
+
*
|
|
272
|
+
* const logger = createPail();
|
|
273
|
+
*
|
|
274
|
+
* // In a request handler:
|
|
275
|
+
* const ev = createWideEvent({ pail: logger, name: "api.checkout" });
|
|
276
|
+
*
|
|
277
|
+
* ev.set({ user: { id: 1, plan: "pro" } });
|
|
278
|
+
* ev.info("Validated cart", { itemCount: 3 });
|
|
279
|
+
* ev.set({ cart: { id: 42, items: 3, total: 9999 } });
|
|
280
|
+
* ev.info("Payment processed");
|
|
281
|
+
* ev.finish({ status: 200 });
|
|
282
|
+
*
|
|
283
|
+
* // Emits a single structured log:
|
|
284
|
+
* // {
|
|
285
|
+
* // event: "api.checkout",
|
|
286
|
+
* // timestamp: "2025-01-24T10:23:45.612Z",
|
|
287
|
+
* // duration: "127ms",
|
|
288
|
+
* // duration_ms: 127,
|
|
289
|
+
* // status: 200,
|
|
290
|
+
* // user: { id: 1, plan: "pro" },
|
|
291
|
+
* // cart: { id: 42, items: 3, total: 9999 },
|
|
292
|
+
* // requestLogs: [
|
|
293
|
+
* // { level: "info", message: "Validated cart", timestamp: "...", context: { itemCount: 3 } },
|
|
294
|
+
* // { level: "info", message: "Payment processed", timestamp: "..." }
|
|
295
|
+
* // ]
|
|
296
|
+
* // }
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
export declare const createWideEvent: <TData extends Record<string, unknown> = Record<string, unknown>, T extends string = string>(options: WideEventOptions<T>) => WideEvent<TData, T>;
|
|
300
|
+
export {};
|