tabletcommand-logger 3.2.7 → 4.0.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/.cspell/project-words.txt +28 -0
- package/CLAUDE.md +150 -0
- package/README.md +111 -1
- package/build/index.js +11 -3
- package/build/index.js.map +1 -1
- package/build/logger-types.js +3 -0
- package/build/logger-types.js.map +1 -0
- package/build/middleware/logger.js +94 -149
- package/build/middleware/logger.js.map +1 -1
- package/build/middleware/status-logger.js.map +1 -1
- package/build/otel.js +54 -0
- package/build/otel.js.map +1 -0
- package/build/pino-logger.js +97 -0
- package/build/pino-logger.js.map +1 -0
- package/build/redaction.js +351 -0
- package/build/redaction.js.map +1 -0
- package/build/test/logger-factory.js +207 -0
- package/build/test/logger-factory.js.map +1 -0
- package/build/test/middleware-logger.js +282 -0
- package/build/test/middleware-logger.js.map +1 -1
- package/build/test/middleware-status-logger.js.map +1 -1
- package/build/test/otel-mixin.js +139 -0
- package/build/test/otel-mixin.js.map +1 -0
- package/build/test/redaction.js +293 -0
- package/build/test/redaction.js.map +1 -0
- package/build/test/signature.js +111 -0
- package/build/test/signature.js.map +1 -0
- package/build/winston-logger.js +18 -47
- package/build/winston-logger.js.map +1 -1
- package/definitions/index.d.ts +5 -2
- package/definitions/index.d.ts.map +1 -1
- package/definitions/logger-types.d.ts +38 -0
- package/definitions/logger-types.d.ts.map +1 -0
- package/definitions/middleware/logger.d.ts +4 -6
- package/definitions/middleware/logger.d.ts.map +1 -1
- package/definitions/middleware/status-logger.d.ts +2 -2
- package/definitions/middleware/status-logger.d.ts.map +1 -1
- package/definitions/otel.d.ts +8 -0
- package/definitions/otel.d.ts.map +1 -0
- package/definitions/pino-logger.d.ts +6 -0
- package/definitions/pino-logger.d.ts.map +1 -0
- package/definitions/redaction.d.ts +25 -0
- package/definitions/redaction.d.ts.map +1 -0
- package/definitions/test/logger-factory.d.ts +2 -0
- package/definitions/test/logger-factory.d.ts.map +1 -0
- package/definitions/test/otel-mixin.d.ts +2 -0
- package/definitions/test/otel-mixin.d.ts.map +1 -0
- package/definitions/test/redaction.d.ts +2 -0
- package/definitions/test/redaction.d.ts.map +1 -0
- package/definitions/test/signature.d.ts +2 -0
- package/definitions/test/signature.d.ts.map +1 -0
- package/definitions/winston-logger.d.ts +2 -13
- package/definitions/winston-logger.d.ts.map +1 -1
- package/eslint.config.mjs +1 -0
- package/package.json +17 -5
- package/src/index.ts +6 -3
- package/src/logger-types.ts +50 -0
- package/src/middleware/logger.ts +101 -164
- package/src/middleware/status-logger.ts +2 -4
- package/src/otel.ts +57 -0
- package/src/pino-logger.ts +110 -0
- package/src/redaction.ts +368 -0
- package/src/test/logger-factory.ts +216 -0
- package/src/test/middleware-logger.ts +321 -2
- package/src/test/middleware-status-logger.ts +2 -2
- package/src/test/otel-mixin.ts +119 -0
- package/src/test/redaction.ts +409 -0
- package/src/test/signature.ts +126 -0
- package/src/winston-logger.ts +2 -72
- package/tabletcommand-logger-4.0.0.tgz +0 -0
package/src/redaction.ts
ADDED
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
import _ from "lodash";
|
|
2
|
+
import { Request } from "express";
|
|
3
|
+
import { Query } from "express-serve-static-core";
|
|
4
|
+
|
|
5
|
+
// Each of these authenticates a request on its own, so a log line that keeps the whole
|
|
6
|
+
// value is a live credential. The header stays, cut to a 7-character prefix: enough to
|
|
7
|
+
// tell two keys apart, not enough to replay one. Node lowercases incoming header names.
|
|
8
|
+
export const keyHeaders = [
|
|
9
|
+
"authorization",
|
|
10
|
+
"apikey",
|
|
11
|
+
"personnelapikey",
|
|
12
|
+
"x-tc-auth-token",
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
// tabletcommand-session reads each of these from the query string when the header is
|
|
16
|
+
// absent, and accepts either spelling. signupKey only ever arrives in the query.
|
|
17
|
+
export const keyParams = [
|
|
18
|
+
"apikey",
|
|
19
|
+
"personnelapikey",
|
|
20
|
+
"signupkey",
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
// A route can wrap a key name in a longer parameter name, such as deviceApiKey, so match
|
|
24
|
+
// the name loosely. Cutting a parameter that holds no credential costs nothing.
|
|
25
|
+
export function isKeyParam(name: string): boolean {
|
|
26
|
+
const lower = decodeName(name).toLowerCase();
|
|
27
|
+
return keyParams.some((keyParam) => lower.includes(keyParam));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Express decodes the name before tabletcommand-session reads it, so api%6Bey
|
|
31
|
+
// authenticates as apikey and has to be matched the same way. decodeURIComponent throws
|
|
32
|
+
// on a malformed escape, and this runs in a res "finish" listener where a throw ends the
|
|
33
|
+
// process.
|
|
34
|
+
export function decodeName(name: string): string {
|
|
35
|
+
try {
|
|
36
|
+
return decodeURIComponent(name.replace(/\+/g, " "));
|
|
37
|
+
} catch {
|
|
38
|
+
return name;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const redacted = "<redacted>";
|
|
43
|
+
|
|
44
|
+
export function keepPrefix(value: string): string {
|
|
45
|
+
return value.substring(0, 7);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Every scheme this service accepts is a short word, such as Bearer or Basic.
|
|
49
|
+
const schemePattern = /^[A-Za-z]{1,20}$/;
|
|
50
|
+
|
|
51
|
+
// A scheme fills the whole prefix on its own, so keep it and take the prefix from the
|
|
52
|
+
// credential behind it. A first word that is not a scheme is part of the credential, so
|
|
53
|
+
// cut the value whole rather than keep that word.
|
|
54
|
+
export function redactAuthorization(value: string): string {
|
|
55
|
+
const [scheme, ...rest] = value.split(" ");
|
|
56
|
+
if (rest.length === 0 || !schemePattern.test(scheme)) {
|
|
57
|
+
return keepPrefix(value);
|
|
58
|
+
}
|
|
59
|
+
return `${scheme} ${keepPrefix(rest.join(" "))}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Cookie names answer "did the caller send a session at all", so they stay and the values go.
|
|
63
|
+
export function redactCookie(value: string): string {
|
|
64
|
+
return value
|
|
65
|
+
.split(";")
|
|
66
|
+
.map((pair) => pair.split("=")[0].trim())
|
|
67
|
+
.filter((name) => name !== "")
|
|
68
|
+
.map((name) => `${name}=${redacted}`)
|
|
69
|
+
.join("; ");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Set-Cookie header contains the cookie pair first, followed by optional attributes
|
|
73
|
+
// (e.g. "session=SECRET; Path=/; Domain=.tabletcommand.com; Max-Age=3600; HttpOnly; Secure; SameSite=None").
|
|
74
|
+
// Redact only the first pair's value while keeping attributes intact for security auditing.
|
|
75
|
+
export function redactSetCookie(value: string): string {
|
|
76
|
+
if (!value || value.trim() === "") {
|
|
77
|
+
return "";
|
|
78
|
+
}
|
|
79
|
+
const parts = value.split(";");
|
|
80
|
+
const first = parts[0].trim();
|
|
81
|
+
if (first === "") {
|
|
82
|
+
return "";
|
|
83
|
+
}
|
|
84
|
+
const equalsIndex = first.indexOf("=");
|
|
85
|
+
const firstRedacted =
|
|
86
|
+
equalsIndex < 0
|
|
87
|
+
? `${first}=${redacted}`
|
|
88
|
+
: `${first.substring(0, equalsIndex).trim()}=${redacted}`;
|
|
89
|
+
|
|
90
|
+
const attributes = parts
|
|
91
|
+
.slice(1)
|
|
92
|
+
.map((attr) => attr.trim())
|
|
93
|
+
.filter((attr) => attr !== "");
|
|
94
|
+
|
|
95
|
+
if (attributes.length === 0) {
|
|
96
|
+
return firstRedacted;
|
|
97
|
+
}
|
|
98
|
+
return [firstRedacted, ...attributes].join("; ");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function redactorFor(name: string): (value: string) => string {
|
|
102
|
+
if (name === "cookie") {
|
|
103
|
+
return redactCookie;
|
|
104
|
+
}
|
|
105
|
+
if (name === "set-cookie") {
|
|
106
|
+
return redactSetCookie;
|
|
107
|
+
}
|
|
108
|
+
if (name === "authorization") {
|
|
109
|
+
return redactAuthorization;
|
|
110
|
+
}
|
|
111
|
+
return keepPrefix;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Node only gives a string or an array of strings, so anything else means other
|
|
115
|
+
// middleware wrote to req.headers. Drop it rather than throw: this runs in a res
|
|
116
|
+
// "finish" listener, where an exception ends the process.
|
|
117
|
+
export function redactHeaderValue(name: string, value: unknown): string | string[] {
|
|
118
|
+
const redact = redactorFor(name);
|
|
119
|
+
if (_.isArray(value)) {
|
|
120
|
+
return value.map((entry: unknown) => (_.isString(entry) ? redact(entry) : redacted));
|
|
121
|
+
}
|
|
122
|
+
if (!_.isString(value)) {
|
|
123
|
+
return redacted;
|
|
124
|
+
}
|
|
125
|
+
return redact(value);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function redactHeaders(headers: Request["headers"]): Request["headers"] {
|
|
129
|
+
const clean = { ...headers };
|
|
130
|
+
for (const [name, value] of Object.entries(clean)) {
|
|
131
|
+
const lower = name.toLowerCase();
|
|
132
|
+
const isCookie = lower === "cookie" || lower === "set-cookie";
|
|
133
|
+
if (_.isUndefined(value) || (!isCookie && !keyHeaders.includes(lower))) {
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
clean[name] = redactHeaderValue(lower, value);
|
|
137
|
+
}
|
|
138
|
+
return clean;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function redactQueryValue(value: Query[string]): Query[string] {
|
|
142
|
+
if (_.isString(value)) {
|
|
143
|
+
return keepPrefix(value);
|
|
144
|
+
}
|
|
145
|
+
if (_.isArray(value)) {
|
|
146
|
+
return value.map((entry: unknown) => (_.isString(entry) ? keepPrefix(entry) : redacted));
|
|
147
|
+
}
|
|
148
|
+
return redacted;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function redactQuery(query: Query): Query {
|
|
152
|
+
const clean: Query = { ...query };
|
|
153
|
+
for (const [name, value] of Object.entries(clean)) {
|
|
154
|
+
if (_.isUndefined(value) || !isKeyParam(name)) {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
clean[name] = redactQueryValue(value);
|
|
158
|
+
}
|
|
159
|
+
return clean;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function redactParamPair(pair: string): string {
|
|
163
|
+
const split = pair.indexOf("=");
|
|
164
|
+
if (split < 0 || !isKeyParam(pair.substring(0, split))) {
|
|
165
|
+
return pair;
|
|
166
|
+
}
|
|
167
|
+
return pair.substring(0, split + 1) + keepPrefix(pair.substring(split + 1));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// req.originalUrl is relative, so this splits the string rather than parsing a URL. A
|
|
171
|
+
// regex over the whole string backtracks for as long as the url, and the url comes from
|
|
172
|
+
// the caller. Splitting on "?" as well as "&" reaches a nested url in a parameter value.
|
|
173
|
+
export function redactOriginalURL(maybeURL?: string): string {
|
|
174
|
+
if (!_.isString(maybeURL) || maybeURL === "") {
|
|
175
|
+
return "";
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const queryStart = maybeURL.indexOf("?");
|
|
179
|
+
if (queryStart < 0) {
|
|
180
|
+
return maybeURL;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const path = maybeURL.substring(0, queryStart + 1);
|
|
184
|
+
const parts = maybeURL.substring(queryStart + 1).split(/([?&])/);
|
|
185
|
+
|
|
186
|
+
return path + parts.map((part, index) => (index % 2 === 0 ? redactParamPair(part) : part)).join("");
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Key names are compared lowercased with "-" and "_" removed, so authToken, auth_token,
|
|
190
|
+
// AUTH-TOKEN and x-api-key all match without listing each spelling. The names come from an
|
|
191
|
+
// audit of massive-tyrion and tabletcommand-backend-models (request bodies, model fields,
|
|
192
|
+
// outgoing request configs).
|
|
193
|
+
export function normalizeKey(key: string): string {
|
|
194
|
+
return key.toLowerCase().replace(/[-_]/g, "");
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Values under these keys go entirely: a prefix of a password, private key, SSN or door
|
|
198
|
+
// code gives away too much of it.
|
|
199
|
+
const fullKeyParts = [
|
|
200
|
+
"password",
|
|
201
|
+
"passwd",
|
|
202
|
+
"passphrase",
|
|
203
|
+
"secret",
|
|
204
|
+
"credential",
|
|
205
|
+
"privatekey",
|
|
206
|
+
"signingkey",
|
|
207
|
+
"encryptionkey",
|
|
208
|
+
];
|
|
209
|
+
const fullKeys = new Set([
|
|
210
|
+
"pass", // user password hash
|
|
211
|
+
"pwd",
|
|
212
|
+
"authpass",
|
|
213
|
+
"salt",
|
|
214
|
+
"ssn",
|
|
215
|
+
"dateofbirth",
|
|
216
|
+
"licensenumber",
|
|
217
|
+
"doorcode",
|
|
218
|
+
]);
|
|
219
|
+
|
|
220
|
+
// Values under these keys keep a 7-character prefix, enough to tell two keys apart.
|
|
221
|
+
const prefixKeyParts = [
|
|
222
|
+
"apikey", // apiKey, departmentApiKey, watchDutyApiKey, x-api-key, apiKeyReadOnly, ...
|
|
223
|
+
];
|
|
224
|
+
const prefixKeys = new Set([
|
|
225
|
+
"signupkey",
|
|
226
|
+
"partnerkey",
|
|
227
|
+
"xpartnerkey",
|
|
228
|
+
"cadkey",
|
|
229
|
+
"vendorkey",
|
|
230
|
+
"licensekey",
|
|
231
|
+
"licensecode",
|
|
232
|
+
"mark43key",
|
|
233
|
+
"authkey",
|
|
234
|
+
"accesscode",
|
|
235
|
+
"joincode",
|
|
236
|
+
"jwt",
|
|
237
|
+
"authheader",
|
|
238
|
+
"sessioncookie",
|
|
239
|
+
"readonlysessioncookie",
|
|
240
|
+
"authtokenplaintext",
|
|
241
|
+
]);
|
|
242
|
+
|
|
243
|
+
// Pagination cursors end in "token" but carry no credential.
|
|
244
|
+
const notSecretTokens = ["pagetoken", "paginationtoken"];
|
|
245
|
+
|
|
246
|
+
export type SecretKind = "full" | "prefix" | "authorization" | "cookie" | "set-cookie";
|
|
247
|
+
|
|
248
|
+
export function secretKind(key: string): SecretKind | null {
|
|
249
|
+
const name = normalizeKey(key);
|
|
250
|
+
if (name === "authorization") {
|
|
251
|
+
return "authorization";
|
|
252
|
+
}
|
|
253
|
+
if (name === "cookie" || name === "cookies") {
|
|
254
|
+
return "cookie";
|
|
255
|
+
}
|
|
256
|
+
if (name === "setcookie") {
|
|
257
|
+
return "set-cookie";
|
|
258
|
+
}
|
|
259
|
+
if (fullKeys.has(name) || fullKeyParts.some((part) => name.includes(part))) {
|
|
260
|
+
return "full";
|
|
261
|
+
}
|
|
262
|
+
if (prefixKeys.has(name) || prefixKeyParts.some((part) => name.includes(part))) {
|
|
263
|
+
return "prefix";
|
|
264
|
+
}
|
|
265
|
+
// accessToken, refresh_token, idToken, sessionToken, x-tc-auth-token, deviceToken, ...
|
|
266
|
+
if (name.endsWith("token") && !notSecretTokens.some((part) => name.endsWith(part))) {
|
|
267
|
+
return "prefix";
|
|
268
|
+
}
|
|
269
|
+
return null;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function redactString(kind: SecretKind, value: string): string {
|
|
273
|
+
switch (kind) {
|
|
274
|
+
case "full":
|
|
275
|
+
return redacted;
|
|
276
|
+
case "authorization":
|
|
277
|
+
return redactAuthorization(value);
|
|
278
|
+
case "cookie":
|
|
279
|
+
return redactCookie(value);
|
|
280
|
+
case "set-cookie":
|
|
281
|
+
return redactSetCookie(value);
|
|
282
|
+
default:
|
|
283
|
+
return keepPrefix(value);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// Booleans, numbers and null under a secret name are flags and settings (hasAuthSecret,
|
|
288
|
+
// minPasswordLength), not secrets, so they stay readable.
|
|
289
|
+
export function redactSecretValue(kind: SecretKind, value: unknown): unknown {
|
|
290
|
+
if (typeof value === "string") {
|
|
291
|
+
return redactString(kind, value);
|
|
292
|
+
}
|
|
293
|
+
if (Array.isArray(value)) {
|
|
294
|
+
return value.map((entry: unknown) => (typeof entry === "string" ? redactString(kind, entry) : redacted));
|
|
295
|
+
}
|
|
296
|
+
if (value === null || typeof value === "boolean" || typeof value === "number") {
|
|
297
|
+
return value;
|
|
298
|
+
}
|
|
299
|
+
return redacted;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Deeper than this the walk stops and leaves the value as it is. It bounds the cost of a
|
|
303
|
+
// large or cyclic object; real log payloads sit well inside it.
|
|
304
|
+
export const MAX_REDACT_DEPTH = 10;
|
|
305
|
+
|
|
306
|
+
function isPlainObject(value: object): value is Record<string, unknown> {
|
|
307
|
+
const proto: unknown = Object.getPrototypeOf(value);
|
|
308
|
+
return proto === Object.prototype || proto === null;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function hasToJSON(value: object): value is { toJSON: () => unknown } {
|
|
312
|
+
return typeof (value as { toJSON?: unknown }).toJSON === "function";
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function walk(value: unknown, depth: number): unknown {
|
|
316
|
+
if (value === null || typeof value !== "object" || depth > MAX_REDACT_DEPTH) {
|
|
317
|
+
return value;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// Mongoose documents, axios errors and the like serialize through toJSON, so redact
|
|
321
|
+
// what will actually be written. Other class instances (raw req/res, Error) are left
|
|
322
|
+
// to their serializers.
|
|
323
|
+
let source: unknown = value;
|
|
324
|
+
if (!Array.isArray(value) && !isPlainObject(value)) {
|
|
325
|
+
if (!hasToJSON(value) || value instanceof Date) {
|
|
326
|
+
return value;
|
|
327
|
+
}
|
|
328
|
+
source = value.toJSON();
|
|
329
|
+
if (source === null || typeof source !== "object") {
|
|
330
|
+
return source;
|
|
331
|
+
}
|
|
332
|
+
if (!Array.isArray(source) && !isPlainObject(source)) {
|
|
333
|
+
return source;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (Array.isArray(source)) {
|
|
338
|
+
let copy: unknown[] | null = null;
|
|
339
|
+
source.forEach((entry: unknown, index: number) => {
|
|
340
|
+
const next = walk(entry, depth + 1);
|
|
341
|
+
if (next !== entry) {
|
|
342
|
+
copy = copy || source.slice();
|
|
343
|
+
copy[index] = next;
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
return copy || source;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// Copy on write: the caller's object is never changed, and an object with nothing to
|
|
350
|
+
// redact is returned as it is.
|
|
351
|
+
const record = source as Record<string, unknown>;
|
|
352
|
+
let copy: Record<string, unknown> | null = null;
|
|
353
|
+
for (const key of Object.keys(record)) {
|
|
354
|
+
const entry = record[key];
|
|
355
|
+
const kind = secretKind(key);
|
|
356
|
+
const next = kind ? redactSecretValue(kind, entry) : walk(entry, depth + 1);
|
|
357
|
+
if (next !== entry) {
|
|
358
|
+
copy = copy || { ...record };
|
|
359
|
+
copy[key] = next;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return copy || source;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// Redacts every secret-named key in a log object, bindings or serialized value.
|
|
366
|
+
export function redactSecrets<T>(value: T): T {
|
|
367
|
+
return walk(value, 0) as T;
|
|
368
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { assert } from "chai";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import { Writable } from "node:stream";
|
|
4
|
+
import logger, { isPinoPrettyAvailable } from "../pino-logger";
|
|
5
|
+
|
|
6
|
+
describe("Logger Factory", () => {
|
|
7
|
+
function createCaptureStream(lines: Record<string, unknown>[]) {
|
|
8
|
+
return new Writable({
|
|
9
|
+
write(chunk: Buffer | string, _encoding: string, callback: (error?: Error | null) => void) {
|
|
10
|
+
try {
|
|
11
|
+
const str = chunk.toString();
|
|
12
|
+
const parsed = JSON.parse(str) as Record<string, unknown>;
|
|
13
|
+
lines.push(parsed);
|
|
14
|
+
} catch {
|
|
15
|
+
// ignore
|
|
16
|
+
}
|
|
17
|
+
callback();
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
it("creates a logger that outputs valid JSON with base service name", () => {
|
|
23
|
+
const lines: Record<string, unknown>[] = [];
|
|
24
|
+
const log = logger(
|
|
25
|
+
{
|
|
26
|
+
name: "test-service",
|
|
27
|
+
logToConsole: true,
|
|
28
|
+
},
|
|
29
|
+
createCaptureStream(lines)
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
assert.isNotNull(log);
|
|
33
|
+
log?.info({ action: "login" }, "user logged in");
|
|
34
|
+
|
|
35
|
+
assert.equal(lines.length, 1);
|
|
36
|
+
const line = lines[0];
|
|
37
|
+
assert.equal(line.service, "test-service");
|
|
38
|
+
assert.equal(line.level, "info");
|
|
39
|
+
assert.equal(line.msg, "user logged in");
|
|
40
|
+
assert.equal(line.action, "login");
|
|
41
|
+
assert.isString(line.time);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("formats level as string label", () => {
|
|
45
|
+
const lines: Record<string, unknown>[] = [];
|
|
46
|
+
const log = logger(
|
|
47
|
+
{
|
|
48
|
+
name: "test-service",
|
|
49
|
+
logToConsole: true,
|
|
50
|
+
},
|
|
51
|
+
createCaptureStream(lines)
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
assert.isNotNull(log);
|
|
55
|
+
log?.warn({ count: 5 }, "threshold warning");
|
|
56
|
+
log?.error(new Error("disk full"), "failure");
|
|
57
|
+
|
|
58
|
+
assert.equal(lines.length, 2);
|
|
59
|
+
assert.equal(lines[0].level, "warn");
|
|
60
|
+
assert.equal(lines[1].level, "error");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("includes extraMeta in the base fields", () => {
|
|
64
|
+
const lines: Record<string, unknown>[] = [];
|
|
65
|
+
const log = logger(
|
|
66
|
+
{
|
|
67
|
+
name: "test-service",
|
|
68
|
+
logToConsole: true,
|
|
69
|
+
extraMeta: {
|
|
70
|
+
environment: "qa",
|
|
71
|
+
cluster: "us-west-2",
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
createCaptureStream(lines)
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
assert.isNotNull(log);
|
|
78
|
+
log?.info("started");
|
|
79
|
+
|
|
80
|
+
assert.equal(lines.length, 1);
|
|
81
|
+
assert.equal(lines[0].environment, "qa");
|
|
82
|
+
assert.equal(lines[0].cluster, "us-west-2");
|
|
83
|
+
assert.equal(lines[0].service, "test-service");
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("respects custom log level override", () => {
|
|
87
|
+
const lines: Record<string, unknown>[] = [];
|
|
88
|
+
const log = logger(
|
|
89
|
+
{
|
|
90
|
+
name: "test-service",
|
|
91
|
+
logToConsole: true,
|
|
92
|
+
level: "warn",
|
|
93
|
+
},
|
|
94
|
+
createCaptureStream(lines)
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
assert.isNotNull(log);
|
|
98
|
+
log?.info("this should be filtered out");
|
|
99
|
+
log?.warn("this should be logged");
|
|
100
|
+
|
|
101
|
+
assert.equal(lines.length, 1);
|
|
102
|
+
assert.equal(lines[0].msg, "this should be logged");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("supports disabling pretty-printing via config.pretty: false", () => {
|
|
106
|
+
const lines: Record<string, unknown>[] = [];
|
|
107
|
+
const log = logger(
|
|
108
|
+
{
|
|
109
|
+
name: "test-service",
|
|
110
|
+
logToConsole: true,
|
|
111
|
+
pretty: false,
|
|
112
|
+
},
|
|
113
|
+
createCaptureStream(lines)
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
assert.isNotNull(log);
|
|
117
|
+
log?.info("ndjson output test");
|
|
118
|
+
assert.equal(lines.length, 1);
|
|
119
|
+
assert.equal(lines[0].msg, "ndjson output test");
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("supports disabling pretty-printing via PINO_PRETTY=false env variable", () => {
|
|
123
|
+
const origEnv = process.env.PINO_PRETTY;
|
|
124
|
+
process.env.PINO_PRETTY = "false";
|
|
125
|
+
try {
|
|
126
|
+
const lines: Record<string, unknown>[] = [];
|
|
127
|
+
const log = logger(
|
|
128
|
+
{
|
|
129
|
+
name: "test-service",
|
|
130
|
+
logToConsole: true,
|
|
131
|
+
},
|
|
132
|
+
createCaptureStream(lines)
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
assert.isNotNull(log);
|
|
136
|
+
log?.info("ndjson env test");
|
|
137
|
+
assert.equal(lines.length, 1);
|
|
138
|
+
assert.equal(lines[0].msg, "ndjson env test");
|
|
139
|
+
} finally {
|
|
140
|
+
if (origEnv !== undefined) {
|
|
141
|
+
process.env.PINO_PRETTY = origEnv;
|
|
142
|
+
} else {
|
|
143
|
+
delete process.env.PINO_PRETTY;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("exports isPinoPrettyAvailable helper and reports boolean", () => {
|
|
149
|
+
assert.isBoolean(isPinoPrettyAvailable());
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("handles config.pretty: true gracefully and returns a valid logger instance", () => {
|
|
153
|
+
const log = logger({
|
|
154
|
+
name: "pretty-test-service",
|
|
155
|
+
pretty: true,
|
|
156
|
+
});
|
|
157
|
+
assert.isNotNull(log);
|
|
158
|
+
assert.isFunction(log?.info);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("correctly enables logger or returns null based on transport config", () => {
|
|
162
|
+
// {logToConsole: true} -> logger
|
|
163
|
+
assert.isNotNull(logger({
|
|
164
|
+
name: "test-service",
|
|
165
|
+
logToConsole: true,
|
|
166
|
+
}));
|
|
167
|
+
|
|
168
|
+
// {logToConsole: false, logToFile: true} -> logger (regression guard)
|
|
169
|
+
assert.isNotNull(logger({
|
|
170
|
+
name: "test-service",
|
|
171
|
+
logToConsole: false,
|
|
172
|
+
logToFile: true,
|
|
173
|
+
}));
|
|
174
|
+
|
|
175
|
+
// {logToConsole: false, logToDatadog: true} -> logger (regression guard)
|
|
176
|
+
assert.isNotNull(logger({
|
|
177
|
+
name: "test-service",
|
|
178
|
+
logToConsole: false,
|
|
179
|
+
logToDatadog: true,
|
|
180
|
+
}));
|
|
181
|
+
|
|
182
|
+
// {name only} -> logger
|
|
183
|
+
assert.isNotNull(logger({
|
|
184
|
+
name: "test-service",
|
|
185
|
+
}));
|
|
186
|
+
|
|
187
|
+
// {logToConsole: false, logToFile: false, logToDatadog: false} -> null
|
|
188
|
+
assert.isNull(logger({
|
|
189
|
+
name: "test-service",
|
|
190
|
+
logToConsole: false,
|
|
191
|
+
logToFile: false,
|
|
192
|
+
logToDatadog: false,
|
|
193
|
+
}));
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it("child logger inherits service name and attaches child bindings", () => {
|
|
197
|
+
const lines: Record<string, unknown>[] = [];
|
|
198
|
+
const parent = logger(
|
|
199
|
+
{
|
|
200
|
+
name: "parent-service",
|
|
201
|
+
logToConsole: true,
|
|
202
|
+
},
|
|
203
|
+
createCaptureStream(lines)
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
assert.isNotNull(parent);
|
|
207
|
+
const child = parent?.child({ requestId: "req-123" });
|
|
208
|
+
child?.info({ data: 42 }, "processing request");
|
|
209
|
+
|
|
210
|
+
assert.equal(lines.length, 1);
|
|
211
|
+
assert.equal(lines[0].service, "parent-service");
|
|
212
|
+
assert.equal(lines[0].requestId, "req-123");
|
|
213
|
+
assert.equal(lines[0].data, 42);
|
|
214
|
+
assert.equal(lines[0].msg, "processing request");
|
|
215
|
+
});
|
|
216
|
+
});
|