Repository navigation
Expand file tree
/
Copy pathlogging.test.ts
More file actions
647 lines (571 loc) · 19.6 KB
/
Copy pathlogging.test.ts
File metadata and controls
647 lines (571 loc) · 19.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
import { describe, it, expect } from 'vitest';
import {
LogLevel,
LogFormat,
LoggerConfigSchema,
LogEntrySchema,
ExtendedLogLevel,
LogDestinationType,
ConsoleDestinationConfigSchema,
FileDestinationConfigSchema,
HttpDestinationConfigSchema,
ExternalServiceDestinationConfigSchema,
LogDestinationSchema,
LogEnrichmentConfigSchema,
StructuredLogEntrySchema,
LoggingConfigSchema,
type LoggerConfig,
type LogEntry,
type LogDestination,
type StructuredLogEntry,
type LoggingConfig,
} from './logging.zod';
describe('LogLevel', () => {
it('should accept valid log levels', () => {
const levels = ['debug', 'info', 'warn', 'error', 'fatal'];
levels.forEach((level) => {
expect(() => LogLevel.parse(level)).not.toThrow();
});
});
it('should reject invalid log levels', () => {
expect(() => LogLevel.parse('invalid')).toThrow();
expect(() => LogLevel.parse('trace')).toThrow(); // trace is only in ExtendedLogLevel
});
});
describe('LogFormat', () => {
it('should accept valid log formats', () => {
const formats = ['json', 'text', 'pretty'];
formats.forEach((format) => {
expect(() => LogFormat.parse(format)).not.toThrow();
});
});
it('should reject invalid log formats', () => {
expect(() => LogFormat.parse('invalid')).toThrow();
});
});
describe('LoggerConfigSchema', () => {
it('should accept minimal configuration', () => {
const config = LoggerConfigSchema.parse({});
expect(config.level).toBe('info');
expect(config.format).toBe('json');
expect(config.sourceLocation).toBe(false);
});
it('should accept full configuration', () => {
const config: LoggerConfig = {
name: 'my-logger',
level: 'debug',
format: 'pretty',
redact: ['apiKey', 'password'],
sourceLocation: true,
file: '/var/log/app.log',
rotation: {
maxSize: '50m',
maxFiles: 10,
},
};
expect(() => LoggerConfigSchema.parse(config)).not.toThrow();
});
it('should apply defaults', () => {
const config = LoggerConfigSchema.parse({});
expect(config.redact).toEqual(['password', 'token', 'secret', 'key']);
});
});
describe('LogEntrySchema', () => {
it('should accept minimal log entry', () => {
const entry: LogEntry = {
timestamp: '2024-01-15T10:30:00.000Z',
level: 'info',
message: 'Application started',
};
expect(() => LogEntrySchema.parse(entry)).not.toThrow();
});
it('should accept full log entry', () => {
const entry: LogEntry = {
timestamp: '2024-01-15T10:30:00.000Z',
level: 'error',
message: 'Database connection failed',
context: { database: 'postgres', attempt: 3 },
error: { message: 'Connection timeout' },
traceId: 'abc123',
spanId: 'def456',
service: 'api-server',
component: 'database-pool',
};
expect(() => LogEntrySchema.parse(entry)).not.toThrow();
});
});
describe('ExtendedLogLevel', () => {
it('should accept valid log levels', () => {
const levels = ['trace', 'debug', 'info', 'warn', 'error', 'fatal'];
levels.forEach((level) => {
expect(() => ExtendedLogLevel.parse(level)).not.toThrow();
});
});
it('should reject invalid log levels', () => {
expect(() => ExtendedLogLevel.parse('invalid')).toThrow();
});
});
describe('LogDestinationType', () => {
it('should accept valid destination types', () => {
const types = [
'console', 'file', 'syslog', 'elasticsearch', 'cloudwatch',
'stackdriver', 'azure_monitor', 'datadog', 'splunk', 'loki',
'http', 'kafka', 'redis', 'custom',
];
types.forEach((type) => {
expect(() => LogDestinationType.parse(type)).not.toThrow();
});
});
});
describe('ConsoleDestinationConfigSchema', () => {
it('should apply defaults', () => {
const config = ConsoleDestinationConfigSchema.parse({});
expect(config.stream).toBe('stdout');
expect(config.colors).toBe(true);
expect(config.prettyPrint).toBe(false);
});
it('should accept custom values', () => {
const config = ConsoleDestinationConfigSchema.parse({
stream: 'stderr',
colors: false,
prettyPrint: true,
});
expect(config.stream).toBe('stderr');
expect(config.colors).toBe(false);
expect(config.prettyPrint).toBe(true);
});
});
describe('FileDestinationConfigSchema', () => {
it('should accept valid file configuration', () => {
const config = FileDestinationConfigSchema.parse({
path: '/var/log/app.log',
});
expect(config.path).toBe('/var/log/app.log');
expect(config.encoding).toBe('utf8');
expect(config.append).toBe(true);
});
it('should accept rotation configuration', () => {
const config = FileDestinationConfigSchema.parse({
path: '/var/log/app.log',
rotation: {
maxSize: '50m',
maxFiles: 10,
compress: true,
interval: 'daily',
},
});
expect(config.rotation?.maxSize).toBe('50m');
expect(config.rotation?.maxFiles).toBe(10);
expect(config.rotation?.compress).toBe(true);
});
});
describe('HttpDestinationConfigSchema', () => {
it('should accept valid HTTP configuration', () => {
const config = HttpDestinationConfigSchema.parse({
url: 'https://logs.example.com/v1/logs',
});
expect(config.url).toBe('https://logs.example.com/v1/logs');
expect(config.method).toBe('POST');
// `timeout` → `timeoutMs` (#17782, ruling A on #15939 executing #14478).
// The 30000 default this pins is unchanged; only the key it is read under
// moved, so the pin follows the key rather than being dropped.
expect(config.timeoutMs).toBe(30000);
});
it('should accept authentication', () => {
const config = HttpDestinationConfigSchema.parse({
url: 'https://logs.example.com/v1/logs',
auth: {
type: 'bearer',
token: 'secret-token',
},
});
expect(config.auth?.type).toBe('bearer');
expect(config.auth?.token).toBe('secret-token');
});
it('should accept batch configuration', () => {
const config = HttpDestinationConfigSchema.parse({
url: 'https://logs.example.com/v1/logs',
batch: {
maxSize: 500,
// `batch.flushInterval` → `batch.flushIntervalMs` (#17782, ruling A on
// #15939 executing #14478). Same 10000 milliseconds, new key.
flushIntervalMs: 10000,
},
});
expect(config.batch?.maxSize).toBe(500);
expect(config.batch?.flushIntervalMs).toBe(10000);
});
});
describe('LogDestinationSchema', () => {
it('should accept valid console destination', () => {
const destination: LogDestination = {
name: 'console_output',
type: 'console',
console: {
stream: 'stdout',
},
};
expect(() => LogDestinationSchema.parse(destination)).not.toThrow();
});
it('should accept valid file destination', () => {
const destination: LogDestination = {
name: 'file_output',
type: 'file',
file: {
path: '/var/log/app.log',
},
};
expect(() => LogDestinationSchema.parse(destination)).not.toThrow();
});
it('should apply defaults', () => {
const destination = LogDestinationSchema.parse({
name: 'test_dest',
type: 'console',
});
expect(destination.level).toBe('info');
expect(destination.enabled).toBe(true);
expect(destination.format).toBe('json');
});
it('should enforce snake_case naming', () => {
expect(() => LogDestinationSchema.parse({
name: 'camelCase',
type: 'console',
})).toThrow();
expect(() => LogDestinationSchema.parse({
name: 'kebab-case',
type: 'console',
})).toThrow();
});
});
describe('LogEnrichmentConfigSchema', () => {
it('should apply defaults', () => {
const config = LogEnrichmentConfigSchema.parse({});
expect(config.addHostname).toBe(true);
expect(config.addProcessId).toBe(true);
expect(config.addEnvironment).toBe(true);
expect(config.addCaller).toBe(false);
expect(config.addCorrelationIds).toBe(true);
});
it('should accept static fields', () => {
const config = LogEnrichmentConfigSchema.parse({
staticFields: {
application: 'my-app',
version: '1.0.0',
},
});
expect(config.staticFields?.application).toBe('my-app');
expect(config.staticFields?.version).toBe('1.0.0');
});
});
describe('StructuredLogEntrySchema', () => {
it('should accept minimal log entry', () => {
const entry: StructuredLogEntry = {
timestamp: '2024-01-15T10:30:00.000Z',
level: 'info',
message: 'Application started',
};
expect(() => StructuredLogEntrySchema.parse(entry)).not.toThrow();
});
it('should accept full log entry with all fields', () => {
const entry: StructuredLogEntry = {
timestamp: '2024-01-15T10:30:00.000Z',
level: 'error',
message: 'Database connection failed',
context: { database: 'postgres', attempt: 3 },
error: {
name: 'ConnectionError',
message: 'Connection timeout',
stack: 'Error: ...',
},
trace: {
traceId: 'abc123',
spanId: 'def456',
},
source: {
service: 'api-server',
component: 'database-pool',
file: 'db.ts',
line: 42,
},
host: {
hostname: 'server-01',
pid: 1234,
},
environment: 'production',
user: {
id: 'user-123',
username: 'john',
},
};
expect(() => StructuredLogEntrySchema.parse(entry)).not.toThrow();
});
});
describe('LoggingConfigSchema', () => {
it('should accept minimal configuration', () => {
const config: LoggingConfig = {
name: 'default_logging',
label: 'Default Logging',
destinations: [
{
name: 'console',
type: 'console',
},
],
};
expect(() => LoggingConfigSchema.parse(config)).not.toThrow();
});
it('should accept configuration with default logger', () => {
const config: LoggingConfig = {
name: 'app_logging',
label: 'Application Logging',
default: {
level: 'info',
format: 'json',
},
destinations: [
{
name: 'console',
type: 'console',
},
],
};
const parsed = LoggingConfigSchema.parse(config);
expect(parsed.default).toBeDefined();
expect(parsed.default?.level).toBe('info');
});
it('should accept configuration with named loggers', () => {
const config: LoggingConfig = {
name: 'multi_logger',
label: 'Multi Logger',
default: {
level: 'info',
},
loggers: {
database: {
level: 'debug',
format: 'json',
},
http: {
level: 'warn',
format: 'text',
},
},
destinations: [],
};
const parsed = LoggingConfigSchema.parse(config);
expect(parsed.loggers).toBeDefined();
expect(parsed.loggers?.database).toBeDefined();
expect(parsed.loggers?.database.level).toBe('debug');
expect(parsed.loggers?.http.level).toBe('warn');
});
it('should apply defaults', () => {
const config = LoggingConfigSchema.parse({
name: 'test_logging',
label: 'Test Logging',
destinations: [],
});
expect(config.enabled).toBe(true);
expect(config.level).toBe('info');
expect(config.redact).toContain('password');
expect(config.redact).toContain('apiKey');
});
it('should accept full configuration', () => {
const config: LoggingConfig = {
name: 'production_logging',
label: 'Production Logging',
enabled: true,
level: 'warn',
default: {
level: 'info',
format: 'json',
},
loggers: {
app: {
level: 'debug',
},
},
destinations: [
{
name: 'console_dest',
type: 'console',
level: 'info',
},
{
name: 'file_dest',
type: 'file',
file: {
path: '/var/log/app.log',
rotation: {
maxSize: '100m',
maxFiles: 7,
},
},
},
],
enrichment: {
staticFields: { app: 'my-app' },
addHostname: true,
},
sampling: {
enabled: true,
rate: 0.8,
},
buffer: {
enabled: true,
size: 5000,
// `buffer.flushInterval` → `buffer.flushIntervalMs` (#17782, ruling A
// on #15939 executing #14478). Same 2000 milliseconds, new key.
flushIntervalMs: 2000,
},
};
expect(() => LoggingConfigSchema.parse(config)).not.toThrow();
});
it('should enforce snake_case naming', () => {
expect(() => LoggingConfigSchema.parse({
name: 'CamelCase',
label: 'Test',
destinations: [],
})).toThrow();
});
it('should enforce max length for name', () => {
const longName = 'a'.repeat(65);
expect(() => LoggingConfigSchema.parse({
name: longName,
label: 'Test',
destinations: [],
})).toThrow();
});
});
// #15939 ruling A (executing #14478) — the four duration keys on this file that
// named milliseconds in a source JSDoc and nowhere an author or a reference-page
// reader could see it: `.describe()` was absent on all four, so
// `content/docs/references/system/logging.mdx` published a bare number. Each is
// renamed with the unit in the key and the old spelling left as a `retiredKey`
// tombstone — none of the four enclosing objects is `.strict()`, so a bare
// deletion would have stripped the value in silence.
//
// `flushInterval` was declared TWICE on this file, in two different defs with
// two different defaults, so each def is pinned separately below: they are
// different keys and each carries its own prescription.
describe('logging duration keys → *Ms', () => {
describe('HttpDestinationConfig.batch.flushInterval → flushIntervalMs', () => {
it('REFUSES the retired spelling with a rename naming `flushIntervalMs`', () => {
const result = HttpDestinationConfigSchema.safeParse({
url: 'https://logs.example.com/v1/logs',
batch: { flushInterval: 5000 },
});
expect(result.success).toBe(false);
const issue = result.error!.issues.find((i) => i.path.join('.') === 'batch.flushInterval');
expect(issue).toBeDefined();
expect(issue!.message).toMatch(
/`HttpDestinationConfig\.batch\.flushInterval` was renamed.*Rename the key to `flushIntervalMs`/s,
);
});
it('accepts the suffixed key at the magnitude the retired key carried, with the same default', () => {
const parsed = HttpDestinationConfigSchema.parse({
url: 'https://logs.example.com/v1/logs',
batch: { flushIntervalMs: 10000 },
});
expect(parsed.batch?.flushIntervalMs).toBe(10000);
expect(parsed.batch).not.toHaveProperty('flushInterval');
expect(
HttpDestinationConfigSchema.parse({
url: 'https://logs.example.com/v1/logs',
batch: {},
}).batch?.flushIntervalMs,
).toBe(5000);
});
it('publishes the unit in the describe — the text the reference pages render', () => {
const batch = HttpDestinationConfigSchema.shape.batch.unwrap();
expect(batch.shape.flushIntervalMs.description).toBe('Flush interval in milliseconds');
});
});
describe('HttpDestinationConfig.retry.initialDelay → initialDelayMs', () => {
it('REFUSES the retired spelling with a rename naming `initialDelayMs`', () => {
const result = HttpDestinationConfigSchema.safeParse({
url: 'https://logs.example.com/v1/logs',
retry: { initialDelay: 1000 },
});
expect(result.success).toBe(false);
const issue = result.error!.issues.find((i) => i.path.join('.') === 'retry.initialDelay');
expect(issue).toBeDefined();
expect(issue!.message).toMatch(
/`HttpDestinationConfig\.retry\.initialDelay` was renamed.*Rename the key to `initialDelayMs`/s,
);
});
it('accepts the suffixed key at the magnitude the retired key carried, with the same default', () => {
const parsed = HttpDestinationConfigSchema.parse({
url: 'https://logs.example.com/v1/logs',
retry: { initialDelayMs: 2500 },
});
expect(parsed.retry?.initialDelayMs).toBe(2500);
expect(parsed.retry).not.toHaveProperty('initialDelay');
expect(
HttpDestinationConfigSchema.parse({
url: 'https://logs.example.com/v1/logs',
retry: {},
}).retry?.initialDelayMs,
).toBe(1000);
});
it('publishes the unit in the describe — the text the reference pages render', () => {
const retry = HttpDestinationConfigSchema.shape.retry.unwrap();
expect(retry.shape.initialDelayMs.description).toBe('Initial retry delay in milliseconds');
});
});
describe('HttpDestinationConfig.timeout → timeoutMs (the one TOP-LEVEL key of the four)', () => {
it('REFUSES the retired spelling with a rename naming `timeoutMs`', () => {
const result = HttpDestinationConfigSchema.safeParse({
url: 'https://logs.example.com/v1/logs',
timeout: 30000,
});
expect(result.success).toBe(false);
const issue = result.error!.issues.find((i) => i.path.join('.') === 'timeout');
expect(issue).toBeDefined();
expect(issue!.message).toMatch(
/`HttpDestinationConfig\.timeout` was renamed.*Rename the key to `timeoutMs`/s,
);
});
it('accepts the suffixed key at the magnitude the retired key carried, with the same default', () => {
const parsed = HttpDestinationConfigSchema.parse({
url: 'https://logs.example.com/v1/logs',
timeoutMs: 45000,
});
expect(parsed.timeoutMs).toBe(45000);
expect(parsed).not.toHaveProperty('timeout');
expect(
HttpDestinationConfigSchema.parse({ url: 'https://logs.example.com/v1/logs' }).timeoutMs,
).toBe(30000);
});
it('publishes the unit in the describe — the text the reference pages render', () => {
expect(HttpDestinationConfigSchema.shape.timeoutMs.description)
.toBe('Timeout in milliseconds');
});
});
describe('LoggingConfig.buffer.flushInterval → flushIntervalMs (a different key from the batch one)', () => {
const base = { name: 'app_logging', label: 'App logging', destinations: [] };
it('REFUSES the retired spelling with a rename naming `flushIntervalMs`', () => {
const result = LoggingConfigSchema.safeParse({
...base,
buffer: { flushInterval: 1000 },
});
expect(result.success).toBe(false);
const issue = result.error!.issues.find((i) => i.path.join('.') === 'buffer.flushInterval');
expect(issue).toBeDefined();
expect(issue!.message).toMatch(
/`LoggingConfig\.buffer\.flushInterval` was renamed.*Rename the key to `flushIntervalMs`/s,
);
});
it('accepts the suffixed key at the magnitude the retired key carried, with its OWN default', () => {
const parsed = LoggingConfigSchema.parse({ ...base, buffer: { flushIntervalMs: 2000 } });
expect(parsed.buffer?.flushIntervalMs).toBe(2000);
expect(parsed.buffer).not.toHaveProperty('flushInterval');
// 1000 here, 5000 on `HttpDestinationConfig.batch` — the two same-named
// keys never shared a default and do not share one now.
expect(LoggingConfigSchema.parse({ ...base, buffer: {} }).buffer?.flushIntervalMs).toBe(1000);
});
it('publishes the unit in the describe — the text the reference pages render', () => {
const buffer = LoggingConfigSchema.shape.buffer.unwrap();
expect(buffer.shape.flushIntervalMs.description).toBe('Flush interval in milliseconds');
});
});
});