@visulima/pail 4.0.0-alpha.6 → 4.0.0-alpha.7
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 +13 -0
- package/LICENSE.md +2 -2
- package/README.md +323 -0
- package/dist/error.d.ts +104 -0
- package/dist/error.js +76 -0
- package/dist/index.browser.d.ts +2 -0
- package/dist/index.browser.js +2 -1
- package/dist/index.server.d.ts +2 -0
- package/dist/index.server.js +3 -1
- 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/packem_shared/{AbstractJsonReporter-DWRpTtGw.js → AbstractJsonReporter-CGKHS8_M.js} +103 -21
- package/dist/packem_shared/{AbstractJsonReporter-BaZ33PlE.js → AbstractJsonReporter-DDjDkciI.js} +103 -21
- package/dist/packem_shared/{JsonReporter-BV5lMnJX.js → JsonReporter-B3XX8GHN.js} +1 -1
- package/dist/packem_shared/{JsonReporter-BRw4skd5.js → JsonReporter-p_BXg6Sj.js} +1 -1
- package/dist/packem_shared/{PrettyReporter-BjXCFQlo.js → PrettyReporter-CvBn-hxP.js} +2 -1
- package/dist/packem_shared/createPailError-B11aRfrT.js +76 -0
- package/dist/packem_shared/headers-Cp4uLtr4.js +123 -0
- package/dist/packem_shared/pailMiddleware-Ci88geIF.js +24 -0
- package/dist/packem_shared/storage-D0vqz8OX.js +36 -0
- package/dist/packem_shared/useLogger-D0rU3lcX.js +33 -0
- package/dist/processor/environment-processor.d.ts +124 -0
- package/dist/processor/environment-processor.js +78 -0
- package/dist/processor/message-formatter-processor.d.ts +1 -2
- 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 +1 -1
- package/dist/reporter/http/http-reporter.edge-light.js +103 -21
- package/dist/reporter/json/index.browser.js +2 -2
- package/dist/reporter/json/index.js +2 -2
- package/dist/reporter/pretty/index.js +1 -1
- package/dist/reporter/simple/simple-reporter.server.js +2 -1
- package/dist/wide-event.d.ts +300 -0
- package/dist/wide-event.js +281 -0
- package/package.json +65 -1
|
@@ -20,7 +20,7 @@ const __cjs_getBuiltinModule = (module) => {
|
|
|
20
20
|
const {
|
|
21
21
|
Buffer
|
|
22
22
|
} = __cjs_getBuiltinModule("node:buffer");
|
|
23
|
-
import { AbstractJsonReporter } from '../../packem_shared/AbstractJsonReporter-
|
|
23
|
+
import { AbstractJsonReporter } from '../../packem_shared/AbstractJsonReporter-CGKHS8_M.js';
|
|
24
24
|
const {
|
|
25
25
|
gzipSync
|
|
26
26
|
} = __cjs_getBuiltinModule("node:zlib");
|
|
@@ -36,7 +36,7 @@ const ErrorProto = Object.create(
|
|
|
36
36
|
{},
|
|
37
37
|
{
|
|
38
38
|
cause: {
|
|
39
|
-
enumerable:
|
|
39
|
+
enumerable: false,
|
|
40
40
|
value: void 0,
|
|
41
41
|
writable: true
|
|
42
42
|
},
|
|
@@ -46,32 +46,55 @@ const ErrorProto = Object.create(
|
|
|
46
46
|
writable: true
|
|
47
47
|
},
|
|
48
48
|
errors: {
|
|
49
|
-
enumerable:
|
|
49
|
+
enumerable: false,
|
|
50
50
|
value: void 0,
|
|
51
51
|
writable: true
|
|
52
52
|
},
|
|
53
53
|
message: {
|
|
54
|
-
enumerable:
|
|
54
|
+
enumerable: false,
|
|
55
55
|
value: void 0,
|
|
56
56
|
writable: true
|
|
57
57
|
},
|
|
58
58
|
name: {
|
|
59
|
-
enumerable:
|
|
59
|
+
enumerable: false,
|
|
60
60
|
value: void 0,
|
|
61
61
|
writable: true
|
|
62
62
|
},
|
|
63
63
|
stack: {
|
|
64
|
-
enumerable:
|
|
64
|
+
enumerable: false,
|
|
65
65
|
value: void 0,
|
|
66
66
|
writable: true
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
);
|
|
70
70
|
const toJsonWasCalled = /* @__PURE__ */ new WeakSet();
|
|
71
|
+
const makePropertiesEnumerable = (object) => {
|
|
72
|
+
if (!object || typeof object !== "object") {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const props = Object.getOwnPropertyNames(object);
|
|
76
|
+
for (const prop of props) {
|
|
77
|
+
const descriptor = Object.getOwnPropertyDescriptor(object, prop);
|
|
78
|
+
if (descriptor) {
|
|
79
|
+
if (!descriptor.enumerable) {
|
|
80
|
+
Object.defineProperty(object, prop, {
|
|
81
|
+
...descriptor,
|
|
82
|
+
enumerable: true
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
if (descriptor.value && typeof descriptor.value === "object" && !Array.isArray(descriptor.value) && (Object.getPrototypeOf(descriptor.value) === Object.prototype || Object.getPrototypeOf(descriptor.value) === null)) {
|
|
86
|
+
makePropertiesEnumerable(descriptor.value);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
71
91
|
const toJSON = (from) => {
|
|
72
92
|
toJsonWasCalled.add(from);
|
|
73
93
|
const json = from.toJSON();
|
|
74
94
|
toJsonWasCalled.delete(from);
|
|
95
|
+
if (json && typeof json === "object" && Object.isExtensible(json)) {
|
|
96
|
+
makePropertiesEnumerable(json);
|
|
97
|
+
}
|
|
75
98
|
return json;
|
|
76
99
|
};
|
|
77
100
|
const serializeValue = (value, seen, depth, options) => {
|
|
@@ -82,7 +105,7 @@ const serializeValue = (value, seen, depth, options) => {
|
|
|
82
105
|
return "[object Stream]";
|
|
83
106
|
}
|
|
84
107
|
if (value instanceof Error) {
|
|
85
|
-
if (seen.
|
|
108
|
+
if (seen.has(value)) {
|
|
86
109
|
return "[Circular]";
|
|
87
110
|
}
|
|
88
111
|
depth += 1;
|
|
@@ -97,11 +120,14 @@ const serializeValue = (value, seen, depth, options) => {
|
|
|
97
120
|
if (typeof value === "function") {
|
|
98
121
|
return `[Function: ${value.name || "anonymous"}]`;
|
|
99
122
|
}
|
|
123
|
+
if (typeof value === "bigint") {
|
|
124
|
+
return `${value}n`;
|
|
125
|
+
}
|
|
100
126
|
if (isPlainObject(value)) {
|
|
101
|
-
depth
|
|
102
|
-
if (options.maxDepth && depth >= options.maxDepth) {
|
|
127
|
+
if (options.maxDepth !== void 0 && options.maxDepth !== Number.POSITIVE_INFINITY && depth + 1 >= options.maxDepth) {
|
|
103
128
|
return {};
|
|
104
129
|
}
|
|
130
|
+
depth += 1;
|
|
105
131
|
const plainObject = {};
|
|
106
132
|
for (const key in value) {
|
|
107
133
|
plainObject[key] = serializeValue(value[key], seen, depth, options);
|
|
@@ -115,7 +141,7 @@ const serializeValue = (value, seen, depth, options) => {
|
|
|
115
141
|
}
|
|
116
142
|
};
|
|
117
143
|
const _serialize = (error, options, seen, depth) => {
|
|
118
|
-
seen.
|
|
144
|
+
seen.add(error);
|
|
119
145
|
if (options.maxDepth === 0) {
|
|
120
146
|
return {};
|
|
121
147
|
}
|
|
@@ -123,31 +149,87 @@ const _serialize = (error, options, seen, depth) => {
|
|
|
123
149
|
return toJSON(error);
|
|
124
150
|
}
|
|
125
151
|
const protoError = Object.create(ErrorProto);
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
152
|
+
Object.defineProperty(protoError, "name", {
|
|
153
|
+
configurable: true,
|
|
154
|
+
enumerable: true,
|
|
155
|
+
value: Object.prototype.toString.call(error.constructor) === "[object Function]" ? error.constructor.name : error.name,
|
|
156
|
+
writable: true
|
|
157
|
+
});
|
|
158
|
+
Object.defineProperty(protoError, "message", {
|
|
159
|
+
configurable: true,
|
|
160
|
+
enumerable: true,
|
|
161
|
+
value: error.message,
|
|
162
|
+
writable: true
|
|
163
|
+
});
|
|
164
|
+
Object.defineProperty(protoError, "stack", {
|
|
165
|
+
configurable: true,
|
|
166
|
+
enumerable: true,
|
|
167
|
+
value: error.stack,
|
|
168
|
+
writable: true
|
|
169
|
+
});
|
|
129
170
|
if (Array.isArray(error.errors)) {
|
|
130
171
|
const aggregateErrors = [];
|
|
131
172
|
for (const aggregateError of error.errors) {
|
|
132
173
|
if (!(aggregateError instanceof Error)) {
|
|
133
174
|
throw new TypeError("All errors in the 'errors' property must be instances of Error");
|
|
134
175
|
}
|
|
135
|
-
if (seen.
|
|
136
|
-
protoError
|
|
176
|
+
if (seen.has(aggregateError)) {
|
|
177
|
+
Object.defineProperty(protoError, "errors", {
|
|
178
|
+
configurable: true,
|
|
179
|
+
enumerable: true,
|
|
180
|
+
value: [],
|
|
181
|
+
writable: true
|
|
182
|
+
});
|
|
137
183
|
return protoError;
|
|
138
184
|
}
|
|
139
185
|
aggregateErrors.push(_serialize(aggregateError, options, seen, depth));
|
|
140
186
|
}
|
|
141
|
-
protoError
|
|
187
|
+
Object.defineProperty(protoError, "errors", {
|
|
188
|
+
configurable: true,
|
|
189
|
+
enumerable: true,
|
|
190
|
+
value: aggregateErrors,
|
|
191
|
+
writable: true
|
|
192
|
+
});
|
|
142
193
|
}
|
|
143
|
-
if (error.cause
|
|
144
|
-
|
|
194
|
+
if (error.cause !== void 0 && error.cause !== null) {
|
|
195
|
+
if (error.cause instanceof Error) {
|
|
196
|
+
if (seen.has(error.cause)) {
|
|
197
|
+
Object.defineProperty(protoError, "cause", {
|
|
198
|
+
configurable: true,
|
|
199
|
+
enumerable: true,
|
|
200
|
+
value: "[Circular]",
|
|
201
|
+
writable: true
|
|
202
|
+
});
|
|
203
|
+
} else {
|
|
204
|
+
Object.defineProperty(protoError, "cause", {
|
|
205
|
+
configurable: true,
|
|
206
|
+
enumerable: true,
|
|
207
|
+
value: _serialize(error.cause, options, seen, depth),
|
|
208
|
+
writable: true
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
} else {
|
|
212
|
+
const serializedCause = serializeValue(error.cause, seen, depth, options);
|
|
213
|
+
Object.defineProperty(protoError, "cause", {
|
|
214
|
+
configurable: true,
|
|
215
|
+
enumerable: true,
|
|
216
|
+
value: serializedCause,
|
|
217
|
+
writable: true
|
|
218
|
+
});
|
|
219
|
+
}
|
|
145
220
|
}
|
|
146
221
|
for (const key in error) {
|
|
147
|
-
if (
|
|
148
|
-
|
|
149
|
-
protoError[key] = serializeValue(value, seen, depth, options);
|
|
222
|
+
if (key === "name" || key === "message" || key === "stack" || key === "cause" || key === "errors") {
|
|
223
|
+
continue;
|
|
150
224
|
}
|
|
225
|
+
const value = error[key];
|
|
226
|
+
const serializedValue = serializeValue(value, seen, depth, options);
|
|
227
|
+
Object.defineProperty(protoError, key, {
|
|
228
|
+
configurable: true,
|
|
229
|
+
enumerable: true,
|
|
230
|
+
value: serializedValue,
|
|
231
|
+
writable: true
|
|
232
|
+
});
|
|
151
233
|
}
|
|
152
234
|
if (Array.isArray(options.exclude) && options.exclude.length > 0) {
|
|
153
235
|
for (const key of options.exclude) {
|
|
@@ -166,7 +248,7 @@ const serialize = (error, options = {}) => _serialize(
|
|
|
166
248
|
maxDepth: options.maxDepth ?? Number.POSITIVE_INFINITY,
|
|
167
249
|
useToJSON: options.useToJSON ?? false
|
|
168
250
|
},
|
|
169
|
-
|
|
251
|
+
/* @__PURE__ */ new Set(),
|
|
170
252
|
0
|
|
171
253
|
);
|
|
172
254
|
|
|
@@ -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-DDjDkciI.js';
|
|
2
|
+
export { default as JsonReporter } from '../../packem_shared/JsonReporter-p_BXg6Sj.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-CGKHS8_M.js';
|
|
2
|
+
export { default as JsonReporter } from '../../packem_shared/JsonReporter-B3XX8GHN.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { PrettyReporter } from '../../packem_shared/PrettyReporter-
|
|
1
|
+
export { PrettyReporter } from '../../packem_shared/PrettyReporter-CvBn-hxP.js';
|
|
@@ -11,7 +11,8 @@ const {
|
|
|
11
11
|
import colorize, { red, greenBright, cyan, green, grey, bold, bgGrey, underline, white } from '@visulima/colorize';
|
|
12
12
|
import { A as AbstractPrettyReporter, d as defaultInspectorConfig, b as writeStream, t as terminalSize, c as getLongestBadge, g as getLongestLabel, e as getStringWidth, f as formatLabel, a as EMPTY_SYMBOL, i as inspect, w as wordWrap, r as renderError, W as WrapMode } from '../../packem_shared/abstract-pretty-reporter-jU8WL_6c.js';
|
|
13
13
|
|
|
14
|
-
const
|
|
14
|
+
const PAIL_DIST_REGEX = /[\\/]pail[\\/]dist/;
|
|
15
|
+
const pailFileFilter = (line) => !PAIL_DIST_REGEX.test(line);
|
|
15
16
|
class SimpleReporter extends AbstractPrettyReporter {
|
|
16
17
|
#stdout;
|
|
17
18
|
#stderr;
|
|
@@ -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 {};
|