@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
|
@@ -1252,7 +1252,8 @@ class AbstractPrettyReporter {
|
|
|
1252
1252
|
}
|
|
1253
1253
|
}
|
|
1254
1254
|
|
|
1255
|
-
const
|
|
1255
|
+
const PAIL_DIST_REGEX = /[\\/]pail[\\/]dist/;
|
|
1256
|
+
const pailFileFilter = (line) => !PAIL_DIST_REGEX.test(line);
|
|
1256
1257
|
class PrettyReporter extends AbstractPrettyReporter {
|
|
1257
1258
|
#stdout;
|
|
1258
1259
|
#stderr;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
class PailError extends Error {
|
|
2
|
+
/** HTTP status code (defaults to 500) */
|
|
3
|
+
status;
|
|
4
|
+
/** Explanation of what caused the failure */
|
|
5
|
+
why;
|
|
6
|
+
/** Suggested resolution steps */
|
|
7
|
+
fix;
|
|
8
|
+
/** Link to relevant documentation */
|
|
9
|
+
link;
|
|
10
|
+
constructor(options) {
|
|
11
|
+
const resolvedOptions = typeof options === "string" ? { message: options } : options;
|
|
12
|
+
super(resolvedOptions.message, resolvedOptions.cause === void 0 ? void 0 : { cause: resolvedOptions.cause });
|
|
13
|
+
this.name = "PailError";
|
|
14
|
+
this.status = resolvedOptions.status ?? 500;
|
|
15
|
+
this.why = resolvedOptions.why;
|
|
16
|
+
this.fix = resolvedOptions.fix;
|
|
17
|
+
this.link = resolvedOptions.link;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Converts the error to a JSON-serializable object.
|
|
21
|
+
*
|
|
22
|
+
* Includes all self-documenting fields in the output for structured logging.
|
|
23
|
+
* @returns A plain object representation of the error
|
|
24
|
+
*/
|
|
25
|
+
toJSON() {
|
|
26
|
+
const json = {
|
|
27
|
+
message: this.message,
|
|
28
|
+
name: this.name,
|
|
29
|
+
status: this.status
|
|
30
|
+
};
|
|
31
|
+
if (this.why) {
|
|
32
|
+
json.why = this.why;
|
|
33
|
+
}
|
|
34
|
+
if (this.fix) {
|
|
35
|
+
json.fix = this.fix;
|
|
36
|
+
}
|
|
37
|
+
if (this.link) {
|
|
38
|
+
json.link = this.link;
|
|
39
|
+
}
|
|
40
|
+
if (this.stack) {
|
|
41
|
+
json.stack = this.stack;
|
|
42
|
+
}
|
|
43
|
+
if (this.cause !== void 0) {
|
|
44
|
+
json.cause = this.cause instanceof Error ? { message: this.cause.message, name: this.cause.name, stack: this.cause.stack } : this.cause;
|
|
45
|
+
}
|
|
46
|
+
return json;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Returns a formatted string representation including self-documenting fields.
|
|
50
|
+
* @returns Formatted error string with why/fix/link context
|
|
51
|
+
*/
|
|
52
|
+
toString() {
|
|
53
|
+
let output = `${this.name} [${this.status}]: ${this.message}`;
|
|
54
|
+
if (this.why) {
|
|
55
|
+
output += `
|
|
56
|
+
Why: ${this.why}`;
|
|
57
|
+
}
|
|
58
|
+
if (this.fix) {
|
|
59
|
+
output += `
|
|
60
|
+
Fix: ${this.fix}`;
|
|
61
|
+
}
|
|
62
|
+
if (this.link) {
|
|
63
|
+
output += `
|
|
64
|
+
Link: ${this.link}`;
|
|
65
|
+
}
|
|
66
|
+
if (this.cause !== void 0) {
|
|
67
|
+
const causeMessage = this.cause instanceof Error ? this.cause.message : String(this.cause);
|
|
68
|
+
output += `
|
|
69
|
+
Cause: ${causeMessage}`;
|
|
70
|
+
}
|
|
71
|
+
return output;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const createPailError = (options) => new PailError(options);
|
|
75
|
+
|
|
76
|
+
export { PailError, createPailError };
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { WideEvent } from '../wide-event.js';
|
|
2
|
+
|
|
3
|
+
const patternToRegex = (pattern) => {
|
|
4
|
+
let regex = "^";
|
|
5
|
+
let index = 0;
|
|
6
|
+
while (index < pattern.length) {
|
|
7
|
+
const char = pattern[index];
|
|
8
|
+
if (char === "*" && pattern[index + 1] === "*") {
|
|
9
|
+
index += 2;
|
|
10
|
+
if (pattern[index] === "/") {
|
|
11
|
+
index += 1;
|
|
12
|
+
}
|
|
13
|
+
if (index >= pattern.length && regex.endsWith("/")) {
|
|
14
|
+
regex = `${regex.slice(0, -1)}(/.*)?`;
|
|
15
|
+
} else if (index >= pattern.length) {
|
|
16
|
+
regex += ".*";
|
|
17
|
+
} else {
|
|
18
|
+
regex += "(.*/)?";
|
|
19
|
+
}
|
|
20
|
+
} else {
|
|
21
|
+
switch (char) {
|
|
22
|
+
case "*": {
|
|
23
|
+
regex += "[^/]*";
|
|
24
|
+
index += 1;
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
case ".": {
|
|
28
|
+
regex += String.raw`\.`;
|
|
29
|
+
index += 1;
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
case "?": {
|
|
33
|
+
regex += "[^/]";
|
|
34
|
+
index += 1;
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
default: {
|
|
38
|
+
regex += char;
|
|
39
|
+
index += 1;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
regex += "$";
|
|
45
|
+
return new RegExp(regex);
|
|
46
|
+
};
|
|
47
|
+
const matchesPattern = (path, pattern) => patternToRegex(pattern).test(path);
|
|
48
|
+
const shouldLog = (path, include, exclude) => {
|
|
49
|
+
if (exclude?.some((pattern) => matchesPattern(path, pattern))) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
if (!include?.length) {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
return include.some((pattern) => matchesPattern(path, pattern));
|
|
56
|
+
};
|
|
57
|
+
const getServiceForPath = (path, routes) => {
|
|
58
|
+
if (!routes) {
|
|
59
|
+
return void 0;
|
|
60
|
+
}
|
|
61
|
+
for (const [pattern, config] of Object.entries(routes)) {
|
|
62
|
+
if (matchesPattern(path, pattern)) {
|
|
63
|
+
return config.service;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return void 0;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const createMiddlewareLogger = (options, request) => {
|
|
70
|
+
const { exclude, include, pail, routes, service } = options;
|
|
71
|
+
const { headers, method, path, requestId } = request;
|
|
72
|
+
if (!shouldLog(path, include, exclude)) {
|
|
73
|
+
const noopLogger = new WideEvent({ autoEmit: false, name: `${method} ${path}`, pail });
|
|
74
|
+
return {
|
|
75
|
+
finish: () => {
|
|
76
|
+
},
|
|
77
|
+
logger: noopLogger,
|
|
78
|
+
skipped: true
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
const routeService = getServiceForPath(path, routes) ?? service;
|
|
82
|
+
const logger = new WideEvent({
|
|
83
|
+
name: `${method} ${path}`,
|
|
84
|
+
pail,
|
|
85
|
+
service: routeService
|
|
86
|
+
});
|
|
87
|
+
logger.set({
|
|
88
|
+
method,
|
|
89
|
+
path,
|
|
90
|
+
requestId,
|
|
91
|
+
...headers ? { headers } : {}
|
|
92
|
+
});
|
|
93
|
+
const finish = (finishOptions) => {
|
|
94
|
+
logger.finish(finishOptions);
|
|
95
|
+
};
|
|
96
|
+
return {
|
|
97
|
+
finish,
|
|
98
|
+
logger,
|
|
99
|
+
skipped: false
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const SENSITIVE_HEADERS = /* @__PURE__ */ new Set(["authorization", "cookie", "proxy-authorization", "set-cookie", "x-api-key", "x-auth-token"]);
|
|
104
|
+
const extractSafeHeaders = (headers) => {
|
|
105
|
+
const safe = {};
|
|
106
|
+
headers.forEach((value, key) => {
|
|
107
|
+
if (!SENSITIVE_HEADERS.has(key.toLowerCase())) {
|
|
108
|
+
safe[key.toLowerCase()] = value;
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
return safe;
|
|
112
|
+
};
|
|
113
|
+
const extractSafeNodeHeaders = (headers) => {
|
|
114
|
+
const safe = {};
|
|
115
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
116
|
+
if (!SENSITIVE_HEADERS.has(key.toLowerCase()) && value !== void 0) {
|
|
117
|
+
safe[key.toLowerCase()] = Array.isArray(value) ? value.join(", ") : value;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return safe;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export { extractSafeHeaders as a, createMiddlewareLogger as c, extractSafeNodeHeaders as e };
|
|
@@ -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 };
|
|
@@ -0,0 +1,33 @@
|
|
|
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 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 };
|
|
@@ -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,78 @@
|
|
|
1
|
+
const detectEnvironment = () => {
|
|
2
|
+
if (!process.env) {
|
|
3
|
+
return {};
|
|
4
|
+
}
|
|
5
|
+
const { env } = process;
|
|
6
|
+
const info = {
|
|
7
|
+
// Commit hash
|
|
8
|
+
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,
|
|
9
|
+
// Environment / Node env
|
|
10
|
+
environment: env.NODE_ENV || env.ENVIRONMENT || env.APP_ENV || void 0,
|
|
11
|
+
// Hostname
|
|
12
|
+
hostname: env.HOSTNAME || env.HOST || void 0,
|
|
13
|
+
// PID
|
|
14
|
+
pid: process.pid,
|
|
15
|
+
// Region (including GCP Cloud Functions FUNCTION_REGION and GOOGLE_CLOUD_REGION)
|
|
16
|
+
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,
|
|
17
|
+
// Service name - check common platform variables (including GCP Cloud Run / App Engine)
|
|
18
|
+
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,
|
|
19
|
+
// Version (including GCP Cloud Run K_REVISION and App Engine GAE_VERSION)
|
|
20
|
+
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
|
|
21
|
+
};
|
|
22
|
+
return Object.fromEntries(Object.entries(info).filter(([, v]) => v !== void 0));
|
|
23
|
+
};
|
|
24
|
+
class EnvironmentProcessor {
|
|
25
|
+
#envInfo;
|
|
26
|
+
/**
|
|
27
|
+
* Creates a new EnvironmentProcessor instance.
|
|
28
|
+
*
|
|
29
|
+
* Auto-detects environment information on construction and merges
|
|
30
|
+
* with any provided overrides. Undefined values in overrides are
|
|
31
|
+
* filtered out so they don't overwrite detected values.
|
|
32
|
+
* @param options Processor configuration options
|
|
33
|
+
*/
|
|
34
|
+
constructor(options = {}) {
|
|
35
|
+
const detected = detectEnvironment();
|
|
36
|
+
const cleanOverrides = {};
|
|
37
|
+
if (options.overrides) {
|
|
38
|
+
for (const [key, value] of Object.entries(options.overrides)) {
|
|
39
|
+
if (value !== void 0) {
|
|
40
|
+
cleanOverrides[key] = value;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const merged = { ...detected, ...cleanOverrides };
|
|
45
|
+
if (!options.includePid && cleanOverrides.pid === void 0) {
|
|
46
|
+
delete merged.pid;
|
|
47
|
+
}
|
|
48
|
+
if (!options.includeHostname && cleanOverrides.hostname === void 0) {
|
|
49
|
+
delete merged.hostname;
|
|
50
|
+
}
|
|
51
|
+
this.#envInfo = merged;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Processes log metadata to add environment information.
|
|
55
|
+
*
|
|
56
|
+
* Adds a `__env` property to the meta containing detected and
|
|
57
|
+
* configured environment details. Each call receives a shallow
|
|
58
|
+
* clone of the environment info to prevent cross-record mutation.
|
|
59
|
+
* @param meta The log metadata to process
|
|
60
|
+
* @returns The processed metadata with environment info added
|
|
61
|
+
*/
|
|
62
|
+
process(meta) {
|
|
63
|
+
const enriched = { ...meta, envStorage: { ...this.#envInfo } };
|
|
64
|
+
return enriched;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Returns the detected environment information.
|
|
68
|
+
*
|
|
69
|
+
* Useful for inspecting what environment details were auto-detected.
|
|
70
|
+
* Returns a shallow clone to prevent external mutation.
|
|
71
|
+
* @returns The environment information object
|
|
72
|
+
*/
|
|
73
|
+
getEnvironmentInfo() {
|
|
74
|
+
return { ...this.#envInfo };
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export { EnvironmentProcessor as default, detectEnvironment };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { FormatterFunction } from "@visulima/fmt";
|
|
2
|
-
import type { stringify } from "safe-stable-stringify";
|
|
3
2
|
import type { Meta, StringifyAwareProcessor } from "../types.d.ts";
|
|
4
3
|
/**
|
|
5
4
|
* Message Formatter Processor.
|
|
@@ -39,7 +38,7 @@ declare class MessageFormatterProcessor<L extends string = string> implements St
|
|
|
39
38
|
* Sets the stringify function for object serialization.
|
|
40
39
|
* @param function_ The stringify function to use for serializing objects
|
|
41
40
|
*/
|
|
42
|
-
setStringify(function_: typeof stringify): void;
|
|
41
|
+
setStringify(function_: typeof JSON.stringify): void;
|
|
43
42
|
/**
|
|
44
43
|
* Processes log metadata to format messages.
|
|
45
44
|
*
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { Meta, Processor } from "../types.d.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Head sampling configuration.
|
|
4
|
+
*
|
|
5
|
+
* Controls random sampling rates per log level. Each key is a log level name
|
|
6
|
+
* and the value is the percentage (0-100) of logs at that level to keep.
|
|
7
|
+
* Levels not listed default to 100 (keep all).
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const headSampling: HeadSamplingConfig = {
|
|
11
|
+
* debug: 0, // Drop all debug logs
|
|
12
|
+
* informational: 10, // Keep 10% of info logs
|
|
13
|
+
* warning: 50, // Keep 50% of warning logs
|
|
14
|
+
* error: 100, // Keep all error logs
|
|
15
|
+
* };
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
type HeadSamplingConfig = Partial<Record<string, number>>;
|
|
19
|
+
/**
|
|
20
|
+
* Tail sampling condition function.
|
|
21
|
+
*
|
|
22
|
+
* A function that receives the log metadata and returns true if the log
|
|
23
|
+
* should be force-kept regardless of head sampling. This allows keeping
|
|
24
|
+
* important logs based on their content (e.g., errors, slow operations).
|
|
25
|
+
* @template L - The log level type
|
|
26
|
+
*/
|
|
27
|
+
type TailSamplingCondition<L extends string = string> = (meta: Readonly<Meta<L>>) => boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Sampling processor configuration options.
|
|
30
|
+
*/
|
|
31
|
+
interface SamplingProcessorOptions<L extends string = string> {
|
|
32
|
+
/**
|
|
33
|
+
* Head sampling rates per log level.
|
|
34
|
+
*
|
|
35
|
+
* A map of log level to sampling percentage (0-100).
|
|
36
|
+
* Levels not specified default to 100 (keep all).
|
|
37
|
+
* Set to 0 to drop all logs at that level.
|
|
38
|
+
*/
|
|
39
|
+
head?: HeadSamplingConfig;
|
|
40
|
+
/**
|
|
41
|
+
* Tail sampling conditions.
|
|
42
|
+
*
|
|
43
|
+
* An array of condition functions that can force-keep a log entry
|
|
44
|
+
* even if it was dropped by head sampling. If any condition returns true,
|
|
45
|
+
* the log is kept.
|
|
46
|
+
*/
|
|
47
|
+
tail?: TailSamplingCondition<L>[];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Sampling Processor.
|
|
51
|
+
*
|
|
52
|
+
* Inspired by evlog's production sampling strategy, this processor implements
|
|
53
|
+
* both head sampling (random per-level) and tail sampling (force-keep based
|
|
54
|
+
* on conditions) to control log volume in production environments.
|
|
55
|
+
*
|
|
56
|
+
* **Head sampling** randomly drops a percentage of logs per level. This is
|
|
57
|
+
* evaluated first and provides broad volume control.
|
|
58
|
+
*
|
|
59
|
+
* **Tail sampling** can override head sampling to force-keep important logs
|
|
60
|
+
* based on their content. For example, you might drop 90% of info logs but
|
|
61
|
+
* force-keep any that contain error information or relate to slow operations.
|
|
62
|
+
*
|
|
63
|
+
* When a log is dropped by sampling, the processor sets a `__dropped: true`
|
|
64
|
+
* boolean flag on the meta object. Reporters should check for this flag and
|
|
65
|
+
* skip entries where `__dropped` is `true`.
|
|
66
|
+
* @template L - The log level type
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* import { createPail } from "@visulima/pail";
|
|
70
|
+
* import SamplingProcessor from "@visulima/pail/processor/sampling";
|
|
71
|
+
*
|
|
72
|
+
* const logger = createPail({
|
|
73
|
+
* processors: [
|
|
74
|
+
* new SamplingProcessor({
|
|
75
|
+
* head: {
|
|
76
|
+
* debug: 0, // Drop all debug logs
|
|
77
|
+
* informational: 10, // Keep 10% of info logs
|
|
78
|
+
* warning: 50, // Keep 50% of warnings
|
|
79
|
+
* error: 100, // Keep all errors
|
|
80
|
+
* },
|
|
81
|
+
* tail: [
|
|
82
|
+
* // Force-keep logs with errors regardless of head sampling
|
|
83
|
+
* (meta) => meta.error !== undefined,
|
|
84
|
+
* // Force-keep logs from critical scopes
|
|
85
|
+
* (meta) => meta.scope?.includes("payment") ?? false,
|
|
86
|
+
* ],
|
|
87
|
+
* }),
|
|
88
|
+
* ],
|
|
89
|
+
* });
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
declare class SamplingProcessor<L extends string = string> implements Processor<L> {
|
|
93
|
+
#private;
|
|
94
|
+
/**
|
|
95
|
+
* Creates a new SamplingProcessor instance.
|
|
96
|
+
* @param options Sampling configuration options
|
|
97
|
+
*/
|
|
98
|
+
constructor(options?: SamplingProcessorOptions<L>);
|
|
99
|
+
/**
|
|
100
|
+
* Processes log metadata to apply sampling rules.
|
|
101
|
+
*
|
|
102
|
+
* First evaluates head sampling (random per-level), then checks tail
|
|
103
|
+
* sampling conditions. If a log is dropped, the meta is marked with
|
|
104
|
+
* `__dropped: true` so reporters can skip it.
|
|
105
|
+
* @param meta The log metadata to process
|
|
106
|
+
* @returns The processed metadata, potentially marked as dropped
|
|
107
|
+
*/
|
|
108
|
+
process(meta: Meta<L>): Meta<L>;
|
|
109
|
+
}
|
|
110
|
+
export default SamplingProcessor;
|
|
111
|
+
export type { HeadSamplingConfig, SamplingProcessorOptions, TailSamplingCondition };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
class SamplingProcessor {
|
|
2
|
+
#headRates;
|
|
3
|
+
#tailConditions;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a new SamplingProcessor instance.
|
|
6
|
+
* @param options Sampling configuration options
|
|
7
|
+
*/
|
|
8
|
+
constructor(options = {}) {
|
|
9
|
+
this.#headRates = options.head ?? {};
|
|
10
|
+
this.#tailConditions = options.tail ?? [];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Processes log metadata to apply sampling rules.
|
|
14
|
+
*
|
|
15
|
+
* First evaluates head sampling (random per-level), then checks tail
|
|
16
|
+
* sampling conditions. If a log is dropped, the meta is marked with
|
|
17
|
+
* `__dropped: true` so reporters can skip it.
|
|
18
|
+
* @param meta The log metadata to process
|
|
19
|
+
* @returns The processed metadata, potentially marked as dropped
|
|
20
|
+
*/
|
|
21
|
+
process(meta) {
|
|
22
|
+
const level = meta.type.level;
|
|
23
|
+
if (this.#shouldDrop(level) && !this.#shouldForceKeep(meta)) {
|
|
24
|
+
return { ...meta, dropped: true };
|
|
25
|
+
}
|
|
26
|
+
return meta;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Evaluates head sampling for the given log level.
|
|
30
|
+
* @returns true if the log should be dropped
|
|
31
|
+
*/
|
|
32
|
+
#shouldDrop(level) {
|
|
33
|
+
const rate = this.#headRates[level];
|
|
34
|
+
if (rate === void 0) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
if (rate <= 0) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
if (rate >= 100) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
return Math.random() * 100 >= rate;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Evaluates tail sampling conditions.
|
|
47
|
+
* @returns true if any condition forces keeping the log
|
|
48
|
+
*/
|
|
49
|
+
#shouldForceKeep(meta) {
|
|
50
|
+
for (const condition of this.#tailConditions) {
|
|
51
|
+
if (condition(meta)) {
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export { SamplingProcessor as default };
|