@flighthq/log 0.5.1-next.920.da8f530 → 0.6.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 +2 -2
- package/dist/consoleLogSink.d.ts +8 -0
- package/dist/consoleLogSink.d.ts.map +1 -0
- package/dist/consoleLogSink.js +62 -0
- package/dist/consoleLogSink.js.map +1 -0
- package/dist/contract.d.ts +4 -1
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +4 -1
- package/dist/contract.js.map +1 -1
- package/dist/fileLogSink.d.ts +8 -0
- package/dist/fileLogSink.d.ts.map +1 -0
- package/dist/fileLogSink.js +69 -0
- package/dist/fileLogSink.js.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/log.d.ts +89 -67
- package/dist/log.d.ts.map +1 -1
- package/dist/log.js +360 -473
- package/dist/log.js.map +1 -1
- package/dist/types.d.ts +115 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +14 -0
- package/dist/types.js.map +1 -0
- package/package.json +6 -4
- package/src/consoleLogSink.test.ts +133 -0
- package/src/fileLogSink.test.ts +314 -0
- package/src/log.test.ts +721 -776
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { EntityRuntimeKey } from '@flighthq/entity/contract';
|
|
2
|
+
|
|
3
|
+
import { createFileLogSink, destroyFileLogSink } from './fileLogSink.ts';
|
|
4
|
+
import type { LogState } from './log.ts';
|
|
5
|
+
import { addLogSink, createLogState, log, removeLogSink } from './log.ts';
|
|
6
|
+
import type {
|
|
7
|
+
FileLogSinkDestroyOutcome,
|
|
8
|
+
LogData,
|
|
9
|
+
LogEntry,
|
|
10
|
+
LogTransport,
|
|
11
|
+
LogTransportDestroyOutcome,
|
|
12
|
+
LogTransportFlushOutcome,
|
|
13
|
+
LogTransportWriteOutcome,
|
|
14
|
+
} from './types.ts';
|
|
15
|
+
import { LogLevel } from './types.ts';
|
|
16
|
+
|
|
17
|
+
function createTestLogTransport(
|
|
18
|
+
overrides: Partial<Pick<LogTransport, 'destroy' | 'flush' | 'write'>> = {},
|
|
19
|
+
): LogTransport {
|
|
20
|
+
const defaultDestroy = async (): Promise<LogTransportDestroyOutcome> => ({ reason: 'destroyed' });
|
|
21
|
+
const defaultFlush = async (): Promise<LogTransportFlushOutcome> => ({
|
|
22
|
+
delivery: 'process-buffer',
|
|
23
|
+
reason: 'drained',
|
|
24
|
+
});
|
|
25
|
+
const defaultWrite = (): LogTransportWriteOutcome => ({ reason: 'accepted' });
|
|
26
|
+
return {
|
|
27
|
+
[EntityRuntimeKey]: undefined,
|
|
28
|
+
destroy: overrides.destroy ?? defaultDestroy,
|
|
29
|
+
flush: overrides.flush ?? defaultFlush,
|
|
30
|
+
write: overrides.write ?? defaultWrite,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function entryOf(level: LogLevel, channel: string | null, data: LogData, timestampMs = 0): LogEntry {
|
|
35
|
+
return { level, channel, data, timestampMs };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let state: LogState;
|
|
39
|
+
|
|
40
|
+
beforeEach(() => {
|
|
41
|
+
state = createLogState();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe('createFileLogSink', () => {
|
|
45
|
+
it.each<LogTransportWriteOutcome>([
|
|
46
|
+
{ reason: 'accepted' },
|
|
47
|
+
{ reason: 'backpressure', retryAfterMs: 25 },
|
|
48
|
+
{ message: 'disk full', reason: 'operation-failed' },
|
|
49
|
+
{ reason: 'destroyed' },
|
|
50
|
+
])('offers one complete formatted line when admission reports $reason', (writeOutcome) => {
|
|
51
|
+
const lines: string[] = [];
|
|
52
|
+
const transport = createTestLogTransport({
|
|
53
|
+
write(line) {
|
|
54
|
+
lines.push(line);
|
|
55
|
+
return writeOutcome;
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
const handle = createFileLogSink(state, transport);
|
|
59
|
+
addLogSink(state, handle.sink);
|
|
60
|
+
log(state, 4, LogLevel.Info, 'file-entry');
|
|
61
|
+
expect(lines).toHaveLength(1);
|
|
62
|
+
const parsed = JSON.parse(lines[0]);
|
|
63
|
+
expect(parsed).toMatchObject({ __flight: true, t: 4, level: 'info', data: { msg: 'file-entry' } });
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('creates an Entity handle', () => {
|
|
67
|
+
const handle = createFileLogSink(state, createTestLogTransport());
|
|
68
|
+
|
|
69
|
+
expect(EntityRuntimeKey in handle).toBe(true);
|
|
70
|
+
expect(handle[EntityRuntimeKey]).toBeUndefined();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('accepts a custom formatter', () => {
|
|
74
|
+
const lines: string[] = [];
|
|
75
|
+
const transport = createTestLogTransport({
|
|
76
|
+
write(line) {
|
|
77
|
+
lines.push(line);
|
|
78
|
+
return { reason: 'accepted' };
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
const handle = createFileLogSink(state, transport, { formatter: () => 'custom' });
|
|
82
|
+
addLogSink(state, handle.sink);
|
|
83
|
+
log(state, 0, LogLevel.Info, 'x');
|
|
84
|
+
expect(lines[0]).toBe('custom\n');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("formats with its state's redaction paths by default", () => {
|
|
88
|
+
const lines: string[] = [];
|
|
89
|
+
const handle = createFileLogSink(
|
|
90
|
+
state,
|
|
91
|
+
createTestLogTransport({
|
|
92
|
+
write(line) {
|
|
93
|
+
lines.push(line);
|
|
94
|
+
return { reason: 'accepted' };
|
|
95
|
+
},
|
|
96
|
+
}),
|
|
97
|
+
);
|
|
98
|
+
state.redactionPaths.push('token');
|
|
99
|
+
|
|
100
|
+
handle.sink(entryOf(LogLevel.Info, null, { token: 'secret' }));
|
|
101
|
+
|
|
102
|
+
expect(JSON.parse(lines[0]).data.token).toBe('[REDACTED]');
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('pins each handle to its injected transport', () => {
|
|
106
|
+
const firstLines: string[] = [];
|
|
107
|
+
const secondLines: string[] = [];
|
|
108
|
+
const first = createFileLogSink(
|
|
109
|
+
state,
|
|
110
|
+
createTestLogTransport({
|
|
111
|
+
write(line) {
|
|
112
|
+
firstLines.push(line);
|
|
113
|
+
return { reason: 'accepted' };
|
|
114
|
+
},
|
|
115
|
+
}),
|
|
116
|
+
{ formatter: () => 'first' },
|
|
117
|
+
);
|
|
118
|
+
const second = createFileLogSink(
|
|
119
|
+
state,
|
|
120
|
+
createTestLogTransport({
|
|
121
|
+
write(line) {
|
|
122
|
+
secondLines.push(line);
|
|
123
|
+
return { reason: 'accepted' };
|
|
124
|
+
},
|
|
125
|
+
}),
|
|
126
|
+
{ formatter: () => 'second' },
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
first.sink(entryOf(LogLevel.Info, null, 'entry'));
|
|
130
|
+
second.sink(entryOf(LogLevel.Info, null, 'entry'));
|
|
131
|
+
|
|
132
|
+
expect(firstLines).toEqual(['first\n']);
|
|
133
|
+
expect(secondLines).toEqual(['second\n']);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
describe('destroyFileLogSink', () => {
|
|
138
|
+
it('awaits flush before destroying the transport', async () => {
|
|
139
|
+
const events: string[] = [];
|
|
140
|
+
let finishFlush: ((outcome: LogTransportFlushOutcome) => void) | undefined;
|
|
141
|
+
const flushPromise = new Promise<LogTransportFlushOutcome>((resolve) => {
|
|
142
|
+
finishFlush = resolve;
|
|
143
|
+
});
|
|
144
|
+
const transport = createTestLogTransport({
|
|
145
|
+
destroy: async () => {
|
|
146
|
+
events.push('destroy');
|
|
147
|
+
return { reason: 'destroyed' };
|
|
148
|
+
},
|
|
149
|
+
flush: () => {
|
|
150
|
+
events.push('flush-start');
|
|
151
|
+
return flushPromise.then((outcome) => {
|
|
152
|
+
events.push('flush-end');
|
|
153
|
+
return outcome;
|
|
154
|
+
});
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const closing = destroyFileLogSink(state, createFileLogSink(state, transport));
|
|
159
|
+
expect(events).toEqual(['flush-start']);
|
|
160
|
+
finishFlush?.({ delivery: 'operating-system', reason: 'drained' });
|
|
161
|
+
|
|
162
|
+
await expect(closing).resolves.toEqual({
|
|
163
|
+
destroy: { reason: 'destroyed' },
|
|
164
|
+
flush: { delivery: 'operating-system', reason: 'drained' },
|
|
165
|
+
reason: 'destroyed',
|
|
166
|
+
});
|
|
167
|
+
expect(events).toEqual(['flush-start', 'flush-end', 'destroy']);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('attempts destroy and preserves both outcomes when flush fails', async () => {
|
|
171
|
+
const events: string[] = [];
|
|
172
|
+
const handle = createFileLogSink(
|
|
173
|
+
state,
|
|
174
|
+
createTestLogTransport({
|
|
175
|
+
destroy: async () => {
|
|
176
|
+
events.push('destroy');
|
|
177
|
+
return { reason: 'destroyed' };
|
|
178
|
+
},
|
|
179
|
+
flush: async () => {
|
|
180
|
+
events.push('flush');
|
|
181
|
+
return { message: 'disk full', reason: 'operation-failed' };
|
|
182
|
+
},
|
|
183
|
+
}),
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
await expect(destroyFileLogSink(state, handle)).resolves.toEqual({
|
|
187
|
+
destroy: { reason: 'destroyed' },
|
|
188
|
+
flush: { message: 'disk full', reason: 'operation-failed' },
|
|
189
|
+
reason: 'operation-failed',
|
|
190
|
+
});
|
|
191
|
+
expect(events).toEqual(['flush', 'destroy']);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('keeps a successor destination live when an older handle is destroyed', async () => {
|
|
195
|
+
const firstEvents: string[] = [];
|
|
196
|
+
const secondLines: string[] = [];
|
|
197
|
+
const first = createFileLogSink(
|
|
198
|
+
state,
|
|
199
|
+
createTestLogTransport({
|
|
200
|
+
destroy: async () => {
|
|
201
|
+
firstEvents.push('destroy');
|
|
202
|
+
return { reason: 'destroyed' };
|
|
203
|
+
},
|
|
204
|
+
}),
|
|
205
|
+
);
|
|
206
|
+
const second = createFileLogSink(
|
|
207
|
+
state,
|
|
208
|
+
createTestLogTransport({
|
|
209
|
+
write(line) {
|
|
210
|
+
secondLines.push(line);
|
|
211
|
+
return { reason: 'accepted' };
|
|
212
|
+
},
|
|
213
|
+
}),
|
|
214
|
+
{ formatter: () => 'second' },
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
await destroyFileLogSink(state, first);
|
|
218
|
+
second.sink(entryOf(LogLevel.Info, null, 'still-live'));
|
|
219
|
+
|
|
220
|
+
expect(firstEvents).toEqual(['destroy']);
|
|
221
|
+
expect(secondLines).toEqual(['second\n']);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('terminals and unregisters the sink before awaiting flush', async () => {
|
|
225
|
+
const lines: string[] = [];
|
|
226
|
+
let finishFlush: ((outcome: LogTransportFlushOutcome) => void) | undefined;
|
|
227
|
+
const handle = createFileLogSink(
|
|
228
|
+
state,
|
|
229
|
+
createTestLogTransport({
|
|
230
|
+
flush: () =>
|
|
231
|
+
new Promise<LogTransportFlushOutcome>((resolve) => {
|
|
232
|
+
finishFlush = resolve;
|
|
233
|
+
}),
|
|
234
|
+
write(line) {
|
|
235
|
+
lines.push(line);
|
|
236
|
+
return { reason: 'accepted' };
|
|
237
|
+
},
|
|
238
|
+
}),
|
|
239
|
+
);
|
|
240
|
+
addLogSink(state, handle.sink);
|
|
241
|
+
|
|
242
|
+
const closing = destroyFileLogSink(state, handle);
|
|
243
|
+
log(state, 0, LogLevel.Info, 'after-destroy-started');
|
|
244
|
+
|
|
245
|
+
expect(lines).toEqual([]);
|
|
246
|
+
expect(removeLogSink(state, handle.sink)).toBe(false);
|
|
247
|
+
finishFlush?.({ delivery: 'process-buffer', reason: 'drained' });
|
|
248
|
+
await closing;
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it('makes repeated teardown idempotent without repeating provider calls', async () => {
|
|
252
|
+
let destroyCalls = 0;
|
|
253
|
+
let flushCalls = 0;
|
|
254
|
+
const handle = createFileLogSink(
|
|
255
|
+
state,
|
|
256
|
+
createTestLogTransport({
|
|
257
|
+
destroy: async () => {
|
|
258
|
+
destroyCalls++;
|
|
259
|
+
return { reason: 'destroyed' };
|
|
260
|
+
},
|
|
261
|
+
flush: async () => {
|
|
262
|
+
flushCalls++;
|
|
263
|
+
return { delivery: 'durable-storage', reason: 'drained' };
|
|
264
|
+
},
|
|
265
|
+
}),
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
const first: FileLogSinkDestroyOutcome = await destroyFileLogSink(state, handle);
|
|
269
|
+
const second = await destroyFileLogSink(state, handle);
|
|
270
|
+
|
|
271
|
+
expect(first).toEqual({
|
|
272
|
+
destroy: { reason: 'destroyed' },
|
|
273
|
+
flush: { delivery: 'durable-storage', reason: 'drained' },
|
|
274
|
+
reason: 'destroyed',
|
|
275
|
+
});
|
|
276
|
+
expect(second).toEqual({ ...first, reason: 'already-destroyed' });
|
|
277
|
+
expect({ destroyCalls, flushCalls }).toEqual({ destroyCalls: 1, flushCalls: 1 });
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it.each<LogTransportFlushOutcome>([
|
|
281
|
+
{ delivery: 'process-buffer', reason: 'drained' },
|
|
282
|
+
{ delivery: 'operating-system', reason: 'drained' },
|
|
283
|
+
{ delivery: 'durable-storage', reason: 'drained' },
|
|
284
|
+
{ delivery: 'remote-acknowledged', reason: 'drained' },
|
|
285
|
+
])('preserves the $delivery delivery boundary', async (flushOutcome) => {
|
|
286
|
+
const handle = createFileLogSink(state, createTestLogTransport({ flush: async () => flushOutcome }));
|
|
287
|
+
|
|
288
|
+
const outcome = await destroyFileLogSink(state, handle);
|
|
289
|
+
|
|
290
|
+
expect(outcome.flush).toEqual(flushOutcome);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it('converts rejected flush and destroy operations into a composite failure', async () => {
|
|
294
|
+
const handle = createFileLogSink(
|
|
295
|
+
state,
|
|
296
|
+
createTestLogTransport({
|
|
297
|
+
destroy: () => Promise.reject(new Error('close failed')),
|
|
298
|
+
flush: () => Promise.reject(new Error('flush failed')),
|
|
299
|
+
}),
|
|
300
|
+
);
|
|
301
|
+
|
|
302
|
+
await expect(destroyFileLogSink(state, handle)).resolves.toEqual({
|
|
303
|
+
destroy: { message: 'close failed', reason: 'operation-failed' },
|
|
304
|
+
flush: { message: 'flush failed', reason: 'operation-failed' },
|
|
305
|
+
reason: 'operation-failed',
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it('rejects an unknown handle', async () => {
|
|
310
|
+
const unknown = { [EntityRuntimeKey]: undefined, sink: (): void => {} };
|
|
311
|
+
|
|
312
|
+
await expect(destroyFileLogSink(state, unknown)).rejects.toThrow('destroyFileLogSink received an unknown handle');
|
|
313
|
+
});
|
|
314
|
+
});
|