@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
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
const LEVEL_PRIORITY = {
|
|
2
|
+
debug: 0,
|
|
3
|
+
error: 3,
|
|
4
|
+
info: 1,
|
|
5
|
+
warn: 2
|
|
6
|
+
};
|
|
7
|
+
const LEVEL_TO_LOG_TYPE = {
|
|
8
|
+
debug: "debug",
|
|
9
|
+
error: "error",
|
|
10
|
+
info: "info",
|
|
11
|
+
warn: "warn"
|
|
12
|
+
};
|
|
13
|
+
const deepMerge = (target, source) => {
|
|
14
|
+
const result = { ...target };
|
|
15
|
+
for (const key of Object.keys(source)) {
|
|
16
|
+
const sourceValue = source[key];
|
|
17
|
+
const targetValue = result[key];
|
|
18
|
+
if (sourceValue !== null && typeof sourceValue === "object" && !Array.isArray(sourceValue) && targetValue !== null && typeof targetValue === "object" && !Array.isArray(targetValue)) {
|
|
19
|
+
result[key] = deepMerge(targetValue, sourceValue);
|
|
20
|
+
} else {
|
|
21
|
+
result[key] = sourceValue;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
26
|
+
const formatDuration = (ms) => {
|
|
27
|
+
if (ms < 1e3) {
|
|
28
|
+
return `${ms}ms`;
|
|
29
|
+
}
|
|
30
|
+
return `${(ms / 1e3).toFixed(2)}s`;
|
|
31
|
+
};
|
|
32
|
+
const serializeError = (error) => {
|
|
33
|
+
const serialized = {
|
|
34
|
+
message: error.message,
|
|
35
|
+
name: error.name
|
|
36
|
+
};
|
|
37
|
+
if (error.stack) {
|
|
38
|
+
serialized.stack = error.stack;
|
|
39
|
+
}
|
|
40
|
+
const errorWithStatus = error;
|
|
41
|
+
if (errorWithStatus.status !== void 0) {
|
|
42
|
+
serialized.status = errorWithStatus.status;
|
|
43
|
+
} else if (errorWithStatus.statusCode !== void 0) {
|
|
44
|
+
serialized.status = errorWithStatus.statusCode;
|
|
45
|
+
}
|
|
46
|
+
if (errorWithStatus.data !== void 0) {
|
|
47
|
+
serialized.data = errorWithStatus.data;
|
|
48
|
+
}
|
|
49
|
+
if (error.cause instanceof Error) {
|
|
50
|
+
serialized.cause = serializeError(error.cause);
|
|
51
|
+
}
|
|
52
|
+
return serialized;
|
|
53
|
+
};
|
|
54
|
+
class WideEvent {
|
|
55
|
+
name;
|
|
56
|
+
autoEmit;
|
|
57
|
+
data;
|
|
58
|
+
emitted;
|
|
59
|
+
attachedError;
|
|
60
|
+
level;
|
|
61
|
+
pail;
|
|
62
|
+
requestLogs;
|
|
63
|
+
service;
|
|
64
|
+
startTime;
|
|
65
|
+
status;
|
|
66
|
+
timestamp;
|
|
67
|
+
type;
|
|
68
|
+
constructor(options) {
|
|
69
|
+
this.name = options.name;
|
|
70
|
+
this.pail = options.pail;
|
|
71
|
+
this.data = {};
|
|
72
|
+
this.startTime = performance.now();
|
|
73
|
+
this.timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
74
|
+
this.emitted = false;
|
|
75
|
+
this.autoEmit = options.autoEmit ?? true;
|
|
76
|
+
this.type = options.type ?? "info";
|
|
77
|
+
this.level = "info";
|
|
78
|
+
this.requestLogs = [];
|
|
79
|
+
this.service = options.service;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Record a debug-level lifecycle log entry.
|
|
83
|
+
* Does not escalate the event level.
|
|
84
|
+
* @param message Description of what happened
|
|
85
|
+
* @param context Optional structured context
|
|
86
|
+
* @returns `this` for chaining
|
|
87
|
+
*/
|
|
88
|
+
debug(message, context) {
|
|
89
|
+
return this.addLogEntry("debug", message, context);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Emit the wide event through the pail logger. Can only be called once;
|
|
93
|
+
* subsequent calls are no-ops.
|
|
94
|
+
*
|
|
95
|
+
* Automatically calculates duration, determines the log type based on
|
|
96
|
+
* the highest severity level reached, and serializes any attached error.
|
|
97
|
+
*
|
|
98
|
+
* Prefer `finish()` for HTTP request contexts where you have a status code.
|
|
99
|
+
* @param typeOverride Override the log type for this emission
|
|
100
|
+
*/
|
|
101
|
+
emit(typeOverride) {
|
|
102
|
+
if (this.emitted) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
this.emitted = true;
|
|
106
|
+
const durationMs = Math.round(performance.now() - this.startTime);
|
|
107
|
+
const resolvedLevel = this.attachedError ? "error" : this.level;
|
|
108
|
+
const type = typeOverride ?? LEVEL_TO_LOG_TYPE[resolvedLevel] ?? this.type;
|
|
109
|
+
const payload = {
|
|
110
|
+
duration: formatDuration(durationMs),
|
|
111
|
+
duration_ms: durationMs,
|
|
112
|
+
event: this.name,
|
|
113
|
+
timestamp: this.timestamp,
|
|
114
|
+
...this.data
|
|
115
|
+
};
|
|
116
|
+
if (this.service) {
|
|
117
|
+
payload.service = this.service;
|
|
118
|
+
}
|
|
119
|
+
if (this.status !== void 0) {
|
|
120
|
+
payload.status = this.status;
|
|
121
|
+
}
|
|
122
|
+
if (this.attachedError) {
|
|
123
|
+
payload.error = serializeError(this.attachedError);
|
|
124
|
+
}
|
|
125
|
+
if (this.requestLogs.length > 0) {
|
|
126
|
+
payload.requestLogs = this.requestLogs;
|
|
127
|
+
}
|
|
128
|
+
const logFunction = this.pail[type];
|
|
129
|
+
logFunction({ message: payload });
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Record an error-level lifecycle log entry and attach the error.
|
|
133
|
+
* Escalates the event level to "error".
|
|
134
|
+
* @param message Description of what went wrong
|
|
135
|
+
* @param error The error that occurred
|
|
136
|
+
* @param context Optional structured context
|
|
137
|
+
* @returns `this` for chaining
|
|
138
|
+
*/
|
|
139
|
+
error(message, error, context) {
|
|
140
|
+
if (error) {
|
|
141
|
+
this.attachedError = error;
|
|
142
|
+
}
|
|
143
|
+
return this.addLogEntry("error", message, context);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Finish and emit the wide event with HTTP context.
|
|
147
|
+
* Sets the response status and optional error before emitting.
|
|
148
|
+
* @example
|
|
149
|
+
* ```typescript
|
|
150
|
+
* ev.finish({ status: 200 });
|
|
151
|
+
* ev.finish({ status: 500, error: new Error("DB timeout") });
|
|
152
|
+
* ```
|
|
153
|
+
* @param options Status code and/or error
|
|
154
|
+
*/
|
|
155
|
+
finish(options) {
|
|
156
|
+
if (options?.status !== void 0) {
|
|
157
|
+
this.status = options.status;
|
|
158
|
+
}
|
|
159
|
+
if (options?.error) {
|
|
160
|
+
this.attachedError = options.error;
|
|
161
|
+
}
|
|
162
|
+
this.emit();
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Get a read-only snapshot of the accumulated data.
|
|
166
|
+
*/
|
|
167
|
+
getData() {
|
|
168
|
+
return this.data;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Get the current severity level of the event.
|
|
172
|
+
* The level auto-escalates as `warn()` or `error()` entries are added.
|
|
173
|
+
*/
|
|
174
|
+
getLevel() {
|
|
175
|
+
return this.attachedError ? "error" : this.level;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Get a read-only copy of the request lifecycle log entries.
|
|
179
|
+
*/
|
|
180
|
+
getRequestLogs() {
|
|
181
|
+
return this.requestLogs;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Record an info-level lifecycle log entry.
|
|
185
|
+
* Does not escalate the event level.
|
|
186
|
+
* @param message Description of what happened
|
|
187
|
+
* @param context Optional structured context
|
|
188
|
+
* @returns `this` for chaining
|
|
189
|
+
*/
|
|
190
|
+
info(message, context) {
|
|
191
|
+
return this.addLogEntry("info", message, context);
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Accumulate context into the wide event via deep merge.
|
|
195
|
+
* Call as many times as needed throughout the operation.
|
|
196
|
+
* @example
|
|
197
|
+
* ```typescript
|
|
198
|
+
* ev.set({ user: { id: 1 } });
|
|
199
|
+
* ev.set({ user: { plan: "pro" } });
|
|
200
|
+
* // data = { user: { id: 1, plan: "pro" } }
|
|
201
|
+
* ```
|
|
202
|
+
* @param data Partial data to merge into the event
|
|
203
|
+
* @returns `this` for chaining
|
|
204
|
+
*/
|
|
205
|
+
set(data) {
|
|
206
|
+
this.data = deepMerge(this.data, data);
|
|
207
|
+
return this;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Attach an error to the event. Automatically escalates the event
|
|
211
|
+
* level to "error".
|
|
212
|
+
* @param error The error to attach
|
|
213
|
+
* @returns `this` for chaining
|
|
214
|
+
*/
|
|
215
|
+
setError(error) {
|
|
216
|
+
this.attachedError = error;
|
|
217
|
+
return this;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Set the HTTP response status code.
|
|
221
|
+
* @param status HTTP status code
|
|
222
|
+
* @returns `this` for chaining
|
|
223
|
+
*/
|
|
224
|
+
setStatus(status) {
|
|
225
|
+
this.status = status;
|
|
226
|
+
return this;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Record a warn-level lifecycle log entry.
|
|
230
|
+
* Escalates the event level to "warn" (unless already "error").
|
|
231
|
+
* @param message Description of the warning
|
|
232
|
+
* @param context Optional structured context
|
|
233
|
+
* @returns `this` for chaining
|
|
234
|
+
*/
|
|
235
|
+
warn(message, context) {
|
|
236
|
+
return this.addLogEntry("warn", message, context);
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Disposable implementation. Auto-emits the event if `autoEmit` is true
|
|
240
|
+
* and the event hasn't been manually emitted yet.
|
|
241
|
+
*
|
|
242
|
+
* Enables usage with TC39 Explicit Resource Management:
|
|
243
|
+
* ```typescript
|
|
244
|
+
* using ev = createWideEvent({ pail: logger, name: "api.checkout" });
|
|
245
|
+
* ev.set({ user: { id: 1 } });
|
|
246
|
+
* // auto-emits here when scope exits
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
[Symbol.dispose]() {
|
|
250
|
+
if (this.autoEmit && !this.emitted) {
|
|
251
|
+
this.emit();
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Add an entry to the request lifecycle log and escalate level if needed.
|
|
256
|
+
*/
|
|
257
|
+
addLogEntry(level, message, context) {
|
|
258
|
+
const entry = {
|
|
259
|
+
level,
|
|
260
|
+
message,
|
|
261
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
262
|
+
};
|
|
263
|
+
if (context) {
|
|
264
|
+
entry.context = context;
|
|
265
|
+
}
|
|
266
|
+
this.requestLogs.push(entry);
|
|
267
|
+
this.escalateLevel(level);
|
|
268
|
+
return this;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Escalate the event level if the new level has higher severity.
|
|
272
|
+
*/
|
|
273
|
+
escalateLevel(level) {
|
|
274
|
+
if ((LEVEL_PRIORITY[level] ?? 0) > (LEVEL_PRIORITY[this.level] ?? 0)) {
|
|
275
|
+
this.level = level;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
const createWideEvent = (options) => new WideEvent(options);
|
|
280
|
+
|
|
281
|
+
export { WideEvent, createWideEvent };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@visulima/pail",
|
|
3
|
-
"version": "4.0.0-alpha.
|
|
3
|
+
"version": "4.0.0-alpha.7",
|
|
4
4
|
"description": "Highly configurable Logger for Node.js, Edge and Browser.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ansi",
|
|
@@ -114,6 +114,18 @@
|
|
|
114
114
|
"types": "./dist/processor/opentelemetry-processor.d.ts",
|
|
115
115
|
"default": "./dist/processor/opentelemetry-processor.js"
|
|
116
116
|
},
|
|
117
|
+
"./processor/sampling": {
|
|
118
|
+
"types": "./dist/processor/sampling-processor.d.ts",
|
|
119
|
+
"default": "./dist/processor/sampling-processor.js"
|
|
120
|
+
},
|
|
121
|
+
"./processor/environment": {
|
|
122
|
+
"types": "./dist/processor/environment-processor.d.ts",
|
|
123
|
+
"default": "./dist/processor/environment-processor.js"
|
|
124
|
+
},
|
|
125
|
+
"./error": {
|
|
126
|
+
"types": "./dist/error.d.ts",
|
|
127
|
+
"default": "./dist/error.js"
|
|
128
|
+
},
|
|
117
129
|
"./reporter/file": {
|
|
118
130
|
"types": "./dist/reporter/file/json-file-reporter.d.ts",
|
|
119
131
|
"default": "./dist/reporter/file/json-file-reporter.js"
|
|
@@ -194,6 +206,34 @@
|
|
|
194
206
|
"types": "./dist/object-tree.d.ts",
|
|
195
207
|
"default": "./dist/object-tree.js"
|
|
196
208
|
},
|
|
209
|
+
"./wide-event": {
|
|
210
|
+
"types": "./dist/wide-event.d.ts",
|
|
211
|
+
"default": "./dist/wide-event.js"
|
|
212
|
+
},
|
|
213
|
+
"./middleware/express": {
|
|
214
|
+
"types": "./dist/middleware/express.d.ts",
|
|
215
|
+
"default": "./dist/middleware/express.js"
|
|
216
|
+
},
|
|
217
|
+
"./middleware/fastify": {
|
|
218
|
+
"types": "./dist/middleware/fastify.d.ts",
|
|
219
|
+
"default": "./dist/middleware/fastify.js"
|
|
220
|
+
},
|
|
221
|
+
"./middleware/hono": {
|
|
222
|
+
"types": "./dist/middleware/hono.d.ts",
|
|
223
|
+
"default": "./dist/middleware/hono.js"
|
|
224
|
+
},
|
|
225
|
+
"./middleware/elysia": {
|
|
226
|
+
"types": "./dist/middleware/elysia.d.ts",
|
|
227
|
+
"default": "./dist/middleware/elysia.js"
|
|
228
|
+
},
|
|
229
|
+
"./middleware/sveltekit": {
|
|
230
|
+
"types": "./dist/middleware/sveltekit.d.ts",
|
|
231
|
+
"default": "./dist/middleware/sveltekit.js"
|
|
232
|
+
},
|
|
233
|
+
"./middleware/next": {
|
|
234
|
+
"types": "./dist/middleware/next/handler.d.ts",
|
|
235
|
+
"default": "./dist/middleware/next/handler.js"
|
|
236
|
+
},
|
|
197
237
|
"./package.json": "./package.json"
|
|
198
238
|
},
|
|
199
239
|
"files": [
|
|
@@ -208,16 +248,40 @@
|
|
|
208
248
|
},
|
|
209
249
|
"peerDependencies": {
|
|
210
250
|
"@opentelemetry/api": "^1.9",
|
|
251
|
+
"@sveltejs/kit": ">=2.0",
|
|
211
252
|
"@visulima/redact": "3.0.0-alpha.5",
|
|
253
|
+
"elysia": ">=1.0",
|
|
254
|
+
"express": ">=4.0",
|
|
255
|
+
"fastify": ">=4.0",
|
|
256
|
+
"hono": ">=4.0",
|
|
257
|
+
"next": ">=14.0",
|
|
212
258
|
"rotating-file-stream": "^3.2.7"
|
|
213
259
|
},
|
|
214
260
|
"peerDependenciesMeta": {
|
|
215
261
|
"@opentelemetry/api": {
|
|
216
262
|
"optional": true
|
|
217
263
|
},
|
|
264
|
+
"@sveltejs/kit": {
|
|
265
|
+
"optional": true
|
|
266
|
+
},
|
|
218
267
|
"@visulima/redact": {
|
|
219
268
|
"optional": true
|
|
220
269
|
},
|
|
270
|
+
"elysia": {
|
|
271
|
+
"optional": true
|
|
272
|
+
},
|
|
273
|
+
"express": {
|
|
274
|
+
"optional": true
|
|
275
|
+
},
|
|
276
|
+
"fastify": {
|
|
277
|
+
"optional": true
|
|
278
|
+
},
|
|
279
|
+
"hono": {
|
|
280
|
+
"optional": true
|
|
281
|
+
},
|
|
282
|
+
"next": {
|
|
283
|
+
"optional": true
|
|
284
|
+
},
|
|
221
285
|
"rotating-file-stream": {
|
|
222
286
|
"optional": true
|
|
223
287
|
}
|