evlog 2.12.0 → 2.14.0
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/README.md +111 -0
- package/dist/adapters/axiom.d.mts +1 -1
- package/dist/adapters/better-stack.d.mts +1 -1
- package/dist/adapters/datadog.d.mts +1 -1
- package/dist/adapters/fs.d.mts +1 -1
- package/dist/adapters/hyperdx.d.mts +1 -1
- package/dist/adapters/otlp.d.mts +1 -1
- package/dist/adapters/posthog.d.mts +1 -1
- package/dist/adapters/sentry.d.mts +1 -1
- package/dist/ai/index.d.mts +106 -5
- package/dist/ai/index.d.mts.map +1 -1
- package/dist/ai/index.mjs +28 -5
- package/dist/ai/index.mjs.map +1 -1
- package/dist/audit-d9esRZOK.mjs +1440 -0
- package/dist/audit-d9esRZOK.mjs.map +1 -0
- package/dist/audit-mUutdf6A.d.mts +1130 -0
- package/dist/audit-mUutdf6A.d.mts.map +1 -0
- package/dist/better-auth/index.d.mts +220 -0
- package/dist/better-auth/index.d.mts.map +1 -0
- package/dist/better-auth/index.mjs +205 -0
- package/dist/better-auth/index.mjs.map +1 -0
- package/dist/browser.d.mts +1 -1
- package/dist/elysia/index.d.mts +2 -2
- package/dist/elysia/index.d.mts.map +1 -1
- package/dist/elysia/index.mjs +16 -4
- package/dist/elysia/index.mjs.map +1 -1
- package/dist/enrichers.d.mts +1 -1
- package/dist/{error-WRz4_F3W.d.mts → error-D1FZI2Kd.d.mts} +2 -2
- package/dist/{error-WRz4_F3W.d.mts.map → error-D1FZI2Kd.d.mts.map} +1 -1
- package/dist/error.d.mts +1 -1
- package/dist/{errors-J2kt7mZh.d.mts → errors-NIXCyk6I.d.mts} +2 -2
- package/dist/{errors-J2kt7mZh.d.mts.map → errors-NIXCyk6I.d.mts.map} +1 -1
- package/dist/express/index.d.mts +2 -2
- package/dist/express/index.d.mts.map +1 -1
- package/dist/express/index.mjs +8 -4
- package/dist/express/index.mjs.map +1 -1
- package/dist/fastify/index.d.mts +2 -2
- package/dist/fastify/index.d.mts.map +1 -1
- package/dist/fastify/index.mjs +8 -4
- package/dist/fastify/index.mjs.map +1 -1
- package/dist/fork-CTJXnpl8.mjs +72 -0
- package/dist/fork-CTJXnpl8.mjs.map +1 -0
- package/dist/headers-D74M0wsg.mjs +30 -0
- package/dist/headers-D74M0wsg.mjs.map +1 -0
- package/dist/hono/index.d.mts +2 -2
- package/dist/hono/index.mjs +2 -1
- package/dist/hono/index.mjs.map +1 -1
- package/dist/http.d.mts +1 -1
- package/dist/index.d.mts +7 -7
- package/dist/index.mjs +2 -2
- package/dist/{logger-Bm0k3Hf3.d.mts → logger-b3epPH0N.d.mts} +8 -4
- package/dist/logger-b3epPH0N.d.mts.map +1 -0
- package/dist/logger.d.mts +1 -1
- package/dist/logger.mjs +1 -1
- package/dist/{headers-ht4yS2mx.mjs → middleware-BWOJ7JI0.mjs} +9 -30
- package/dist/middleware-BWOJ7JI0.mjs.map +1 -0
- package/dist/{middleware-D_igVy93.d.mts → middleware-BYf26Lfu.d.mts} +14 -3
- package/dist/{middleware-D_igVy93.d.mts.map → middleware-BYf26Lfu.d.mts.map} +1 -1
- package/dist/nestjs/index.d.mts +2 -2
- package/dist/nestjs/index.d.mts.map +1 -1
- package/dist/nestjs/index.mjs +8 -4
- package/dist/nestjs/index.mjs.map +1 -1
- package/dist/next/client.d.mts +1 -1
- package/dist/next/index.d.mts +4 -4
- package/dist/next/index.d.mts.map +1 -1
- package/dist/next/index.mjs +15 -1
- package/dist/next/index.mjs.map +1 -1
- package/dist/next/instrumentation.d.mts +1 -1
- package/dist/next/instrumentation.mjs +1 -1
- package/dist/nitro/module.d.mts +2 -2
- package/dist/nitro/plugin.mjs +1 -1
- package/dist/nitro/v3/index.d.mts +2 -2
- package/dist/nitro/v3/module.d.mts +1 -1
- package/dist/nitro/v3/plugin.mjs +1 -1
- package/dist/nitro/v3/useLogger.d.mts +1 -1
- package/dist/{nitro-BeRXZcBd.d.mts → nitro-DenB86W6.d.mts} +2 -2
- package/dist/{nitro-BeRXZcBd.d.mts.map → nitro-DenB86W6.d.mts.map} +1 -1
- package/dist/nuxt/module.d.mts +1 -1
- package/dist/nuxt/module.mjs +1 -1
- package/dist/{parseError-DhXS_vzM.d.mts → parseError-BR9pocvY.d.mts} +2 -2
- package/dist/parseError-BR9pocvY.d.mts.map +1 -0
- package/dist/react-router/index.d.mts +2 -2
- package/dist/react-router/index.d.mts.map +1 -1
- package/dist/react-router/index.mjs +8 -4
- package/dist/react-router/index.mjs.map +1 -1
- package/dist/runtime/client/log.d.mts +1 -1
- package/dist/runtime/server/routes/_evlog/ingest.post.mjs +1 -1
- package/dist/runtime/server/useLogger.d.mts +1 -1
- package/dist/runtime/utils/parseError.d.mts +2 -2
- package/dist/{storage-DpLJYMoc.mjs → storage-CFGTn37X.mjs} +1 -1
- package/dist/{storage-DpLJYMoc.mjs.map → storage-CFGTn37X.mjs.map} +1 -1
- package/dist/sveltekit/index.d.mts +2 -2
- package/dist/sveltekit/index.d.mts.map +1 -1
- package/dist/sveltekit/index.mjs +8 -4
- package/dist/sveltekit/index.mjs.map +1 -1
- package/dist/toolkit.d.mts +41 -4
- package/dist/toolkit.d.mts.map +1 -1
- package/dist/toolkit.mjs +5 -3
- package/dist/types.d.mts +2 -2
- package/dist/{useLogger-Dcj1Nrsa.d.mts → useLogger-C56tDPwf.d.mts} +2 -2
- package/dist/{useLogger-Dcj1Nrsa.d.mts.map → useLogger-C56tDPwf.d.mts.map} +1 -1
- package/dist/{utils-Bnc95-VC.d.mts → utils-DzGCLRFe.d.mts} +2 -2
- package/dist/{utils-Bnc95-VC.d.mts.map → utils-DzGCLRFe.d.mts.map} +1 -1
- package/dist/utils.d.mts +1 -1
- package/dist/vite/index.d.mts +1 -1
- package/dist/workers.d.mts +1 -1
- package/dist/workers.mjs +1 -1
- package/package.json +16 -3
- package/dist/headers-ht4yS2mx.mjs.map +0 -1
- package/dist/logger-Bm0k3Hf3.d.mts.map +0 -1
- package/dist/logger-DY0X5oQd.mjs +0 -704
- package/dist/logger-DY0X5oQd.mjs.map +0 -1
- package/dist/parseError-DhXS_vzM.d.mts.map +0 -1
- package/dist/types-D5OwxZCw.d.mts +0 -587
- package/dist/types-D5OwxZCw.d.mts.map +0 -1
|
@@ -0,0 +1,1440 @@
|
|
|
1
|
+
import { colors, cssColors, detectEnvironment, escapeFormatString, formatDuration, getConsoleMethod, getCssLevelColor, getLevelColor, isClient, isDev, isLevelEnabled, matchesPattern } from "./utils.mjs";
|
|
2
|
+
//#region src/redact.ts
|
|
3
|
+
const DEFAULT_REPLACEMENT = "[REDACTED]";
|
|
4
|
+
/**
|
|
5
|
+
* Built-in PII detection patterns with smart masking.
|
|
6
|
+
* Each builtin preserves just enough signal for debugging while scrubbing PII.
|
|
7
|
+
*/
|
|
8
|
+
const builtinPatterns = {
|
|
9
|
+
creditCard: {
|
|
10
|
+
pattern: /\b\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}\b/g,
|
|
11
|
+
mask: (m) => `****${m.replace(/[\s-]/g, "").slice(-4)}`
|
|
12
|
+
},
|
|
13
|
+
email: {
|
|
14
|
+
pattern: /[\w.+-]+@[\w-]+\.[\w.]+/g,
|
|
15
|
+
mask: (m) => {
|
|
16
|
+
if (m.indexOf("@") < 1) return "***@***";
|
|
17
|
+
const tld = m.slice(m.lastIndexOf("."));
|
|
18
|
+
return `${m[0]}***@***${tld}`;
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
ipv4: {
|
|
22
|
+
pattern: /\b(?!0\.0\.0\.0\b)(?!127\.0\.0\.1\b)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/g,
|
|
23
|
+
mask: (m) => `***.***.***.${m.split(".").pop()}`
|
|
24
|
+
},
|
|
25
|
+
phone: {
|
|
26
|
+
pattern: /(?:\+\d{1,3}[\s.-]?\(?\d{1,4}\)?|\(\d{1,4}\))(?:[\s.-]?\d{2,4}){2,4}\b/g,
|
|
27
|
+
mask: (m) => {
|
|
28
|
+
const digits = m.replace(/[^\d]/g, "");
|
|
29
|
+
if (m.startsWith("+") && digits.length > 4) {
|
|
30
|
+
const ccMatch = m.match(/^\+\d{1,3}/);
|
|
31
|
+
return `${ccMatch ? ccMatch[0] : "+"}******${digits.slice(-2)}`;
|
|
32
|
+
}
|
|
33
|
+
if (digits.length > 2) return `${"*".repeat(digits.length - 2)}${digits.slice(-2)}`;
|
|
34
|
+
return "***";
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
jwt: {
|
|
38
|
+
pattern: /\beyJ[\w-]*\.[\w-]*\.[\w-]*\b/g,
|
|
39
|
+
mask: () => "eyJ***.***"
|
|
40
|
+
},
|
|
41
|
+
bearer: {
|
|
42
|
+
pattern: /\bBearer\s+[\w\-.~+/]{8,}=*/gi,
|
|
43
|
+
mask: () => "Bearer ***"
|
|
44
|
+
},
|
|
45
|
+
iban: {
|
|
46
|
+
pattern: /\b[A-Z]{2}\d{2}[\s-]?[\dA-Z]{4}[\s-]?[\dA-Z]{4}[\s-]?[\dA-Z]{4}[\s-]?[\dA-Z]{0,4}[\s-]?[\dA-Z]{0,4}[\s-]?[\dA-Z]{0,4}\b/g,
|
|
47
|
+
mask: (m) => {
|
|
48
|
+
const clean = m.replace(/[\s-]/g, "");
|
|
49
|
+
return `${clean.slice(0, 4)}****${clean.slice(-3)}`;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Resolve a `redact` option (boolean or object) into a concrete `RedactConfig`.
|
|
55
|
+
*
|
|
56
|
+
* - `true` → all built-in patterns with smart masking, no custom paths
|
|
57
|
+
* - `{ ... }` → built-in maskers merged with user config (opt-out: `builtins: false`)
|
|
58
|
+
* - `false` / `undefined` → `undefined` (no redaction)
|
|
59
|
+
*/
|
|
60
|
+
function resolveRedactConfig(input) {
|
|
61
|
+
if (input === void 0 || input === false) return void 0;
|
|
62
|
+
if (input === true) return { _maskers: allBuiltinMaskers() };
|
|
63
|
+
if (input.builtins === false) return input;
|
|
64
|
+
const maskers = Array.isArray(input.builtins) ? input.builtins.map((name) => builtinPatterns[name]).filter(Boolean).map((b) => [cloneRegex(b.pattern), b.mask]) : allBuiltinMaskers();
|
|
65
|
+
return {
|
|
66
|
+
...input,
|
|
67
|
+
_maskers: maskers
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function allBuiltinMaskers() {
|
|
71
|
+
return Object.values(builtinPatterns).map((b) => [cloneRegex(b.pattern), b.mask]);
|
|
72
|
+
}
|
|
73
|
+
function cloneRegex(re) {
|
|
74
|
+
return new RegExp(re.source, re.flags);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Redact sensitive data from a wide event in-place.
|
|
78
|
+
*
|
|
79
|
+
* Three strategies applied in order:
|
|
80
|
+
* 1. **Path-based**: dot-notation paths — the leaf value is replaced with `replacement`.
|
|
81
|
+
* 2. **Masker-based**: built-in patterns with smart partial masking (e.g. `****1111`).
|
|
82
|
+
* 3. **Pattern-based**: custom RegExp patterns replaced with `replacement`.
|
|
83
|
+
*
|
|
84
|
+
* @param event - The wide event object (mutated in-place).
|
|
85
|
+
* @param config - Redaction configuration.
|
|
86
|
+
*/
|
|
87
|
+
function redactEvent(event, config) {
|
|
88
|
+
const replacement = config.replacement ?? DEFAULT_REPLACEMENT;
|
|
89
|
+
if (config.paths?.length) for (const path of config.paths) redactPath(event, path.split("."), replacement);
|
|
90
|
+
if (config._maskers?.length) applyMaskersToTree(event, config._maskers);
|
|
91
|
+
if (config.patterns?.length) redactPatterns(event, config.patterns, replacement);
|
|
92
|
+
}
|
|
93
|
+
function redactPath(obj, segments, replacement) {
|
|
94
|
+
let current = obj;
|
|
95
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
96
|
+
if (current === null || current === void 0 || typeof current !== "object") return;
|
|
97
|
+
current = current[segments[i]];
|
|
98
|
+
}
|
|
99
|
+
if (current === null || current === void 0 || typeof current !== "object") return;
|
|
100
|
+
const leaf = segments[segments.length - 1];
|
|
101
|
+
if (leaf in current) current[leaf] = replacement;
|
|
102
|
+
}
|
|
103
|
+
function redactPatterns(obj, patterns, replacement) {
|
|
104
|
+
if (obj === null || obj === void 0) return;
|
|
105
|
+
if (Array.isArray(obj)) {
|
|
106
|
+
for (let i = 0; i < obj.length; i++) if (typeof obj[i] === "string") obj[i] = applyPatterns(obj[i], patterns, replacement);
|
|
107
|
+
else if (typeof obj[i] === "object") redactPatterns(obj[i], patterns, replacement);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (typeof obj === "object") {
|
|
111
|
+
const record = obj;
|
|
112
|
+
for (const key in record) {
|
|
113
|
+
const val = record[key];
|
|
114
|
+
if (typeof val === "string") record[key] = applyPatterns(val, patterns, replacement);
|
|
115
|
+
else if (typeof val === "object") redactPatterns(val, patterns, replacement);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function applyPatterns(value, patterns, replacement) {
|
|
120
|
+
let result = value;
|
|
121
|
+
for (const pattern of patterns) {
|
|
122
|
+
pattern.lastIndex = 0;
|
|
123
|
+
result = result.replace(pattern, replacement);
|
|
124
|
+
}
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
function applyMaskersToTree(obj, maskers) {
|
|
128
|
+
if (obj === null || obj === void 0) return;
|
|
129
|
+
if (Array.isArray(obj)) {
|
|
130
|
+
for (let i = 0; i < obj.length; i++) if (typeof obj[i] === "string") obj[i] = applyMaskers(obj[i], maskers);
|
|
131
|
+
else if (typeof obj[i] === "object") applyMaskersToTree(obj[i], maskers);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (typeof obj === "object") {
|
|
135
|
+
const record = obj;
|
|
136
|
+
for (const key in record) {
|
|
137
|
+
const val = record[key];
|
|
138
|
+
if (typeof val === "string") record[key] = applyMaskers(val, maskers);
|
|
139
|
+
else if (typeof val === "object") applyMaskersToTree(val, maskers);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function applyMaskers(value, maskers) {
|
|
144
|
+
let result = value;
|
|
145
|
+
for (const [pattern, mask] of maskers) {
|
|
146
|
+
pattern.lastIndex = 0;
|
|
147
|
+
result = result.replace(pattern, mask);
|
|
148
|
+
}
|
|
149
|
+
return result;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Normalize a redact config that may have been deserialized from JSON
|
|
153
|
+
* (e.g. via `process.env.__EVLOG_CONFIG`). Converts pattern strings
|
|
154
|
+
* back to RegExp instances, then resolves built-in patterns.
|
|
155
|
+
*/
|
|
156
|
+
function normalizeRedactConfig(raw) {
|
|
157
|
+
if (raw === void 0 || raw === false) return void 0;
|
|
158
|
+
if (raw === true) return resolveRedactConfig(true);
|
|
159
|
+
const config = {};
|
|
160
|
+
if (Array.isArray(raw.paths)) config.paths = raw.paths;
|
|
161
|
+
if (typeof raw.replacement === "string") config.replacement = raw.replacement;
|
|
162
|
+
if (raw.builtins === false) config.builtins = false;
|
|
163
|
+
else if (Array.isArray(raw.builtins)) config.builtins = raw.builtins;
|
|
164
|
+
if (Array.isArray(raw.patterns)) config.patterns = raw.patterns.map((p) => {
|
|
165
|
+
if (p instanceof RegExp) return p;
|
|
166
|
+
if (typeof p === "string") return new RegExp(p, "g");
|
|
167
|
+
if (typeof p === "object" && p !== null) {
|
|
168
|
+
const obj = p;
|
|
169
|
+
return new RegExp(obj.source, obj.flags ?? "g");
|
|
170
|
+
}
|
|
171
|
+
return null;
|
|
172
|
+
}).filter((p) => p !== null);
|
|
173
|
+
return resolveRedactConfig(config);
|
|
174
|
+
}
|
|
175
|
+
//#endregion
|
|
176
|
+
//#region src/logger.ts
|
|
177
|
+
function isPlainObject(val) {
|
|
178
|
+
return val !== null && typeof val === "object" && !Array.isArray(val);
|
|
179
|
+
}
|
|
180
|
+
const _tsDate = /* @__PURE__ */ new Date();
|
|
181
|
+
function isoNow() {
|
|
182
|
+
_tsDate.setTime(Date.now());
|
|
183
|
+
return _tsDate.toISOString();
|
|
184
|
+
}
|
|
185
|
+
/** Shown after post-emit warnings so users can fix fire-and-forget / ALS continuations. */
|
|
186
|
+
const POST_EMIT_FORK_HINT = "For intentional background work tied to this request, use log.fork('label', fn) when your integration supports it (see https://evlog.dev).";
|
|
187
|
+
function warnPostEmit(method, detail) {
|
|
188
|
+
console.warn(`[evlog] ${method} called after the wide event was emitted — ${detail} This data will not appear in observability. ${POST_EMIT_FORK_HINT}`);
|
|
189
|
+
}
|
|
190
|
+
function mergeInto(target, source) {
|
|
191
|
+
for (const key in source) {
|
|
192
|
+
const sourceVal = source[key];
|
|
193
|
+
if (sourceVal === void 0 || sourceVal === null) continue;
|
|
194
|
+
const targetVal = target[key];
|
|
195
|
+
if (isPlainObject(sourceVal) && isPlainObject(targetVal)) mergeInto(targetVal, sourceVal);
|
|
196
|
+
else if (Array.isArray(targetVal) && Array.isArray(sourceVal)) target[key] = [...targetVal, ...sourceVal];
|
|
197
|
+
else target[key] = sourceVal;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
let globalEnv = {
|
|
201
|
+
service: "app",
|
|
202
|
+
environment: "development"
|
|
203
|
+
};
|
|
204
|
+
let globalPretty = isDev();
|
|
205
|
+
let globalSampling = {};
|
|
206
|
+
let globalStringify = true;
|
|
207
|
+
let globalDrain;
|
|
208
|
+
let globalRedact;
|
|
209
|
+
let globalEnabled = true;
|
|
210
|
+
let globalSilent = false;
|
|
211
|
+
/** Minimum level for the global `log` API only (`ownsEvent === false`). Default: all levels. */
|
|
212
|
+
let globalMinLevel = "debug";
|
|
213
|
+
let _locked = false;
|
|
214
|
+
/**
|
|
215
|
+
* Initialize the logger with configuration.
|
|
216
|
+
* Call this once at application startup.
|
|
217
|
+
*/
|
|
218
|
+
function initLogger(config = {}) {
|
|
219
|
+
globalEnabled = config.enabled ?? true;
|
|
220
|
+
const detected = detectEnvironment();
|
|
221
|
+
globalEnv = {
|
|
222
|
+
service: config.env?.service ?? detected.service ?? "app",
|
|
223
|
+
environment: config.env?.environment ?? detected.environment ?? "development",
|
|
224
|
+
version: config.env?.version ?? detected.version,
|
|
225
|
+
commitHash: config.env?.commitHash ?? detected.commitHash,
|
|
226
|
+
region: config.env?.region ?? detected.region
|
|
227
|
+
};
|
|
228
|
+
globalPretty = config.pretty ?? isDev();
|
|
229
|
+
globalSampling = config.sampling ?? {};
|
|
230
|
+
globalStringify = config.stringify ?? true;
|
|
231
|
+
globalDrain = config.drain;
|
|
232
|
+
globalRedact = resolveRedactConfig(config.redact ?? !isDev());
|
|
233
|
+
globalSilent = config.silent ?? false;
|
|
234
|
+
globalMinLevel = config.minLevel ?? "debug";
|
|
235
|
+
if (globalSilent && !globalDrain && !config._suppressDrainWarning) console.warn("[evlog] silent mode is enabled but no drain is configured. Events will be built and sampled but not output anywhere. Set a drain via initLogger({ drain }) or a framework hook (evlog:drain).");
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Check if logging is globally enabled.
|
|
239
|
+
*/
|
|
240
|
+
function isEnabled() {
|
|
241
|
+
return globalEnabled;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* @internal Lock the logger to prevent re-initialization.
|
|
245
|
+
* Called by instrumentation register() after setting up the logger with drain.
|
|
246
|
+
* Prevents configureHandler() from overwriting the drain config.
|
|
247
|
+
*/
|
|
248
|
+
function lockLogger() {
|
|
249
|
+
_locked = true;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* @internal Check if the logger has been locked by instrumentation.
|
|
253
|
+
*/
|
|
254
|
+
function isLoggerLocked() {
|
|
255
|
+
return _locked;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* @internal Get the globally configured drain callback.
|
|
259
|
+
* Used by framework middleware to fall back to the global drain
|
|
260
|
+
* when no middleware-level drain is provided.
|
|
261
|
+
*/
|
|
262
|
+
function getGlobalDrain() {
|
|
263
|
+
return globalDrain;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Determine if a log at the given level should be emitted based on sampling config.
|
|
267
|
+
* Error level defaults to 100% (always logged) unless explicitly configured otherwise.
|
|
268
|
+
*/
|
|
269
|
+
function shouldSample(level) {
|
|
270
|
+
const { rates } = globalSampling;
|
|
271
|
+
if (!rates) return true;
|
|
272
|
+
const percentage = level === "error" && rates.error === void 0 ? 100 : rates[level] ?? 100;
|
|
273
|
+
if (percentage <= 0) return false;
|
|
274
|
+
if (percentage >= 100) return true;
|
|
275
|
+
return Math.random() * 100 < percentage;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Evaluate tail sampling conditions to determine if a log should be force-kept.
|
|
279
|
+
* Returns true if ANY condition matches (OR logic).
|
|
280
|
+
*/
|
|
281
|
+
function shouldKeep(ctx) {
|
|
282
|
+
const { keep } = globalSampling;
|
|
283
|
+
if (!keep?.length) return false;
|
|
284
|
+
return keep.some((condition) => {
|
|
285
|
+
if (condition.status !== void 0 && ctx.status !== void 0 && ctx.status >= condition.status) return true;
|
|
286
|
+
if (condition.duration !== void 0 && ctx.duration !== void 0 && ctx.duration >= condition.duration) return true;
|
|
287
|
+
if (condition.path && ctx.path && matchesPattern(ctx.path, condition.path)) return true;
|
|
288
|
+
return false;
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
function emitWideEvent(level, event, deferDrain = false, ownsEvent = false) {
|
|
292
|
+
if (!globalEnabled) return null;
|
|
293
|
+
if (!ownsEvent) {
|
|
294
|
+
if (!isLevelEnabled(level, globalMinLevel)) return null;
|
|
295
|
+
if (!shouldSample(level)) return null;
|
|
296
|
+
}
|
|
297
|
+
let formatted;
|
|
298
|
+
if (ownsEvent) {
|
|
299
|
+
event.timestamp = isoNow();
|
|
300
|
+
event.level = level;
|
|
301
|
+
if (event.service === void 0) event.service = globalEnv.service;
|
|
302
|
+
if (event.environment === void 0) event.environment = globalEnv.environment;
|
|
303
|
+
if (globalEnv.version !== void 0 && event.version === void 0) event.version = globalEnv.version;
|
|
304
|
+
if (globalEnv.commitHash !== void 0 && event.commitHash === void 0) event.commitHash = globalEnv.commitHash;
|
|
305
|
+
if (globalEnv.region !== void 0 && event.region === void 0) event.region = globalEnv.region;
|
|
306
|
+
formatted = event;
|
|
307
|
+
} else formatted = {
|
|
308
|
+
timestamp: isoNow(),
|
|
309
|
+
level,
|
|
310
|
+
...globalEnv,
|
|
311
|
+
...event
|
|
312
|
+
};
|
|
313
|
+
finalizeAudit(formatted);
|
|
314
|
+
if (globalRedact) redactEvent(formatted, globalRedact);
|
|
315
|
+
if (!globalSilent) if (globalPretty) prettyPrintWideEvent(formatted);
|
|
316
|
+
else if (globalStringify) console[getConsoleMethod(level)](JSON.stringify(formatted));
|
|
317
|
+
else console[getConsoleMethod(level)](formatted);
|
|
318
|
+
if (globalDrain && !deferDrain) Promise.resolve(globalDrain({ event: formatted })).catch((err) => {
|
|
319
|
+
console.error("[evlog] drain failed:", err);
|
|
320
|
+
});
|
|
321
|
+
return formatted;
|
|
322
|
+
}
|
|
323
|
+
function emitTaggedLog(level, tag, message) {
|
|
324
|
+
if (!globalEnabled) return;
|
|
325
|
+
if (globalPretty && !globalSilent) {
|
|
326
|
+
if (!isLevelEnabled(level, globalMinLevel)) return;
|
|
327
|
+
if (!shouldSample(level)) return;
|
|
328
|
+
if (isClient()) {
|
|
329
|
+
const levelColor = getCssLevelColor(level);
|
|
330
|
+
const timestamp = isoNow().slice(11, 23);
|
|
331
|
+
console.log(`%c${timestamp}%c %c[${escapeFormatString(tag)}]%c ${escapeFormatString(message)}`, cssColors.dim, cssColors.reset, levelColor, cssColors.reset);
|
|
332
|
+
} else {
|
|
333
|
+
const color = getLevelColor(level);
|
|
334
|
+
const timestamp = isoNow().slice(11, 23);
|
|
335
|
+
console.log(`${colors.dim}${timestamp}${colors.reset} ${color}[${tag}]${colors.reset} ${message}`);
|
|
336
|
+
}
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
emitWideEvent(level, {
|
|
340
|
+
tag,
|
|
341
|
+
message
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
function formatValue(value) {
|
|
345
|
+
if (value === null || value === void 0) return String(value);
|
|
346
|
+
if (typeof value === "object") {
|
|
347
|
+
const pairs = [];
|
|
348
|
+
for (const [k, v] of Object.entries(value)) if (v !== void 0 && v !== null) if (typeof v === "object") pairs.push(`${k}=${JSON.stringify(v)}`);
|
|
349
|
+
else pairs.push(`${k}=${v}`);
|
|
350
|
+
return pairs.join(" ");
|
|
351
|
+
}
|
|
352
|
+
return String(value);
|
|
353
|
+
}
|
|
354
|
+
function formatCost(cost) {
|
|
355
|
+
if (cost < .01) return `$${cost.toFixed(6)}`;
|
|
356
|
+
if (cost < 1) return `$${cost.toFixed(4)}`;
|
|
357
|
+
return `$${cost.toFixed(2)}`;
|
|
358
|
+
}
|
|
359
|
+
function buildAIEntries(ai) {
|
|
360
|
+
const entries = [];
|
|
361
|
+
const headerParts = [];
|
|
362
|
+
if (ai.model) {
|
|
363
|
+
let m = String(ai.model);
|
|
364
|
+
if (ai.provider) m += ` (${ai.provider})`;
|
|
365
|
+
headerParts.push(m);
|
|
366
|
+
}
|
|
367
|
+
if (ai.calls) headerParts.push(`${ai.calls} call${ai.calls > 1 ? "s" : ""}`);
|
|
368
|
+
if (ai.steps && ai.steps > 1) headerParts.push(`${ai.steps} steps`);
|
|
369
|
+
entries.push({
|
|
370
|
+
key: "ai",
|
|
371
|
+
value: headerParts.join(" · ")
|
|
372
|
+
});
|
|
373
|
+
const inputTokens = ai.inputTokens;
|
|
374
|
+
const outputTokens = ai.outputTokens;
|
|
375
|
+
const totalTokens = ai.totalTokens;
|
|
376
|
+
if (inputTokens !== void 0 && outputTokens !== void 0) {
|
|
377
|
+
let tokLine = `${inputTokens} in → ${outputTokens} out`;
|
|
378
|
+
if (totalTokens) tokLine += ` (${totalTokens} total)`;
|
|
379
|
+
const extras = [];
|
|
380
|
+
if (ai.cacheReadTokens) extras.push(`${ai.cacheReadTokens} cache read`);
|
|
381
|
+
if (ai.cacheWriteTokens) extras.push(`${ai.cacheWriteTokens} cache write`);
|
|
382
|
+
if (ai.reasoningTokens) extras.push(`${ai.reasoningTokens} reasoning`);
|
|
383
|
+
if (extras.length) tokLine += ` · ${extras.join(" · ")}`;
|
|
384
|
+
entries.push({
|
|
385
|
+
key: "ai.tokens",
|
|
386
|
+
value: tokLine
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
const msFirst = ai.msToFirstChunk;
|
|
390
|
+
const msFinish = ai.msToFinish;
|
|
391
|
+
const tps = ai.tokensPerSecond;
|
|
392
|
+
if (msFirst !== void 0 || msFinish !== void 0) {
|
|
393
|
+
const parts = [];
|
|
394
|
+
if (msFirst !== void 0) parts.push(`${formatDuration(msFirst)} to first chunk`);
|
|
395
|
+
if (msFinish !== void 0) parts.push(`${formatDuration(msFinish)} total`);
|
|
396
|
+
let streamLine = parts.join(" → ");
|
|
397
|
+
if (tps) streamLine += ` · ${tps} tok/s`;
|
|
398
|
+
entries.push({
|
|
399
|
+
key: "ai.streaming",
|
|
400
|
+
value: streamLine
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
if (ai.estimatedCost !== void 0) entries.push({
|
|
404
|
+
key: "ai.cost",
|
|
405
|
+
value: formatCost(ai.estimatedCost)
|
|
406
|
+
});
|
|
407
|
+
if (ai.totalDurationMs !== void 0) entries.push({
|
|
408
|
+
key: "ai.totalDuration",
|
|
409
|
+
value: formatDuration(ai.totalDurationMs)
|
|
410
|
+
});
|
|
411
|
+
const toolCalls = ai.toolCalls;
|
|
412
|
+
const tools = ai.tools;
|
|
413
|
+
const hasInputs = toolCalls?.length ? typeof toolCalls[0] === "object" : false;
|
|
414
|
+
if (tools?.length) {
|
|
415
|
+
const children = tools.map((t, idx) => {
|
|
416
|
+
const mark = t.success ? "✓" : "✗";
|
|
417
|
+
let line = `${t.name} ${formatDuration(t.durationMs)} ${mark}`;
|
|
418
|
+
if (t.error) line += ` ${t.error}`;
|
|
419
|
+
if (hasInputs && toolCalls && idx < toolCalls.length) {
|
|
420
|
+
const tc = toolCalls[idx];
|
|
421
|
+
const inputStr = typeof tc.input === "string" ? tc.input : JSON.stringify(tc.input);
|
|
422
|
+
const truncated = inputStr.length > 100 ? `${inputStr.slice(0, 100)}…` : inputStr;
|
|
423
|
+
line += ` ${truncated}`;
|
|
424
|
+
}
|
|
425
|
+
return line;
|
|
426
|
+
});
|
|
427
|
+
entries.push({
|
|
428
|
+
key: "ai.tools",
|
|
429
|
+
value: "",
|
|
430
|
+
children
|
|
431
|
+
});
|
|
432
|
+
} else if (toolCalls?.length) if (hasInputs) {
|
|
433
|
+
const children = toolCalls.map((tc) => {
|
|
434
|
+
const inputStr = typeof tc.input === "string" ? tc.input : JSON.stringify(tc.input);
|
|
435
|
+
const truncated = inputStr.length > 100 ? `${inputStr.slice(0, 100)}…` : inputStr;
|
|
436
|
+
return `${tc.name}(${truncated})`;
|
|
437
|
+
});
|
|
438
|
+
entries.push({
|
|
439
|
+
key: "ai.tools",
|
|
440
|
+
value: "",
|
|
441
|
+
children
|
|
442
|
+
});
|
|
443
|
+
} else entries.push({
|
|
444
|
+
key: "ai.tools",
|
|
445
|
+
value: toolCalls.join(", ")
|
|
446
|
+
});
|
|
447
|
+
const stepsUsage = ai.stepsUsage;
|
|
448
|
+
if (stepsUsage?.length) {
|
|
449
|
+
const allSameModel = stepsUsage.every((s) => s.model === stepsUsage[0].model);
|
|
450
|
+
const children = stepsUsage.map((s) => {
|
|
451
|
+
let line = `${allSameModel ? "" : `${s.model} `}${s.inputTokens} in → ${s.outputTokens} out`;
|
|
452
|
+
const stepTools = s.toolCalls;
|
|
453
|
+
if (stepTools?.length) line += ` [${stepTools.join(", ")}]`;
|
|
454
|
+
return line;
|
|
455
|
+
});
|
|
456
|
+
entries.push({
|
|
457
|
+
key: "ai.steps",
|
|
458
|
+
value: "",
|
|
459
|
+
children
|
|
460
|
+
});
|
|
461
|
+
} else if (ai.steps && ai.steps > 1) entries.push({
|
|
462
|
+
key: "ai.steps",
|
|
463
|
+
value: String(ai.steps)
|
|
464
|
+
});
|
|
465
|
+
const embedding = ai.embedding;
|
|
466
|
+
if (embedding) {
|
|
467
|
+
const parts = [];
|
|
468
|
+
if (embedding.model) parts.push(String(embedding.model));
|
|
469
|
+
parts.push(`${embedding.tokens} tokens`);
|
|
470
|
+
if (embedding.dimensions) parts.push(`${embedding.dimensions}d`);
|
|
471
|
+
if (embedding.count) parts.push(`${embedding.count} items`);
|
|
472
|
+
entries.push({
|
|
473
|
+
key: "ai.embedding",
|
|
474
|
+
value: parts.join(" · ")
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
if (ai.finishReason) entries.push({
|
|
478
|
+
key: "ai.finishReason",
|
|
479
|
+
value: String(ai.finishReason)
|
|
480
|
+
});
|
|
481
|
+
if (ai.error) entries.push({
|
|
482
|
+
key: "ai.error",
|
|
483
|
+
value: String(ai.error)
|
|
484
|
+
});
|
|
485
|
+
if (ai.responseId) entries.push({
|
|
486
|
+
key: "ai.responseId",
|
|
487
|
+
value: String(ai.responseId)
|
|
488
|
+
});
|
|
489
|
+
return entries;
|
|
490
|
+
}
|
|
491
|
+
function prettyPrintWideEvent(event) {
|
|
492
|
+
const { timestamp, level, service, environment, version, ...rest } = event;
|
|
493
|
+
const ts = timestamp.slice(11, 23);
|
|
494
|
+
const browser = isClient();
|
|
495
|
+
const parts = [];
|
|
496
|
+
const styles = [];
|
|
497
|
+
if (browser) {
|
|
498
|
+
const lc = getCssLevelColor(level);
|
|
499
|
+
parts.push(`%c${ts}%c %c${level.toUpperCase()}%c %c[${escapeFormatString(String(service))}]%c`);
|
|
500
|
+
styles.push(cssColors.dim, cssColors.reset, lc, cssColors.reset, cssColors.cyan, cssColors.reset);
|
|
501
|
+
} else {
|
|
502
|
+
const lc = getLevelColor(level);
|
|
503
|
+
parts.push(`${colors.dim}${ts}${colors.reset} ${lc}${level.toUpperCase()}${colors.reset} ${colors.cyan}[${service}]${colors.reset}`);
|
|
504
|
+
}
|
|
505
|
+
if (rest.method && rest.path) {
|
|
506
|
+
parts.push(browser ? ` ${escapeFormatString(String(rest.method))} ${escapeFormatString(String(rest.path))}` : ` ${rest.method} ${rest.path}`);
|
|
507
|
+
delete rest.method;
|
|
508
|
+
delete rest.path;
|
|
509
|
+
}
|
|
510
|
+
if (rest.status) {
|
|
511
|
+
const sc = browser ? rest.status >= 400 ? cssColors.red : cssColors.green : rest.status >= 400 ? colors.red : colors.green;
|
|
512
|
+
if (browser) {
|
|
513
|
+
parts.push(` %c${rest.status}%c`);
|
|
514
|
+
styles.push(sc, cssColors.reset);
|
|
515
|
+
} else parts.push(` ${sc}${rest.status}${colors.reset}`);
|
|
516
|
+
delete rest.status;
|
|
517
|
+
}
|
|
518
|
+
if (rest.duration) {
|
|
519
|
+
if (browser) {
|
|
520
|
+
parts.push(` %c${escapeFormatString(`in ${rest.duration}`)}%c`);
|
|
521
|
+
styles.push(cssColors.dim, cssColors.reset);
|
|
522
|
+
} else parts.push(` ${colors.dim}in ${rest.duration}${colors.reset}`);
|
|
523
|
+
delete rest.duration;
|
|
524
|
+
}
|
|
525
|
+
console.log(parts.join(""), ...styles);
|
|
526
|
+
const aiData = rest.ai;
|
|
527
|
+
if (aiData && typeof aiData === "object") delete rest.ai;
|
|
528
|
+
const restEntries = Object.entries(rest).filter(([_, v]) => v !== void 0);
|
|
529
|
+
const aiEntries = aiData ? buildAIEntries(aiData) : [];
|
|
530
|
+
const allEntries = [...restEntries.map(([key, value]) => ({
|
|
531
|
+
key,
|
|
532
|
+
value: formatValue(value)
|
|
533
|
+
})), ...aiEntries];
|
|
534
|
+
for (let i = 0; i < allEntries.length; i++) {
|
|
535
|
+
const entry = allEntries[i];
|
|
536
|
+
const hasChildren = entry.children && entry.children.length > 0;
|
|
537
|
+
const prefix = i === allEntries.length - 1 && !hasChildren ? "└─" : "├─";
|
|
538
|
+
if (browser) {
|
|
539
|
+
const val = entry.value ? ` ${escapeFormatString(entry.value)}` : "";
|
|
540
|
+
console.log(` %c${prefix}%c %c${escapeFormatString(entry.key)}:%c${val}`, cssColors.dim, cssColors.reset, cssColors.cyan, cssColors.reset);
|
|
541
|
+
} else {
|
|
542
|
+
const val = entry.value ? ` ${entry.value}` : "";
|
|
543
|
+
console.log(` ${colors.dim}${prefix}${colors.reset} ${colors.cyan}${entry.key}:${colors.reset}${val}`);
|
|
544
|
+
}
|
|
545
|
+
if (hasChildren) {
|
|
546
|
+
const connector = i === allEntries.length - 1 ? " " : "│";
|
|
547
|
+
for (let j = 0; j < entry.children.length; j++) {
|
|
548
|
+
const child = entry.children[j];
|
|
549
|
+
const childPrefix = j === entry.children.length - 1 ? "└─" : "├─";
|
|
550
|
+
if (browser) console.log(` %c${connector} ${childPrefix}%c ${escapeFormatString(child)}`, cssColors.dim, cssColors.reset);
|
|
551
|
+
else console.log(` ${colors.dim}${connector} ${childPrefix}${colors.reset} ${child}`);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
function createLogMethod(level) {
|
|
557
|
+
return function logMethod(tagOrEvent, message) {
|
|
558
|
+
if (typeof tagOrEvent === "string" && message !== void 0) emitTaggedLog(level, tagOrEvent, message);
|
|
559
|
+
else if (typeof tagOrEvent === "object") emitWideEvent(level, tagOrEvent);
|
|
560
|
+
else emitTaggedLog(level, "log", String(tagOrEvent));
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Simple logging API - as easy as console.log
|
|
565
|
+
*
|
|
566
|
+
* @example
|
|
567
|
+
* ```ts
|
|
568
|
+
* log.info('auth', 'User logged in')
|
|
569
|
+
* log.error({ action: 'payment', error: 'failed' })
|
|
570
|
+
* ```
|
|
571
|
+
*/
|
|
572
|
+
const _log = {
|
|
573
|
+
info: createLogMethod("info"),
|
|
574
|
+
error: createLogMethod("error"),
|
|
575
|
+
warn: createLogMethod("warn"),
|
|
576
|
+
debug: createLogMethod("debug")
|
|
577
|
+
};
|
|
578
|
+
const noopLogger = {
|
|
579
|
+
set() {},
|
|
580
|
+
error() {},
|
|
581
|
+
info() {},
|
|
582
|
+
warn() {},
|
|
583
|
+
emit() {
|
|
584
|
+
return null;
|
|
585
|
+
},
|
|
586
|
+
getContext() {
|
|
587
|
+
return {};
|
|
588
|
+
},
|
|
589
|
+
audit: Object.assign(() => {}, { deny: () => {} })
|
|
590
|
+
};
|
|
591
|
+
/**
|
|
592
|
+
* Create a scoped logger for building wide events.
|
|
593
|
+
* Use this for any context: workflows, jobs, scripts, queues, etc.
|
|
594
|
+
*
|
|
595
|
+
* After `emit()` (including when sampling returns `null`), the logger is sealed and
|
|
596
|
+
* further mutations log `[evlog]` warnings. Standalone loggers do not have `fork`;
|
|
597
|
+
* that method is only attached by supported framework integrations.
|
|
598
|
+
*
|
|
599
|
+
* @example
|
|
600
|
+
* ```ts
|
|
601
|
+
* const log = createLogger({ jobId: job.id, queue: 'emails' })
|
|
602
|
+
* log.set({ batch: { size: 50, processed: 12 } })
|
|
603
|
+
* log.emit()
|
|
604
|
+
* ```
|
|
605
|
+
*/
|
|
606
|
+
function createLogger(initialContext = {}, internalOptions) {
|
|
607
|
+
if (!globalEnabled) return noopLogger;
|
|
608
|
+
const deferDrain = internalOptions?._deferDrain ?? false;
|
|
609
|
+
const startTime = Date.now();
|
|
610
|
+
const context = { ...initialContext };
|
|
611
|
+
let hasError = false;
|
|
612
|
+
let hasWarn = false;
|
|
613
|
+
let emitted = false;
|
|
614
|
+
function addLog(level, message) {
|
|
615
|
+
if (!Array.isArray(context.requestLogs)) context.requestLogs = [];
|
|
616
|
+
context.requestLogs.push({
|
|
617
|
+
level,
|
|
618
|
+
message,
|
|
619
|
+
timestamp: isoNow()
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
const auditMethod = function audit(input) {
|
|
623
|
+
if (emitted) {
|
|
624
|
+
warnPostEmit("log.audit()", `Audit dropped: action=${input.action}.`);
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
const fields = buildAuditFields(input);
|
|
628
|
+
if (!isPlainObject(context.audit)) context.audit = fields;
|
|
629
|
+
else mergeInto(context.audit, fields);
|
|
630
|
+
context._auditForceKeep = true;
|
|
631
|
+
};
|
|
632
|
+
auditMethod.deny = function deny(reason, input) {
|
|
633
|
+
auditMethod({
|
|
634
|
+
...input,
|
|
635
|
+
outcome: "denied",
|
|
636
|
+
reason
|
|
637
|
+
});
|
|
638
|
+
};
|
|
639
|
+
return {
|
|
640
|
+
audit: auditMethod,
|
|
641
|
+
set(data) {
|
|
642
|
+
if (emitted) {
|
|
643
|
+
const keys = Object.keys(data);
|
|
644
|
+
warnPostEmit("log.set()", `Keys dropped: ${keys.length ? keys.join(", ") : "(empty)"}.`);
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
mergeInto(context, data);
|
|
648
|
+
},
|
|
649
|
+
error(error, errorContext) {
|
|
650
|
+
if (emitted) {
|
|
651
|
+
warnPostEmit("log.error()", `Keys dropped: ${(errorContext ? [...Object.keys(errorContext), "error"] : ["error"]).join(", ")}.`);
|
|
652
|
+
return;
|
|
653
|
+
}
|
|
654
|
+
hasError = true;
|
|
655
|
+
const err = typeof error === "string" ? new Error(error) : error;
|
|
656
|
+
if (errorContext) mergeInto(context, errorContext);
|
|
657
|
+
const errorObj = {
|
|
658
|
+
name: err.name,
|
|
659
|
+
message: err.message,
|
|
660
|
+
stack: err.stack
|
|
661
|
+
};
|
|
662
|
+
const errRecord = err;
|
|
663
|
+
for (const k of [
|
|
664
|
+
"status",
|
|
665
|
+
"statusText",
|
|
666
|
+
"statusCode",
|
|
667
|
+
"statusMessage",
|
|
668
|
+
"data",
|
|
669
|
+
"cause",
|
|
670
|
+
"internal"
|
|
671
|
+
]) if (k in err) errorObj[k] = errRecord[k];
|
|
672
|
+
if (isPlainObject(context.error)) mergeInto(context.error, errorObj);
|
|
673
|
+
else context.error = errorObj;
|
|
674
|
+
},
|
|
675
|
+
info(message, infoContext) {
|
|
676
|
+
if (emitted) {
|
|
677
|
+
warnPostEmit("log.info()", `Keys dropped: ${(infoContext ? ["message", ...Object.keys(infoContext).filter((k) => k !== "requestLogs")] : ["message"]).join(", ")}.`);
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
addLog("info", message);
|
|
681
|
+
if (infoContext) {
|
|
682
|
+
const { requestLogs: _, ...rest } = infoContext;
|
|
683
|
+
mergeInto(context, rest);
|
|
684
|
+
}
|
|
685
|
+
},
|
|
686
|
+
warn(message, warnContext) {
|
|
687
|
+
if (emitted) {
|
|
688
|
+
warnPostEmit("log.warn()", `Keys dropped: ${(warnContext ? ["message", ...Object.keys(warnContext).filter((k) => k !== "requestLogs")] : ["message"]).join(", ")}.`);
|
|
689
|
+
return;
|
|
690
|
+
}
|
|
691
|
+
hasWarn = true;
|
|
692
|
+
addLog("warn", message);
|
|
693
|
+
if (warnContext) {
|
|
694
|
+
const { requestLogs: _, ...rest } = warnContext;
|
|
695
|
+
mergeInto(context, rest);
|
|
696
|
+
}
|
|
697
|
+
},
|
|
698
|
+
emit(overrides) {
|
|
699
|
+
if (emitted) {
|
|
700
|
+
warnPostEmit("log.emit()", "Ignoring duplicate emit.");
|
|
701
|
+
return null;
|
|
702
|
+
}
|
|
703
|
+
const durationMs = Date.now() - startTime;
|
|
704
|
+
const level = hasError ? "error" : hasWarn ? "warn" : "info";
|
|
705
|
+
let forceKeep = false;
|
|
706
|
+
if (overrides?._forceKeep) forceKeep = true;
|
|
707
|
+
else if (consumeAuditForceKeep(context)) forceKeep = true;
|
|
708
|
+
else if (globalSampling.keep?.length) forceKeep = shouldKeep({
|
|
709
|
+
status: overrides?.status ?? context.status,
|
|
710
|
+
duration: durationMs,
|
|
711
|
+
path: context.path,
|
|
712
|
+
method: context.method,
|
|
713
|
+
context
|
|
714
|
+
});
|
|
715
|
+
if (!forceKeep && !shouldSample(level)) {
|
|
716
|
+
emitted = true;
|
|
717
|
+
return null;
|
|
718
|
+
}
|
|
719
|
+
if (overrides) {
|
|
720
|
+
const obj = overrides;
|
|
721
|
+
for (const key in obj) if (key !== "_forceKeep") context[key] = obj[key];
|
|
722
|
+
}
|
|
723
|
+
context.duration = formatDuration(durationMs);
|
|
724
|
+
const wide = emitWideEvent(level, context, deferDrain, true);
|
|
725
|
+
emitted = true;
|
|
726
|
+
return wide;
|
|
727
|
+
},
|
|
728
|
+
getContext() {
|
|
729
|
+
return { ...context };
|
|
730
|
+
}
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
/**
|
|
734
|
+
* Create a request-scoped logger for building wide events.
|
|
735
|
+
* Convenience wrapper around `createLogger` that pre-populates HTTP request fields.
|
|
736
|
+
*
|
|
737
|
+
* @example
|
|
738
|
+
* ```ts
|
|
739
|
+
* const log = createRequestLogger({ method: 'POST', path: '/checkout' })
|
|
740
|
+
* log.set({ user: { id: '123' } })
|
|
741
|
+
* log.set({ cart: { items: 3 } })
|
|
742
|
+
* log.emit()
|
|
743
|
+
* ```
|
|
744
|
+
*/
|
|
745
|
+
function createRequestLogger(options = {}, internalOptions) {
|
|
746
|
+
const initial = {};
|
|
747
|
+
if (options.method !== void 0) initial.method = options.method;
|
|
748
|
+
if (options.path !== void 0) initial.path = options.path;
|
|
749
|
+
if (options.requestId !== void 0) initial.requestId = options.requestId;
|
|
750
|
+
return createLogger(initial, internalOptions);
|
|
751
|
+
}
|
|
752
|
+
/**
|
|
753
|
+
* Get the current environment context.
|
|
754
|
+
*/
|
|
755
|
+
function getEnvironment() {
|
|
756
|
+
return { ...globalEnv };
|
|
757
|
+
}
|
|
758
|
+
if (typeof __EVLOG_CONFIG__ !== "undefined") initLogger(__EVLOG_CONFIG__);
|
|
759
|
+
//#endregion
|
|
760
|
+
//#region src/audit.ts
|
|
761
|
+
/**
|
|
762
|
+
* Current version of the audit envelope. Bumped when `AuditFields` evolves
|
|
763
|
+
* in a backward-incompatible way so downstream pipelines can branch on it.
|
|
764
|
+
*/
|
|
765
|
+
const AUDIT_SCHEMA_VERSION = 1;
|
|
766
|
+
/**
|
|
767
|
+
* @internal Stable JSON stringification with deterministic key order.
|
|
768
|
+
* Used by `idempotencyKey` and `hash-chain` so the same logical event always
|
|
769
|
+
* produces the same digest, regardless of how object keys were added.
|
|
770
|
+
*/
|
|
771
|
+
function stableStringify(value) {
|
|
772
|
+
if (value === null || typeof value !== "object") return JSON.stringify(value);
|
|
773
|
+
if (Array.isArray(value)) return `[${value.map(stableStringify).join(",")}]`;
|
|
774
|
+
return `{${Object.keys(value).sort().map((k) => `${JSON.stringify(k)}:${stableStringify(value[k])}`).join(",")}}`;
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* @internal Sync, isomorphic 32-bit FNV-1a. Used to derive the idempotency
|
|
778
|
+
* key without pulling `node:crypto` into the static import graph (which would
|
|
779
|
+
* break browser / Cloudflare Workers bundles that import `evlog` for types
|
|
780
|
+
* or shared utilities). Idempotency keys are dedup tokens, not security
|
|
781
|
+
* primitives — collision resistance at 128 bits is more than sufficient.
|
|
782
|
+
*/
|
|
783
|
+
function fnv1a32(input, seed) {
|
|
784
|
+
let h = seed >>> 0;
|
|
785
|
+
for (let i = 0; i < input.length; i++) {
|
|
786
|
+
h ^= input.charCodeAt(i) & 255;
|
|
787
|
+
h = Math.imul(h, 16777619) >>> 0;
|
|
788
|
+
}
|
|
789
|
+
return h >>> 0;
|
|
790
|
+
}
|
|
791
|
+
/**
|
|
792
|
+
* @internal Compute the deterministic idempotency key for an audit event.
|
|
793
|
+
* Includes `action`, `actor.{type,id}`, `target.{type,id}`, `outcome`, and
|
|
794
|
+
* `timestamp` rounded to the second so retries within the same second collapse.
|
|
795
|
+
*
|
|
796
|
+
* Uses four interleaved FNV-1a 32-bit hashes (128-bit output, 32 hex chars)
|
|
797
|
+
* so the implementation stays sync and isomorphic across Node, browsers,
|
|
798
|
+
* Bun, Deno, and Cloudflare Workers.
|
|
799
|
+
*/
|
|
800
|
+
function computeIdempotencyKey(audit, timestamp) {
|
|
801
|
+
const seconds = timestamp.slice(0, 19);
|
|
802
|
+
const payload = stableStringify({
|
|
803
|
+
action: audit.action,
|
|
804
|
+
actor: {
|
|
805
|
+
type: audit.actor.type,
|
|
806
|
+
id: audit.actor.id
|
|
807
|
+
},
|
|
808
|
+
target: audit.target ? {
|
|
809
|
+
type: audit.target.type,
|
|
810
|
+
id: audit.target.id
|
|
811
|
+
} : void 0,
|
|
812
|
+
outcome: audit.outcome,
|
|
813
|
+
timestamp: seconds
|
|
814
|
+
});
|
|
815
|
+
const a = fnv1a32(payload, 2166136261).toString(16).padStart(8, "0");
|
|
816
|
+
const b = fnv1a32(payload, 3735928559).toString(16).padStart(8, "0");
|
|
817
|
+
const c = fnv1a32(payload, 528734635).toString(16).padStart(8, "0");
|
|
818
|
+
const d = fnv1a32(payload, 1541459225).toString(16).padStart(8, "0");
|
|
819
|
+
return a + b + c + d;
|
|
820
|
+
}
|
|
821
|
+
/**
|
|
822
|
+
* Build a normalised {@link AuditFields} from caller input. Defaults:
|
|
823
|
+
* - `outcome` → `'success'`
|
|
824
|
+
* - `version` → {@link AUDIT_SCHEMA_VERSION}
|
|
825
|
+
*
|
|
826
|
+
* `idempotencyKey` is filled at emit time with the event timestamp so retries
|
|
827
|
+
* stay deterministic.
|
|
828
|
+
*/
|
|
829
|
+
function buildAuditFields(input) {
|
|
830
|
+
return {
|
|
831
|
+
action: input.action,
|
|
832
|
+
actor: input.actor,
|
|
833
|
+
target: input.target,
|
|
834
|
+
outcome: input.outcome ?? "success",
|
|
835
|
+
reason: input.reason,
|
|
836
|
+
changes: input.changes,
|
|
837
|
+
causationId: input.causationId,
|
|
838
|
+
correlationId: input.correlationId,
|
|
839
|
+
version: input.version ?? 1
|
|
840
|
+
};
|
|
841
|
+
}
|
|
842
|
+
/**
|
|
843
|
+
* @internal Test-collector hook installed by {@link mockAudit}. When set, every
|
|
844
|
+
* audit event flowing through `log.audit()` / `audit()` is also pushed to it.
|
|
845
|
+
*/
|
|
846
|
+
let _testCollector = null;
|
|
847
|
+
/** @internal Emit-time decoration: assign timestamp-based idempotency key. */
|
|
848
|
+
function decorateAudit(audit, timestamp) {
|
|
849
|
+
if (audit.idempotencyKey) return audit;
|
|
850
|
+
return {
|
|
851
|
+
...audit,
|
|
852
|
+
idempotencyKey: computeIdempotencyKey(audit, timestamp)
|
|
853
|
+
};
|
|
854
|
+
}
|
|
855
|
+
/**
|
|
856
|
+
* Add audit semantics to an existing {@link RequestLogger}.
|
|
857
|
+
*
|
|
858
|
+
* Mutates the logger in place by adding an `audit` method (with a `.deny()`
|
|
859
|
+
* sub-method). Strictly equivalent to calling `log.set({ audit: ... })` plus
|
|
860
|
+
* `_forceKeep` on emit. Idempotent: calling twice on the same logger only
|
|
861
|
+
* attaches the methods once.
|
|
862
|
+
*
|
|
863
|
+
* @example
|
|
864
|
+
* ```ts
|
|
865
|
+
* const log = withAuditMethods(createLogger())
|
|
866
|
+
* log.audit({
|
|
867
|
+
* action: 'invoice.refund',
|
|
868
|
+
* actor: { type: 'user', id: user.id },
|
|
869
|
+
* target: { type: 'invoice', id: 'inv_889' },
|
|
870
|
+
* })
|
|
871
|
+
* ```
|
|
872
|
+
*/
|
|
873
|
+
function withAuditMethods(logger) {
|
|
874
|
+
const target = logger;
|
|
875
|
+
if (target.audit) return target;
|
|
876
|
+
const audit = function audit(input) {
|
|
877
|
+
const fields = buildAuditFields(input);
|
|
878
|
+
target.set({ audit: fields });
|
|
879
|
+
markForceKeep(target);
|
|
880
|
+
};
|
|
881
|
+
audit.deny = function deny(reason, input) {
|
|
882
|
+
audit({
|
|
883
|
+
...input,
|
|
884
|
+
outcome: "denied",
|
|
885
|
+
reason
|
|
886
|
+
});
|
|
887
|
+
};
|
|
888
|
+
target.audit = audit;
|
|
889
|
+
return target;
|
|
890
|
+
}
|
|
891
|
+
/**
|
|
892
|
+
* @internal Mark a logger so its next `emit()` is force-kept past tail sampling.
|
|
893
|
+
* Implemented by stamping a hidden flag on the accumulated context which
|
|
894
|
+
* `emit()` reads via `_forceKeep`.
|
|
895
|
+
*/
|
|
896
|
+
function markForceKeep(logger) {
|
|
897
|
+
const ctx = logger.getContext();
|
|
898
|
+
ctx._auditForceKeep = true;
|
|
899
|
+
}
|
|
900
|
+
/**
|
|
901
|
+
* Standalone audit emitter for non-request contexts (jobs, scripts, CLIs).
|
|
902
|
+
*
|
|
903
|
+
* Creates a one-shot logger, sets the audit fields, and emits immediately.
|
|
904
|
+
* The event is force-kept past tail sampling. Returns the emitted wide event,
|
|
905
|
+
* or `null` if logging is globally disabled.
|
|
906
|
+
*
|
|
907
|
+
* @example
|
|
908
|
+
* ```ts
|
|
909
|
+
* import { audit } from 'evlog'
|
|
910
|
+
*
|
|
911
|
+
* audit({
|
|
912
|
+
* action: 'cron.cleanup',
|
|
913
|
+
* actor: { type: 'system', id: 'cron' },
|
|
914
|
+
* target: { type: 'job', id: 'cleanup-stale-sessions' },
|
|
915
|
+
* outcome: 'success',
|
|
916
|
+
* })
|
|
917
|
+
* ```
|
|
918
|
+
*/
|
|
919
|
+
function audit(input) {
|
|
920
|
+
const fields = buildAuditFields(input);
|
|
921
|
+
const wide = createLogger({ audit: fields }).emit({ _forceKeep: true });
|
|
922
|
+
_testCollector?.(fields, wide);
|
|
923
|
+
return wide;
|
|
924
|
+
}
|
|
925
|
+
/**
|
|
926
|
+
* Wrap a function so its outcome (success / failure / denied) is automatically
|
|
927
|
+
* audited.
|
|
928
|
+
*
|
|
929
|
+
* Behaviour:
|
|
930
|
+
* - If `fn` resolves, an audit event with `outcome: 'success'` is emitted.
|
|
931
|
+
* - If `fn` throws an `EvlogError` (or any error) with `status === 403`, the
|
|
932
|
+
* audit event is recorded as `'denied'` with the error message as `reason`.
|
|
933
|
+
* - Any other thrown error produces `outcome: 'failure'` and re-throws.
|
|
934
|
+
*
|
|
935
|
+
* Use {@link AuditDeniedError} to signal denial without an HTTP status.
|
|
936
|
+
*
|
|
937
|
+
* @example
|
|
938
|
+
* ```ts
|
|
939
|
+
* const refundInvoice = withAudit(
|
|
940
|
+
* { action: 'invoice.refund', target: (input) => ({ type: 'invoice', id: input.id }) },
|
|
941
|
+
* async (input: { id: string }, ctx: { actor: AuditActor }) => {
|
|
942
|
+
* await db.invoices.refund(input.id)
|
|
943
|
+
* }
|
|
944
|
+
* )
|
|
945
|
+
*
|
|
946
|
+
* await refundInvoice({ id: 'inv_889' }, { actor: { type: 'user', id: user.id } })
|
|
947
|
+
* ```
|
|
948
|
+
*/
|
|
949
|
+
function withAudit(options, fn) {
|
|
950
|
+
return async (input, ctx) => {
|
|
951
|
+
const target = typeof options.target === "function" ? options.target(input) : options.target;
|
|
952
|
+
try {
|
|
953
|
+
const result = await fn(input, ctx);
|
|
954
|
+
audit({
|
|
955
|
+
action: options.action,
|
|
956
|
+
actor: ctx.actor,
|
|
957
|
+
target,
|
|
958
|
+
outcome: "success",
|
|
959
|
+
causationId: ctx.causationId,
|
|
960
|
+
correlationId: ctx.correlationId
|
|
961
|
+
});
|
|
962
|
+
return result;
|
|
963
|
+
} catch (err) {
|
|
964
|
+
const error = err;
|
|
965
|
+
const status = error.status ?? error.statusCode;
|
|
966
|
+
const denied = err instanceof AuditDeniedError || status === 403;
|
|
967
|
+
audit({
|
|
968
|
+
action: options.action,
|
|
969
|
+
actor: ctx.actor,
|
|
970
|
+
target,
|
|
971
|
+
outcome: denied ? "denied" : "failure",
|
|
972
|
+
reason: error.message,
|
|
973
|
+
causationId: ctx.causationId,
|
|
974
|
+
correlationId: ctx.correlationId
|
|
975
|
+
});
|
|
976
|
+
throw err;
|
|
977
|
+
}
|
|
978
|
+
};
|
|
979
|
+
}
|
|
980
|
+
/**
|
|
981
|
+
* Throw inside a {@link withAudit} body to mark the action as `outcome: 'denied'`
|
|
982
|
+
* regardless of the underlying HTTP status. The constructor message becomes the
|
|
983
|
+
* audit `reason`.
|
|
984
|
+
*/
|
|
985
|
+
var AuditDeniedError = class extends Error {
|
|
986
|
+
constructor(reason) {
|
|
987
|
+
super(reason);
|
|
988
|
+
this.name = "AuditDeniedError";
|
|
989
|
+
}
|
|
990
|
+
};
|
|
991
|
+
/**
|
|
992
|
+
* Compute a compact, redact-aware diff between two objects for the
|
|
993
|
+
* `changes` field. Output is a JSON Patch-style array (RFC 6902 subset:
|
|
994
|
+
* `add`, `remove`, `replace`) — small enough to ship over the wire.
|
|
995
|
+
*
|
|
996
|
+
* Object keys whose name matches one of the `redactPaths` (dot-notation, e.g.
|
|
997
|
+
* `'user.password'`, `'card.cvv'`) are replaced with `'[REDACTED]'` so PII
|
|
998
|
+
* never leaks through the diff.
|
|
999
|
+
*
|
|
1000
|
+
* @example
|
|
1001
|
+
* ```ts
|
|
1002
|
+
* log.audit({
|
|
1003
|
+
* action: 'user.update',
|
|
1004
|
+
* actor: { type: 'user', id: user.id },
|
|
1005
|
+
* target: { type: 'user', id: 'usr_42' },
|
|
1006
|
+
* changes: auditDiff(before, after, { redactPaths: ['password'] }),
|
|
1007
|
+
* })
|
|
1008
|
+
* ```
|
|
1009
|
+
*/
|
|
1010
|
+
function auditDiff(before, after, options = {}) {
|
|
1011
|
+
const replacement = options.replacement ?? "[REDACTED]";
|
|
1012
|
+
const redactSet = new Set((options.redactPaths ?? []).map((p) => p));
|
|
1013
|
+
const patch = [];
|
|
1014
|
+
function isRedacted(path) {
|
|
1015
|
+
if (redactSet.size === 0) return false;
|
|
1016
|
+
if (redactSet.has(path)) return true;
|
|
1017
|
+
for (const p of redactSet) if (path.endsWith(`.${p}`)) return true;
|
|
1018
|
+
return false;
|
|
1019
|
+
}
|
|
1020
|
+
function diff(a, b, path) {
|
|
1021
|
+
if (a === b) return;
|
|
1022
|
+
if (a === void 0 && b !== void 0) {
|
|
1023
|
+
patch.push({
|
|
1024
|
+
op: "add",
|
|
1025
|
+
path: path || "/",
|
|
1026
|
+
value: redactValue(b, path)
|
|
1027
|
+
});
|
|
1028
|
+
return;
|
|
1029
|
+
}
|
|
1030
|
+
if (a !== void 0 && b === void 0) {
|
|
1031
|
+
patch.push({
|
|
1032
|
+
op: "remove",
|
|
1033
|
+
path: path || "/"
|
|
1034
|
+
});
|
|
1035
|
+
return;
|
|
1036
|
+
}
|
|
1037
|
+
if (a !== null && b !== null && typeof a === "object" && typeof b === "object" && !Array.isArray(a) && !Array.isArray(b)) {
|
|
1038
|
+
const keys = new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
1039
|
+
for (const key of keys) diff(a[key], b[key], `${path}/${key}`);
|
|
1040
|
+
return;
|
|
1041
|
+
}
|
|
1042
|
+
patch.push({
|
|
1043
|
+
op: "replace",
|
|
1044
|
+
path: path || "/",
|
|
1045
|
+
value: redactValue(b, path)
|
|
1046
|
+
});
|
|
1047
|
+
}
|
|
1048
|
+
function redactValue(value, path) {
|
|
1049
|
+
if (value === null || typeof value !== "object") {
|
|
1050
|
+
const segs = path.split("/").filter(Boolean);
|
|
1051
|
+
const last = segs[segs.length - 1];
|
|
1052
|
+
if (last && isRedacted(last)) return replacement;
|
|
1053
|
+
return value;
|
|
1054
|
+
}
|
|
1055
|
+
if (Array.isArray(value)) return value.map((v, i) => redactValue(v, `${path}/${i}`));
|
|
1056
|
+
const out = {};
|
|
1057
|
+
for (const [k, v] of Object.entries(value)) out[k] = isRedacted(k) ? replacement : redactValue(v, `${path}/${k}`);
|
|
1058
|
+
return out;
|
|
1059
|
+
}
|
|
1060
|
+
diff(before, after, "");
|
|
1061
|
+
const result = { patch };
|
|
1062
|
+
if (options.includeBefore) result.before = redactValue(before, "");
|
|
1063
|
+
if (options.includeAfter) result.after = redactValue(after, "");
|
|
1064
|
+
return result;
|
|
1065
|
+
}
|
|
1066
|
+
/**
|
|
1067
|
+
* Define a typed audit action with an optional fixed target type.
|
|
1068
|
+
*
|
|
1069
|
+
* Returns a curried helper that fills in the action name (and target shape
|
|
1070
|
+
* if provided) so call sites stay terse and the action set is discoverable
|
|
1071
|
+
* in one place.
|
|
1072
|
+
*
|
|
1073
|
+
* @example
|
|
1074
|
+
* ```ts
|
|
1075
|
+
* const refund = defineAuditAction('invoice.refund', { target: 'invoice' })
|
|
1076
|
+
*
|
|
1077
|
+
* log.audit(refund({
|
|
1078
|
+
* actor: { type: 'user', id: user.id },
|
|
1079
|
+
* target: { id: 'inv_889' }, // type inferred as 'invoice'
|
|
1080
|
+
* outcome: 'success',
|
|
1081
|
+
* }))
|
|
1082
|
+
* ```
|
|
1083
|
+
*/
|
|
1084
|
+
function defineAuditAction(action, options) {
|
|
1085
|
+
const targetType = options?.target;
|
|
1086
|
+
return (input) => {
|
|
1087
|
+
const merged = {
|
|
1088
|
+
...input,
|
|
1089
|
+
action
|
|
1090
|
+
};
|
|
1091
|
+
if (targetType && input.target && !input.target.type) merged.target = {
|
|
1092
|
+
...input.target,
|
|
1093
|
+
type: targetType
|
|
1094
|
+
};
|
|
1095
|
+
return merged;
|
|
1096
|
+
};
|
|
1097
|
+
}
|
|
1098
|
+
/**
|
|
1099
|
+
* Test helper that captures every audit event emitted while it is active.
|
|
1100
|
+
*
|
|
1101
|
+
* Returns `{ events, restore, expect }`:
|
|
1102
|
+
* - `events` — live array of captured `AuditFields`, populated as audits fire.
|
|
1103
|
+
* - `restore()` — uninstall the collector. Call from `afterEach()`.
|
|
1104
|
+
* - `expect.toIncludeAuditOf(matcher)` — assertion helper used inside `expect`
|
|
1105
|
+
* blocks, returns `true` if at least one captured event matches.
|
|
1106
|
+
*
|
|
1107
|
+
* Only captures audits going through `log.audit()` and the standalone
|
|
1108
|
+
* `audit()` function. Events emitted via raw `log.set({ audit })` skip the
|
|
1109
|
+
* collector by design — wrap them with `log.audit()` to make them visible to
|
|
1110
|
+
* tests.
|
|
1111
|
+
*
|
|
1112
|
+
* @example
|
|
1113
|
+
* ```ts
|
|
1114
|
+
* const captured = mockAudit()
|
|
1115
|
+
* await refundInvoice('inv_889')
|
|
1116
|
+
* expect(captured.events).toHaveLength(1)
|
|
1117
|
+
* expect(captured.toIncludeAuditOf({ action: 'invoice.refund' })).toBe(true)
|
|
1118
|
+
* captured.restore()
|
|
1119
|
+
* ```
|
|
1120
|
+
*/
|
|
1121
|
+
function mockAudit() {
|
|
1122
|
+
const events = [];
|
|
1123
|
+
const previous = _testCollector;
|
|
1124
|
+
_testCollector = (event) => {
|
|
1125
|
+
events.push(event);
|
|
1126
|
+
};
|
|
1127
|
+
return {
|
|
1128
|
+
events,
|
|
1129
|
+
restore() {
|
|
1130
|
+
_testCollector = previous;
|
|
1131
|
+
},
|
|
1132
|
+
toIncludeAuditOf(matcher) {
|
|
1133
|
+
return events.some((event) => matchesAudit(event, matcher));
|
|
1134
|
+
}
|
|
1135
|
+
};
|
|
1136
|
+
}
|
|
1137
|
+
function matchesAudit(event, matcher) {
|
|
1138
|
+
if (matcher.action !== void 0) {
|
|
1139
|
+
if (matcher.action instanceof RegExp) {
|
|
1140
|
+
if (!matcher.action.test(event.action)) return false;
|
|
1141
|
+
} else if (event.action !== matcher.action) return false;
|
|
1142
|
+
}
|
|
1143
|
+
if (matcher.outcome !== void 0 && event.outcome !== matcher.outcome) return false;
|
|
1144
|
+
if (matcher.actor) {
|
|
1145
|
+
for (const [k, v] of Object.entries(matcher.actor)) if (event.actor[k] !== v) return false;
|
|
1146
|
+
}
|
|
1147
|
+
if (matcher.target) {
|
|
1148
|
+
if (!event.target) return false;
|
|
1149
|
+
for (const [k, v] of Object.entries(matcher.target)) if (event.target[k] !== v) return false;
|
|
1150
|
+
}
|
|
1151
|
+
return true;
|
|
1152
|
+
}
|
|
1153
|
+
/**
|
|
1154
|
+
* @internal Hook used by `RequestLogger.emit()` to detect audit-driven
|
|
1155
|
+
* force-keep flags on the accumulated context. Returns whether the event was
|
|
1156
|
+
* marked by `log.audit()` and clears the flag.
|
|
1157
|
+
*/
|
|
1158
|
+
function consumeAuditForceKeep(context) {
|
|
1159
|
+
if (context._auditForceKeep) {
|
|
1160
|
+
delete context._auditForceKeep;
|
|
1161
|
+
return true;
|
|
1162
|
+
}
|
|
1163
|
+
if (context.audit) return true;
|
|
1164
|
+
return false;
|
|
1165
|
+
}
|
|
1166
|
+
/**
|
|
1167
|
+
* @internal Decorate the audit field on an event right before drain — fills
|
|
1168
|
+
* in the deterministic idempotency key. Called by the logger pipeline so
|
|
1169
|
+
* it works for both `log.audit()` and direct `log.set({ audit })` paths.
|
|
1170
|
+
*/
|
|
1171
|
+
function finalizeAudit(event) {
|
|
1172
|
+
const a = event.audit;
|
|
1173
|
+
if (!a) return;
|
|
1174
|
+
event.audit = decorateAudit(a, String(event.timestamp));
|
|
1175
|
+
}
|
|
1176
|
+
/**
|
|
1177
|
+
* Enrich audit-bearing wide events with request, runtime, and tenant context.
|
|
1178
|
+
*
|
|
1179
|
+
* Runs only when `event.audit` is present — every other event passes through
|
|
1180
|
+
* untouched. Populates:
|
|
1181
|
+
* - `event.audit.context.requestId` from `ctx.request.requestId`
|
|
1182
|
+
* - `event.audit.context.traceId` from `event.traceId`
|
|
1183
|
+
* - `event.audit.context.ip` from `x-forwarded-for` / `x-real-ip`
|
|
1184
|
+
* - `event.audit.context.userAgent` from `user-agent`
|
|
1185
|
+
* - `event.audit.context.tenantId` from `options.tenantId(ctx)`
|
|
1186
|
+
*
|
|
1187
|
+
* Optionally fills `event.audit.actor` from the better-auth bridge when the
|
|
1188
|
+
* caller did not provide one. Anything else (custom actor strategies,
|
|
1189
|
+
* extra context) belongs in a custom enricher — replace this one entirely.
|
|
1190
|
+
*/
|
|
1191
|
+
function auditEnricher(options = {}) {
|
|
1192
|
+
return async (ctx) => {
|
|
1193
|
+
const event = ctx.event;
|
|
1194
|
+
const a = event.audit;
|
|
1195
|
+
if (!a) return;
|
|
1196
|
+
const context = { ...a.context ?? {} };
|
|
1197
|
+
function setIfMissing(key, value) {
|
|
1198
|
+
if (value === void 0) return;
|
|
1199
|
+
if (options.overwrite || context[key] === void 0) context[key] = value;
|
|
1200
|
+
}
|
|
1201
|
+
setIfMissing("requestId", ctx.request?.requestId);
|
|
1202
|
+
setIfMissing("traceId", typeof event.traceId === "string" ? event.traceId : void 0);
|
|
1203
|
+
setIfMissing("ip", getHeader(ctx.headers, "x-forwarded-for")?.split(",")[0]?.trim() ?? getHeader(ctx.headers, "x-real-ip"));
|
|
1204
|
+
setIfMissing("userAgent", getHeader(ctx.headers, "user-agent"));
|
|
1205
|
+
if (options.tenantId) {
|
|
1206
|
+
const tid = options.tenantId(ctx);
|
|
1207
|
+
if (tid !== void 0) setIfMissing("tenantId", tid);
|
|
1208
|
+
}
|
|
1209
|
+
let { actor } = a;
|
|
1210
|
+
if (!actor && options.bridge) {
|
|
1211
|
+
const fromBridge = await options.bridge.getSession(ctx);
|
|
1212
|
+
if (fromBridge) actor = fromBridge;
|
|
1213
|
+
}
|
|
1214
|
+
event.audit = {
|
|
1215
|
+
...a,
|
|
1216
|
+
context,
|
|
1217
|
+
actor: actor ?? a.actor
|
|
1218
|
+
};
|
|
1219
|
+
};
|
|
1220
|
+
}
|
|
1221
|
+
function getHeader(headers, name) {
|
|
1222
|
+
if (!headers) return void 0;
|
|
1223
|
+
if (headers[name] !== void 0) return headers[name];
|
|
1224
|
+
const lower = name.toLowerCase();
|
|
1225
|
+
if (headers[lower] !== void 0) return headers[lower];
|
|
1226
|
+
for (const [k, v] of Object.entries(headers)) if (k.toLowerCase() === lower) return v;
|
|
1227
|
+
}
|
|
1228
|
+
/**
|
|
1229
|
+
* Wrap any drain so it only receives events that carry an `audit` field.
|
|
1230
|
+
*
|
|
1231
|
+
* Use to route audit events to dedicated storage (separate Axiom dataset,
|
|
1232
|
+
* append-only Postgres table, FS journal) without affecting your main drain.
|
|
1233
|
+
*
|
|
1234
|
+
* Per-sink failure isolation comes from `initLogger({ drain: [...] })`: each
|
|
1235
|
+
* drain in the array is invoked independently, so a crashed Axiom call never
|
|
1236
|
+
* blocks the FS audit drain.
|
|
1237
|
+
*
|
|
1238
|
+
* @example
|
|
1239
|
+
* ```ts
|
|
1240
|
+
* import { initLogger, auditOnly } from 'evlog'
|
|
1241
|
+
* import { createAxiomDrain } from 'evlog/axiom'
|
|
1242
|
+
* import { createFsDrain } from 'evlog/fs'
|
|
1243
|
+
*
|
|
1244
|
+
* initLogger({
|
|
1245
|
+
* drain: [
|
|
1246
|
+
* createAxiomDrain({ dataset: 'logs' }),
|
|
1247
|
+
* auditOnly(createFsDrain({ dir: '.audit' }), { await: true }),
|
|
1248
|
+
* ],
|
|
1249
|
+
* })
|
|
1250
|
+
* ```
|
|
1251
|
+
*/
|
|
1252
|
+
function auditOnly(drain, options = {}) {
|
|
1253
|
+
return async (ctx) => {
|
|
1254
|
+
if (!ctx.event.audit) return;
|
|
1255
|
+
if (options.await) {
|
|
1256
|
+
await drain(ctx);
|
|
1257
|
+
return;
|
|
1258
|
+
}
|
|
1259
|
+
drain(ctx);
|
|
1260
|
+
};
|
|
1261
|
+
}
|
|
1262
|
+
/**
|
|
1263
|
+
* Wrap a drain so every event passing through gains tamper-evident integrity.
|
|
1264
|
+
*
|
|
1265
|
+
* - `'hmac'` — adds `event.audit.signature` (HMAC of the canonical event).
|
|
1266
|
+
* - `'hash-chain'` — adds `event.audit.prevHash` and `event.audit.hash` so the
|
|
1267
|
+
* sequence of events forms a verifiable chain. State persists in memory
|
|
1268
|
+
* by default; pass a `state: { load, save }` for cross-process / durable
|
|
1269
|
+
* chains (Redis, file, Postgres).
|
|
1270
|
+
*
|
|
1271
|
+
* The signature is computed before the event is forwarded to the wrapped
|
|
1272
|
+
* drain — combine with {@link auditOnly} when you only want integrity for
|
|
1273
|
+
* audit events.
|
|
1274
|
+
*
|
|
1275
|
+
* @example
|
|
1276
|
+
* ```ts
|
|
1277
|
+
* import { initLogger, auditOnly, signed } from 'evlog'
|
|
1278
|
+
* import { createFsDrain } from 'evlog/fs'
|
|
1279
|
+
*
|
|
1280
|
+
* initLogger({
|
|
1281
|
+
* drain: auditOnly(
|
|
1282
|
+
* signed(createFsDrain({ dir: '.audit' }), { strategy: 'hash-chain' }),
|
|
1283
|
+
* { await: true },
|
|
1284
|
+
* ),
|
|
1285
|
+
* })
|
|
1286
|
+
* ```
|
|
1287
|
+
*/
|
|
1288
|
+
function signed(drain, options) {
|
|
1289
|
+
if (options.strategy === "hmac") {
|
|
1290
|
+
const algorithm = options.algorithm ?? "sha256";
|
|
1291
|
+
const { secret } = options;
|
|
1292
|
+
return async (ctx) => {
|
|
1293
|
+
const a = ctx.event.audit;
|
|
1294
|
+
if (a) {
|
|
1295
|
+
const signature = await hmacHex(algorithm, secret, stableStringify(stripIntegrity(ctx.event)));
|
|
1296
|
+
ctx.event.audit = {
|
|
1297
|
+
...a,
|
|
1298
|
+
signature
|
|
1299
|
+
};
|
|
1300
|
+
}
|
|
1301
|
+
await drain(ctx);
|
|
1302
|
+
};
|
|
1303
|
+
}
|
|
1304
|
+
const algorithm = options.algorithm ?? "sha256";
|
|
1305
|
+
const { state } = options;
|
|
1306
|
+
let inMemoryPrev = null;
|
|
1307
|
+
let initialised = !state;
|
|
1308
|
+
let queue = Promise.resolve();
|
|
1309
|
+
return (ctx) => {
|
|
1310
|
+
queue = queue.then(async () => {
|
|
1311
|
+
const a = ctx.event.audit;
|
|
1312
|
+
if (a) {
|
|
1313
|
+
if (!initialised && state) {
|
|
1314
|
+
inMemoryPrev = await state.load() ?? null;
|
|
1315
|
+
initialised = true;
|
|
1316
|
+
}
|
|
1317
|
+
const prevHash = inMemoryPrev ?? void 0;
|
|
1318
|
+
const hash = await digestHex(algorithm, stableStringify({
|
|
1319
|
+
...stripIntegrity(ctx.event),
|
|
1320
|
+
audit: {
|
|
1321
|
+
...stripIntegrity(ctx.event).audit,
|
|
1322
|
+
prevHash
|
|
1323
|
+
}
|
|
1324
|
+
}));
|
|
1325
|
+
ctx.event.audit = {
|
|
1326
|
+
...a,
|
|
1327
|
+
prevHash,
|
|
1328
|
+
hash
|
|
1329
|
+
};
|
|
1330
|
+
inMemoryPrev = hash;
|
|
1331
|
+
await state?.save(hash);
|
|
1332
|
+
}
|
|
1333
|
+
await drain(ctx);
|
|
1334
|
+
}).catch((err) => {
|
|
1335
|
+
console.error("[evlog/audit] signed drain failed:", err);
|
|
1336
|
+
});
|
|
1337
|
+
return queue;
|
|
1338
|
+
};
|
|
1339
|
+
}
|
|
1340
|
+
/**
|
|
1341
|
+
* @internal Resolve the Web Crypto SubtleCrypto interface. Available natively
|
|
1342
|
+
* in browsers, Node 19+, Bun, Deno, and Cloudflare Workers. Falls back to
|
|
1343
|
+
* Node's `webcrypto` for Node 18 (where `globalThis.crypto` is gated behind
|
|
1344
|
+
* a flag). The dynamic import keeps `node:crypto` out of browser bundles.
|
|
1345
|
+
*/
|
|
1346
|
+
async function getSubtle() {
|
|
1347
|
+
const c = globalThis.crypto;
|
|
1348
|
+
if (c?.subtle) return c.subtle;
|
|
1349
|
+
return (await import(
|
|
1350
|
+
/* @vite-ignore */
|
|
1351
|
+
"node:crypto"
|
|
1352
|
+
)).webcrypto.subtle;
|
|
1353
|
+
}
|
|
1354
|
+
function normalizeAlgo(algorithm) {
|
|
1355
|
+
switch (algorithm.toLowerCase()) {
|
|
1356
|
+
case "sha1":
|
|
1357
|
+
case "sha-1": return "SHA-1";
|
|
1358
|
+
case "sha256":
|
|
1359
|
+
case "sha-256": return "SHA-256";
|
|
1360
|
+
case "sha384":
|
|
1361
|
+
case "sha-384": return "SHA-384";
|
|
1362
|
+
case "sha512":
|
|
1363
|
+
case "sha-512": return "SHA-512";
|
|
1364
|
+
default: return "SHA-256";
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
function bufToHex(buf) {
|
|
1368
|
+
let out = "";
|
|
1369
|
+
for (const byte of new Uint8Array(buf)) out += byte.toString(16).padStart(2, "0");
|
|
1370
|
+
return out;
|
|
1371
|
+
}
|
|
1372
|
+
async function digestHex(algorithm, data) {
|
|
1373
|
+
return bufToHex(await (await getSubtle()).digest(normalizeAlgo(algorithm), new TextEncoder().encode(data)));
|
|
1374
|
+
}
|
|
1375
|
+
async function hmacHex(algorithm, secret, data) {
|
|
1376
|
+
const subtle = await getSubtle();
|
|
1377
|
+
const hash = normalizeAlgo(algorithm);
|
|
1378
|
+
const key = await subtle.importKey("raw", new TextEncoder().encode(secret), {
|
|
1379
|
+
name: "HMAC",
|
|
1380
|
+
hash
|
|
1381
|
+
}, false, ["sign"]);
|
|
1382
|
+
return bufToHex(await subtle.sign("HMAC", key, new TextEncoder().encode(data)));
|
|
1383
|
+
}
|
|
1384
|
+
/** @internal Strip integrity fields before hashing so signatures stay stable. */
|
|
1385
|
+
function stripIntegrity(event) {
|
|
1386
|
+
const a = event.audit;
|
|
1387
|
+
if (!a) return event;
|
|
1388
|
+
const { signature, prevHash, hash, ...rest } = a;
|
|
1389
|
+
return {
|
|
1390
|
+
...event,
|
|
1391
|
+
audit: rest
|
|
1392
|
+
};
|
|
1393
|
+
}
|
|
1394
|
+
/**
|
|
1395
|
+
* Strict redact preset for audit events.
|
|
1396
|
+
*
|
|
1397
|
+
* Combine with the user's existing redact configuration via spread:
|
|
1398
|
+
* `initLogger({ redact: { paths: [...auditRedactPreset.paths!, ...mine] } })`.
|
|
1399
|
+
*
|
|
1400
|
+
* Hardens PII handling:
|
|
1401
|
+
* - Drops `Authorization` and `Cookie` headers anywhere they appear.
|
|
1402
|
+
* - Drops common credential field names (`password`, `passwordHash`, `token`,
|
|
1403
|
+
* `apiKey`, `secret`, `accessToken`, `refreshToken`, `cardNumber`, `cvv`,
|
|
1404
|
+
* `ssn`).
|
|
1405
|
+
*
|
|
1406
|
+
* Built-in pattern maskers (email, credit card, …) keep their default
|
|
1407
|
+
* behaviour — partial masking, not full redaction — so audit trails retain
|
|
1408
|
+
* enough signal to be useful.
|
|
1409
|
+
*/
|
|
1410
|
+
const auditRedactPreset = { paths: [
|
|
1411
|
+
"audit.changes.before.password",
|
|
1412
|
+
"audit.changes.before.passwordHash",
|
|
1413
|
+
"audit.changes.before.token",
|
|
1414
|
+
"audit.changes.before.apiKey",
|
|
1415
|
+
"audit.changes.before.secret",
|
|
1416
|
+
"audit.changes.before.accessToken",
|
|
1417
|
+
"audit.changes.before.refreshToken",
|
|
1418
|
+
"audit.changes.before.cardNumber",
|
|
1419
|
+
"audit.changes.before.cvv",
|
|
1420
|
+
"audit.changes.before.ssn",
|
|
1421
|
+
"audit.changes.after.password",
|
|
1422
|
+
"audit.changes.after.passwordHash",
|
|
1423
|
+
"audit.changes.after.token",
|
|
1424
|
+
"audit.changes.after.apiKey",
|
|
1425
|
+
"audit.changes.after.secret",
|
|
1426
|
+
"audit.changes.after.accessToken",
|
|
1427
|
+
"audit.changes.after.refreshToken",
|
|
1428
|
+
"audit.changes.after.cardNumber",
|
|
1429
|
+
"audit.changes.after.cvv",
|
|
1430
|
+
"audit.changes.after.ssn",
|
|
1431
|
+
"headers.authorization",
|
|
1432
|
+
"headers.cookie",
|
|
1433
|
+
"headers.set-cookie",
|
|
1434
|
+
"audit.context.headers.authorization",
|
|
1435
|
+
"audit.context.headers.cookie"
|
|
1436
|
+
] };
|
|
1437
|
+
//#endregion
|
|
1438
|
+
export { shouldKeep as C, resolveRedactConfig as E, lockLogger as S, redactEvent as T, getEnvironment as _, auditEnricher as a, isEnabled as b, buildAuditFields as c, signed as d, withAudit as f, createRequestLogger as g, createLogger as h, auditDiff as i, defineAuditAction as l, _log as m, AuditDeniedError as n, auditOnly as o, withAuditMethods as p, audit as r, auditRedactPreset as s, AUDIT_SCHEMA_VERSION as t, mockAudit as u, getGlobalDrain as v, normalizeRedactConfig as w, isLoggerLocked as x, initLogger as y };
|
|
1439
|
+
|
|
1440
|
+
//# sourceMappingURL=audit-d9esRZOK.mjs.map
|