@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
package/dist/log.js
CHANGED
|
@@ -1,61 +1,71 @@
|
|
|
1
|
+
import { allocateEntity } from '@flighthq/entity/contract';
|
|
1
2
|
import { createSignal, emitSignal } from '@flighthq/signals/contract';
|
|
2
|
-
import {
|
|
3
|
-
// Logging is split into two faces of one contract so each consumer tree-shakes its half:
|
|
4
|
-
//
|
|
5
|
-
// Emit side — log and the log* wrappers. Featherweight: each checks the level gate and
|
|
6
|
-
// forwards an entry to the installed sinks. A build that never installs a sink
|
|
7
|
-
// carries only the forwarder and the LogLevel enum; the listener-side code
|
|
8
|
-
// tree-shakes away.
|
|
9
|
-
// Listener side — createConsoleCaptureSink, sink management (addLogSink / removeLogSink),
|
|
10
|
-
// formatters, and sink combinators. Imported by tools (the examples, the capture
|
|
11
|
-
// harness). This is where levels gate output: each sink records what it wants;
|
|
12
|
-
// the console-capture sink records EVERY level (the machine record is complete)
|
|
13
|
-
// and additionally prints a human console line for levels at or above the
|
|
14
|
-
// configured threshold.
|
|
3
|
+
import { LogLevel } from './types.js';
|
|
15
4
|
// Listener side. Adds a sink to the fan-out list. No-op if already present.
|
|
16
|
-
export function addLogSink(sink) {
|
|
17
|
-
if (
|
|
5
|
+
export function addLogSink(state, sink) {
|
|
6
|
+
if (state.sinks.includes(sink))
|
|
18
7
|
return;
|
|
19
|
-
|
|
8
|
+
state.sinks.push(sink);
|
|
9
|
+
}
|
|
10
|
+
// Advances a buffered sink's clock. When at least `intervalMs` has passed since the last flush it
|
|
11
|
+
// flushes the queued entries. The first call only starts the interval. This replaces a hidden
|
|
12
|
+
// interval timer: the caller ticks the sink from whatever loop it already runs.
|
|
13
|
+
export function advanceLogSink(handle, nowMs) {
|
|
14
|
+
const state = _bufferedSinkStates.get(handle);
|
|
15
|
+
if (!state)
|
|
16
|
+
return;
|
|
17
|
+
if (state.lastFlushMs === null) {
|
|
18
|
+
state.lastFlushMs = nowMs;
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (nowMs - state.lastFlushMs < state.intervalMs)
|
|
22
|
+
return;
|
|
23
|
+
state.lastFlushMs = nowMs;
|
|
24
|
+
_flushBufferedState(state);
|
|
20
25
|
}
|
|
21
26
|
// Opens a named log group. All entries emitted while the group is open carry a `depth` field in
|
|
22
27
|
// their structured data and the text formatter will indent them. Call endLogGroup to close. Groups
|
|
23
28
|
// nest: each beginLogGroup increments the depth, endLogGroup decrements it.
|
|
24
|
-
export function beginLogGroup(label, channel = null) {
|
|
25
|
-
|
|
26
|
-
if (!_passesLevelGate(LogLevel.Debug, channel))
|
|
29
|
+
export function beginLogGroup(state, nowMs, label, channel = null) {
|
|
30
|
+
state.groupDepth++;
|
|
31
|
+
if (!_passesLevelGate(state, LogLevel.Debug, channel))
|
|
27
32
|
return;
|
|
28
|
-
_emitToSinks(
|
|
33
|
+
_emitToSinks(state, {
|
|
34
|
+
level: LogLevel.Debug,
|
|
35
|
+
channel,
|
|
36
|
+
data: { msg: label, group: 'begin', depth: state.groupDepth },
|
|
37
|
+
timestampMs: nowMs,
|
|
38
|
+
});
|
|
29
39
|
}
|
|
30
40
|
// Listener side. Clears one per-channel level override so the channel inherits the global level.
|
|
31
|
-
export function clearLogChannelLevel(channel) {
|
|
32
|
-
|
|
41
|
+
export function clearLogChannelLevel(state, channel) {
|
|
42
|
+
state.channelLevels.delete(channel);
|
|
33
43
|
}
|
|
34
44
|
// Listener side. Clears all per-channel level overrides.
|
|
35
|
-
export function clearLogChannelLevels() {
|
|
36
|
-
|
|
45
|
+
export function clearLogChannelLevels(state) {
|
|
46
|
+
state.channelLevels.clear();
|
|
37
47
|
}
|
|
38
48
|
// Resets the group nesting depth to zero without emitting any entries. Useful for test teardown or
|
|
39
49
|
// error recovery when normal beginLogGroup / endLogGroup pairing breaks down.
|
|
40
|
-
export function clearLogGroups() {
|
|
41
|
-
|
|
50
|
+
export function clearLogGroups(state) {
|
|
51
|
+
state.groupDepth = 0;
|
|
42
52
|
}
|
|
43
53
|
// Clears the set of keys that logOnce has already emitted, so subsequent calls with those keys will
|
|
44
|
-
// fire again.
|
|
45
|
-
export function clearLogOnceKeys() {
|
|
46
|
-
|
|
54
|
+
// fire again.
|
|
55
|
+
export function clearLogOnceKeys(state) {
|
|
56
|
+
state.onceKeys.clear();
|
|
47
57
|
}
|
|
48
58
|
// Clears all field redaction paths set by setLogRedactionPaths.
|
|
49
|
-
export function clearLogRedactionPaths() {
|
|
50
|
-
|
|
59
|
+
export function clearLogRedactionPaths(state) {
|
|
60
|
+
state.redactionPaths.length = 0;
|
|
51
61
|
}
|
|
52
62
|
// Clears all custom serializer registrations set by registerLogSerializer.
|
|
53
|
-
export function clearLogSerializers() {
|
|
54
|
-
|
|
63
|
+
export function clearLogSerializers(state) {
|
|
64
|
+
state.serializers.clear();
|
|
55
65
|
}
|
|
56
66
|
// Listener side. Clears all installed sinks.
|
|
57
|
-
export function clearLogSinks() {
|
|
58
|
-
|
|
67
|
+
export function clearLogSinks(state) {
|
|
68
|
+
state.sinks.length = 0;
|
|
59
69
|
}
|
|
60
70
|
// Clears all captured entries from a memory sink.
|
|
61
71
|
export function clearMemoryLogSink(handle) {
|
|
@@ -66,81 +76,29 @@ export function clearMemoryLogSink(handle) {
|
|
|
66
76
|
state.head = 0;
|
|
67
77
|
}
|
|
68
78
|
// Creates a sink that batches entries and forwards them to `target` in bulk. The buffer flushes
|
|
69
|
-
// when it reaches `size` entries
|
|
70
|
-
//
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const flush = () => {
|
|
76
|
-
const state = _bufferedSinkStates.get(handle);
|
|
77
|
-
if (state.buf.length === 0)
|
|
78
|
-
return;
|
|
79
|
-
const batch = state.buf.splice(0);
|
|
80
|
-
for (const entry of batch)
|
|
81
|
-
target(entry);
|
|
82
|
-
};
|
|
83
|
-
const sink = (entry) => {
|
|
84
|
-
const state = _bufferedSinkStates.get(handle);
|
|
85
|
-
state.buf.push({ level: entry.level, channel: entry.channel, data: entry.data });
|
|
86
|
-
if (state.buf.length >= size)
|
|
87
|
-
flush();
|
|
88
|
-
};
|
|
89
|
-
const handle = initializeLogEntity({ sink });
|
|
90
|
-
let timer = null;
|
|
91
|
-
if (intervalMs > 0 && typeof setInterval !== 'undefined') {
|
|
92
|
-
timer = setInterval(flush, intervalMs);
|
|
93
|
-
}
|
|
94
|
-
_bufferedSinkStates.set(handle, { buf: [], timer, flush });
|
|
95
|
-
return handle;
|
|
79
|
+
// when it reaches `size` entries, when advanceLogSink sees `intervalMs` elapse, or on flushLogSink /
|
|
80
|
+
// disposeLogSink.
|
|
81
|
+
export function createBufferedLogSink(target, options) {
|
|
82
|
+
const out = allocateEntity();
|
|
83
|
+
initializeBufferedLogSink(out, target, options);
|
|
84
|
+
return out;
|
|
96
85
|
}
|
|
97
86
|
// Creates a child context that inherits the parent's channel and fields. The child wins on key
|
|
98
87
|
// collision. An explicit channel overrides the parent's channel.
|
|
99
88
|
export function createChildLogContext(parent, fields, channel) {
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// visual noise, but the Playwright capture script reads every console level, so it always
|
|
105
|
-
// lands in logs.jsonl. Entries at or above the console threshold (setLogConsoleLevel) are ALSO
|
|
106
|
-
// printed as a human-readable line via the matching console method. Install with addLogSink or
|
|
107
|
-
// setLogSink.
|
|
108
|
-
export function createConsoleCaptureSink(options = {}) {
|
|
109
|
-
const envelopeFormatter = options.formatter ?? _defaultJsonFormatter;
|
|
110
|
-
return (entry) => _writeConsoleCaptureEntry(entry, envelopeFormatter);
|
|
111
|
-
}
|
|
112
|
-
export function createConsoleLogSink(options = {}) {
|
|
113
|
-
const formatter = options.formatter ?? createTextLogFormatter({ levelPrefix: true });
|
|
114
|
-
return (entry) => _writeConsoleLogEntry(entry, formatter);
|
|
89
|
+
const out = createLogContext();
|
|
90
|
+
out.channel = channel !== undefined ? channel : parent.channel;
|
|
91
|
+
out.fields = { ...parent.fields, ...fields };
|
|
92
|
+
return out;
|
|
115
93
|
}
|
|
116
94
|
// Creates a fan-out sink that forwards every entry to all supplied sinks.
|
|
117
|
-
export function createFanoutLogSink(
|
|
95
|
+
export function createFanoutLogSink(sinks) {
|
|
118
96
|
const list = sinks.slice();
|
|
119
97
|
return (entry) => {
|
|
120
98
|
for (const s of list)
|
|
121
99
|
s(entry);
|
|
122
100
|
};
|
|
123
101
|
}
|
|
124
|
-
// Creates an Entity sink that exclusively owns the configured transport for its entire lifetime.
|
|
125
|
-
// The formatter defaults to createJsonLogFormatter (one complete JSON line per entry). Each sink
|
|
126
|
-
// call offers that line synchronously to the pinned transport; admission and later delivery remain
|
|
127
|
-
// the transport's explicit outcome contracts.
|
|
128
|
-
export function createFileLogSink(transport, options = {}) {
|
|
129
|
-
const formatter = options.formatter ?? createJsonLogFormatter();
|
|
130
|
-
const sink = (entry) => {
|
|
131
|
-
const state = _fileLogSinkStates.get(handle);
|
|
132
|
-
if (!state || state.status !== 'active' || state.transport === null)
|
|
133
|
-
return;
|
|
134
|
-
state.transport.write(formatter(entry) + '\n');
|
|
135
|
-
};
|
|
136
|
-
const handle = { [EntityRuntimeKey]: undefined, sink };
|
|
137
|
-
_fileLogSinkStates.set(handle, {
|
|
138
|
-
destroyPromise: null,
|
|
139
|
-
status: 'active',
|
|
140
|
-
transport,
|
|
141
|
-
});
|
|
142
|
-
return handle;
|
|
143
|
-
}
|
|
144
102
|
// Creates a sink that forwards only entries matching a predicate. Compose with addLogSink to give
|
|
145
103
|
// each target its own independent filter (per-sink level, per-channel filter, etc.).
|
|
146
104
|
export function createFilterLogSink(target, predicate) {
|
|
@@ -150,73 +108,58 @@ export function createFilterLogSink(target, predicate) {
|
|
|
150
108
|
};
|
|
151
109
|
}
|
|
152
110
|
// Creates a formatter that produces the `__flight` JSON envelope used by the capture harness.
|
|
153
|
-
// Field redaction (setLogRedactionPaths) and custom serializers (registerLogSerializer) are
|
|
154
|
-
//
|
|
155
|
-
export function createJsonLogFormatter() {
|
|
111
|
+
// Field redaction (setLogRedactionPaths) and custom serializers (registerLogSerializer) are read
|
|
112
|
+
// from `state` at format time; the envelope's `t` is the entry's own timestamp.
|
|
113
|
+
export function createJsonLogFormatter(state) {
|
|
156
114
|
return (entry) => {
|
|
157
115
|
const { level, channel, data } = entry;
|
|
158
|
-
const serialized = _applySerializers(typeof data === 'string' ? { msg: data } : data);
|
|
159
|
-
const redacted =
|
|
116
|
+
const serialized = _applySerializers(state, typeof data === 'string' ? { msg: data } : data);
|
|
117
|
+
const redacted = state.redactionPaths.length > 0 ? _applyRedaction(state, serialized) : serialized;
|
|
160
118
|
return JSON.stringify({
|
|
161
119
|
__flight: true,
|
|
162
|
-
t:
|
|
120
|
+
t: entry.timestampMs,
|
|
163
121
|
level: _levelNames[level],
|
|
164
122
|
channel,
|
|
165
123
|
data: redacted,
|
|
166
124
|
});
|
|
167
125
|
};
|
|
168
126
|
}
|
|
169
|
-
// Creates
|
|
170
|
-
// logWith / logErrorWith / etc. to
|
|
171
|
-
export function createLogContext(
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
//
|
|
177
|
-
//
|
|
178
|
-
//
|
|
179
|
-
|
|
180
|
-
|
|
127
|
+
// Creates an empty bound logging context: no channel and no base fields. Assign `channel` and
|
|
128
|
+
// `fields`, then pass it to logWith / logErrorWith / etc. to merge them into every entry.
|
|
129
|
+
export function createLogContext() {
|
|
130
|
+
const out = allocateEntity();
|
|
131
|
+
initializeLogContext(out);
|
|
132
|
+
return out;
|
|
133
|
+
}
|
|
134
|
+
// Creates a tracing span with the next span id from `state` and an empty name, channel and fields.
|
|
135
|
+
// Assign those, then enterLogSpan to activate it. While active, the span's fields are merged into
|
|
136
|
+
// every emitted entry (span fields have lower priority than direct emit fields). enterLogSpan /
|
|
137
|
+
// exitLogSpan are stack-based: multiple spans can be active simultaneously; the most recently
|
|
138
|
+
// entered span wins on field key collision.
|
|
139
|
+
export function createLogSpan(state) {
|
|
140
|
+
const out = allocateEntity();
|
|
141
|
+
initializeLogSpan(out, state);
|
|
142
|
+
return out;
|
|
143
|
+
}
|
|
144
|
+
// Creates a fresh caller-owned log state: no sinks, global level Verbose, console level Info.
|
|
145
|
+
export function createLogState() {
|
|
146
|
+
const out = allocateEntity();
|
|
147
|
+
initializeLogState(out);
|
|
148
|
+
return out;
|
|
181
149
|
}
|
|
182
150
|
// Creates a sink that captures the last `capacity` entries in a ring buffer. Use
|
|
183
151
|
// getMemoryLogSinkEntries to read (oldest-first) and clearMemoryLogSink to reset.
|
|
184
152
|
export function createMemoryLogSink(capacity) {
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
if (state.buf.length < capacity) {
|
|
189
|
-
state.buf.push(stored);
|
|
190
|
-
}
|
|
191
|
-
else {
|
|
192
|
-
state.buf[state.head] = stored;
|
|
193
|
-
state.head = (state.head + 1) % capacity;
|
|
194
|
-
}
|
|
195
|
-
};
|
|
196
|
-
const handle = initializeLogEntity({ sink });
|
|
197
|
-
_memorySinkStates.set(handle, state);
|
|
198
|
-
return handle;
|
|
153
|
+
const out = allocateEntity();
|
|
154
|
+
initializeMemoryLogSink(out, capacity);
|
|
155
|
+
return out;
|
|
199
156
|
}
|
|
200
|
-
// Creates a sink that forwards at most `maxPerInterval` entries per `intervalMs` window
|
|
201
|
-
// `perChannel` is true, the budget is tracked per channel
|
|
157
|
+
// Creates a sink that forwards at most `maxPerInterval` entries per `intervalMs` window, measured on
|
|
158
|
+
// each entry's `timestampMs`. When `perChannel` is true, the budget is tracked per channel.
|
|
202
159
|
export function createRateLimitedLogSink(target, options) {
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
const sink = (entry) => {
|
|
207
|
-
const now = _timestamp();
|
|
208
|
-
if (now - windowStart >= intervalMs) {
|
|
209
|
-
counts.clear();
|
|
210
|
-
windowStart = now;
|
|
211
|
-
}
|
|
212
|
-
const key = perChannel ? entry.channel : null;
|
|
213
|
-
const current = counts.get(key) ?? 0;
|
|
214
|
-
if (current >= maxPerInterval)
|
|
215
|
-
return;
|
|
216
|
-
counts.set(key, current + 1);
|
|
217
|
-
target(entry);
|
|
218
|
-
};
|
|
219
|
-
return initializeLogEntity({ sink });
|
|
160
|
+
const out = allocateEntity();
|
|
161
|
+
initializeRateLimitedLogSink(out, target, options);
|
|
162
|
+
return out;
|
|
220
163
|
}
|
|
221
164
|
// Creates a sink that forwards approximately 1-in-N entries (probability = 1 / rate when rate > 1).
|
|
222
165
|
export function createSampledLogSink(target, rate) {
|
|
@@ -229,19 +172,23 @@ export function createSampledLogSink(target, rate) {
|
|
|
229
172
|
target(entry);
|
|
230
173
|
};
|
|
231
174
|
}
|
|
232
|
-
// Creates a formatter that produces a human-readable `[channel] message` line. When
|
|
233
|
-
//
|
|
234
|
-
|
|
175
|
+
// Creates a formatter that produces a human-readable `[channel] message` line. When `indentGroups`
|
|
176
|
+
// is true the formatter indents the message by `state`'s current group depth; `timestamp` prints the
|
|
177
|
+
// entry's own timestamp.
|
|
178
|
+
export function createTextLogFormatter(state, options) {
|
|
179
|
+
const indentGroups = options?.indentGroups ?? false;
|
|
180
|
+
const levelPrefix = options?.levelPrefix ?? false;
|
|
181
|
+
const timestamp = options?.timestamp ?? false;
|
|
235
182
|
return (entry) => {
|
|
236
183
|
const { level, channel, data } = entry;
|
|
237
184
|
const parts = [];
|
|
238
|
-
if (
|
|
239
|
-
parts.push(`t=${
|
|
240
|
-
if (
|
|
185
|
+
if (timestamp)
|
|
186
|
+
parts.push(`t=${entry.timestampMs.toFixed(2)}`);
|
|
187
|
+
if (levelPrefix)
|
|
241
188
|
parts.push(_levelNames[level] ?? 'unknown');
|
|
242
189
|
parts.push(channel !== null ? `[${channel}]` : '[flight]');
|
|
243
|
-
if (
|
|
244
|
-
parts.push(' '.repeat(
|
|
190
|
+
if (indentGroups && state.groupDepth > 0)
|
|
191
|
+
parts.push(' '.repeat(state.groupDepth));
|
|
245
192
|
if (typeof data === 'string')
|
|
246
193
|
parts.push(data);
|
|
247
194
|
else
|
|
@@ -249,240 +196,251 @@ export function createTextLogFormatter(options = {}) {
|
|
|
249
196
|
return parts.join(' ');
|
|
250
197
|
};
|
|
251
198
|
}
|
|
252
|
-
//
|
|
253
|
-
// attempting terminal transport teardown. Destroy is attempted even when flush fails or rejects, and
|
|
254
|
-
// both operation outcomes are preserved. Repeated calls await the same teardown without invoking the
|
|
255
|
-
// provider again.
|
|
256
|
-
export async function destroyFileLogSink(handle) {
|
|
257
|
-
const state = _fileLogSinkStates.get(handle);
|
|
258
|
-
if (!state)
|
|
259
|
-
throw new Error('destroyFileLogSink received an unknown handle');
|
|
260
|
-
if (state.status !== 'active') {
|
|
261
|
-
const outcome = await state.destroyPromise;
|
|
262
|
-
return { ...outcome, reason: 'already-destroyed' };
|
|
263
|
-
}
|
|
264
|
-
state.status = 'destroying';
|
|
265
|
-
removeLogSink(handle.sink);
|
|
266
|
-
state.destroyPromise = _destroyFileLogSinkState(state);
|
|
267
|
-
return state.destroyPromise;
|
|
268
|
-
}
|
|
269
|
-
// Disposes a buffered sink: cancels its interval timer and flushes remaining entries. The sink
|
|
270
|
-
// remains callable after disposal but will no longer auto-flush. (`dispose*` because this
|
|
271
|
-
// detaches the timer that keeps the sink reachable — no non-GC resource.)
|
|
199
|
+
// Disposes a buffered sink: flushes remaining entries. The sink stays callable afterwards.
|
|
272
200
|
export function disposeLogSink(handle) {
|
|
273
201
|
const state = _bufferedSinkStates.get(handle);
|
|
274
|
-
if (
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
_logSignals = initializeLogEntity({
|
|
288
|
-
onLogEntry: createSignal(),
|
|
289
|
-
onLogError: createSignal(),
|
|
290
|
-
});
|
|
291
|
-
return _logSignals;
|
|
202
|
+
if (state)
|
|
203
|
+
_flushBufferedState(state);
|
|
204
|
+
}
|
|
205
|
+
// Enables the log signals group on `state`. Calling it again returns the same LogSignals. Tree-shakes
|
|
206
|
+
// away unless this function is called. The signals are emitted synchronously in the fan-out path.
|
|
207
|
+
export function enableLogSignals(state) {
|
|
208
|
+
if (state.signals !== null)
|
|
209
|
+
return state.signals;
|
|
210
|
+
const signals = allocateEntity();
|
|
211
|
+
signals.onLogEntry = createSignal();
|
|
212
|
+
signals.onLogError = createSignal();
|
|
213
|
+
state.signals = signals;
|
|
214
|
+
return state.signals;
|
|
292
215
|
}
|
|
293
216
|
// Closes the innermost open log group (decrements nesting depth). Emits a Debug entry marking
|
|
294
217
|
// the group end. No-op if no group is open.
|
|
295
|
-
export function endLogGroup(channel = null) {
|
|
296
|
-
if (
|
|
218
|
+
export function endLogGroup(state, nowMs, channel = null) {
|
|
219
|
+
if (state.groupDepth <= 0)
|
|
297
220
|
return;
|
|
298
|
-
|
|
299
|
-
if (!_passesLevelGate(LogLevel.Debug, channel))
|
|
221
|
+
state.groupDepth--;
|
|
222
|
+
if (!_passesLevelGate(state, LogLevel.Debug, channel))
|
|
300
223
|
return;
|
|
301
|
-
_emitToSinks(
|
|
224
|
+
_emitToSinks(state, {
|
|
225
|
+
level: LogLevel.Debug,
|
|
226
|
+
channel,
|
|
227
|
+
data: { group: 'end', depth: state.groupDepth + 1 },
|
|
228
|
+
timestampMs: nowMs,
|
|
229
|
+
});
|
|
302
230
|
}
|
|
303
231
|
// Ends a timer, emits a structured Debug entry with the elapsed milliseconds, and returns the
|
|
304
232
|
// elapsed time in milliseconds.
|
|
305
|
-
export function endLogTimer(timer) {
|
|
306
|
-
const elapsed =
|
|
307
|
-
logDebug({ label: timer.label, elapsedMs: elapsed }, timer.channel);
|
|
233
|
+
export function endLogTimer(state, nowMs, timer) {
|
|
234
|
+
const elapsed = nowMs - timer.startedAt;
|
|
235
|
+
logDebug(state, nowMs, { label: timer.label, elapsedMs: elapsed }, timer.channel);
|
|
308
236
|
return elapsed;
|
|
309
237
|
}
|
|
310
238
|
// Activates a log span, pushing it onto the active-span stack. While active, the span's fields
|
|
311
|
-
//
|
|
312
|
-
//
|
|
313
|
-
export function enterLogSpan(span) {
|
|
314
|
-
|
|
239
|
+
// are merged into every emitted entry. The span with the highest stack index (most recently
|
|
240
|
+
// entered) wins on field key collision. Pair every enterLogSpan with exitLogSpan.
|
|
241
|
+
export function enterLogSpan(state, span) {
|
|
242
|
+
state.spans.push(span);
|
|
315
243
|
}
|
|
316
244
|
// Deactivates a log span by removing it from the active-span stack. The span need not be the
|
|
317
|
-
// topmost — it is removed by
|
|
318
|
-
//
|
|
319
|
-
export function exitLogSpan(span) {
|
|
320
|
-
const
|
|
321
|
-
if (
|
|
322
|
-
|
|
245
|
+
// topmost — it is removed by id from wherever it appears in the stack, supporting both strict LIFO
|
|
246
|
+
// and out-of-order unwinding. No-op if the span is not in the stack.
|
|
247
|
+
export function exitLogSpan(state, span) {
|
|
248
|
+
const index = state.spans.findIndex((active) => active.id === span.id);
|
|
249
|
+
if (index >= 0)
|
|
250
|
+
state.spans.splice(index, 1);
|
|
323
251
|
}
|
|
324
252
|
// Flushes a buffered sink immediately, forwarding all queued entries to its target.
|
|
325
253
|
export function flushLogSink(handle) {
|
|
326
254
|
const state = _bufferedSinkStates.get(handle);
|
|
327
255
|
if (state)
|
|
328
|
-
state
|
|
256
|
+
_flushBufferedState(state);
|
|
329
257
|
}
|
|
330
258
|
// Listener side. Per-channel level override. Returns null when no channel-specific level is set
|
|
331
259
|
// (the channel inherits the global level).
|
|
332
|
-
export function getLogChannelLevel(channel) {
|
|
333
|
-
return
|
|
260
|
+
export function getLogChannelLevel(state, channel) {
|
|
261
|
+
return state.channelLevels.get(channel) ?? null;
|
|
334
262
|
}
|
|
335
263
|
// Listener side. Reads the human-readable console threshold (default LogLevel.Info). Capture is
|
|
336
264
|
// unaffected by it — the console-capture sink records every level regardless.
|
|
337
|
-
export function getLogConsoleLevel() {
|
|
338
|
-
return
|
|
265
|
+
export function getLogConsoleLevel(state) {
|
|
266
|
+
return state.consoleLevel;
|
|
339
267
|
}
|
|
340
|
-
// Listener side. Reads the global minimum emit level (default LogLevel.Verbose — emit
|
|
341
|
-
//
|
|
342
|
-
export function getLogLevel() {
|
|
343
|
-
return
|
|
268
|
+
// Listener side. Reads the global minimum emit level (default LogLevel.Verbose — emit everything).
|
|
269
|
+
// Lower-priority levels (higher numeric value) are suppressed before any sink work.
|
|
270
|
+
export function getLogLevel(state) {
|
|
271
|
+
return state.level;
|
|
344
272
|
}
|
|
345
273
|
// Listener side. Returns the canonical lowercase name for a LogLevel value.
|
|
346
274
|
export function getLogLevelName(level) {
|
|
347
275
|
return _levelNames[level] ?? 'unknown';
|
|
348
276
|
}
|
|
349
|
-
//
|
|
350
|
-
|
|
277
|
+
// Writes the captured log entries from a memory sink into `out` (cleared first) in insertion order
|
|
278
|
+
// (oldest-first).
|
|
279
|
+
export function getMemoryLogSinkEntries(out, handle) {
|
|
280
|
+
out.length = 0;
|
|
351
281
|
const state = _memorySinkStates.get(handle);
|
|
352
282
|
if (!state)
|
|
353
|
-
return
|
|
283
|
+
return;
|
|
354
284
|
const { buf, head } = state;
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
285
|
+
for (let i = head; i < buf.length; i++)
|
|
286
|
+
out.push(buf[i]);
|
|
287
|
+
for (let i = 0; i < head; i++)
|
|
288
|
+
out.push(buf[i]);
|
|
289
|
+
}
|
|
290
|
+
// Writes a buffered sink over `target` into `out` (the construction initializer of
|
|
291
|
+
// createBufferedLogSink).
|
|
292
|
+
export function initializeBufferedLogSink(out, target, options) {
|
|
293
|
+
const state = {
|
|
294
|
+
buf: [],
|
|
295
|
+
intervalMs: options?.intervalMs ?? 1000,
|
|
296
|
+
lastFlushMs: null,
|
|
297
|
+
size: options?.size ?? 100,
|
|
298
|
+
target,
|
|
299
|
+
};
|
|
300
|
+
out.sink = (entry) => {
|
|
301
|
+
state.buf.push(_copyLogEntry(entry));
|
|
302
|
+
if (state.buf.length >= state.size)
|
|
303
|
+
_flushBufferedState(state);
|
|
304
|
+
};
|
|
305
|
+
_bufferedSinkStates.set(out, state);
|
|
306
|
+
}
|
|
307
|
+
// Resets a context to no channel and no base fields (the construction initializer of
|
|
308
|
+
// createLogContext).
|
|
309
|
+
export function initializeLogContext(out) {
|
|
310
|
+
out.channel = null;
|
|
311
|
+
out.fields = {};
|
|
312
|
+
}
|
|
313
|
+
// Gives `out` the next span id from `state` and an empty name, channel and fields (the construction
|
|
314
|
+
// initializer of createLogSpan).
|
|
315
|
+
export function initializeLogSpan(out, state) {
|
|
316
|
+
out.id = state.nextSpanId++;
|
|
317
|
+
out.name = '';
|
|
318
|
+
out.channel = null;
|
|
319
|
+
out.fields = {};
|
|
320
|
+
}
|
|
321
|
+
// Resets `out` to a fresh log state (the construction initializer of createLogState).
|
|
322
|
+
export function initializeLogState(out) {
|
|
323
|
+
out.sinks = [];
|
|
324
|
+
out.level = LogLevel.Verbose;
|
|
325
|
+
out.consoleLevel = LogLevel.Info;
|
|
326
|
+
out.channelLevels = new Map();
|
|
327
|
+
out.onceKeys = new Set();
|
|
328
|
+
out.redactionPaths = [];
|
|
329
|
+
out.serializers = new Map();
|
|
330
|
+
out.groupDepth = 0;
|
|
331
|
+
out.spans = [];
|
|
332
|
+
out.signals = null;
|
|
333
|
+
out.nextSpanId = 1;
|
|
334
|
+
}
|
|
335
|
+
// Writes an empty ring buffer of `capacity` entries into `out` (the construction initializer of
|
|
336
|
+
// createMemoryLogSink).
|
|
337
|
+
export function initializeMemoryLogSink(out, capacity) {
|
|
338
|
+
const state = { buf: [], head: 0 };
|
|
339
|
+
out.sink = (entry) => {
|
|
340
|
+
const stored = _copyLogEntry(entry);
|
|
341
|
+
if (state.buf.length < capacity) {
|
|
342
|
+
state.buf.push(stored);
|
|
343
|
+
}
|
|
344
|
+
else {
|
|
345
|
+
state.buf[state.head] = stored;
|
|
346
|
+
state.head = (state.head + 1) % capacity;
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
_memorySinkStates.set(out, state);
|
|
350
|
+
}
|
|
351
|
+
// Writes a rate-limited sink over `target` into `out` (the construction initializer of
|
|
352
|
+
// createRateLimitedLogSink).
|
|
353
|
+
export function initializeRateLimitedLogSink(out, target, options) {
|
|
354
|
+
const perChannel = options.perChannel ?? false;
|
|
355
|
+
const { maxPerInterval, intervalMs } = options;
|
|
356
|
+
const counts = new Map();
|
|
357
|
+
let windowStartMs = null;
|
|
358
|
+
out.sink = (entry) => {
|
|
359
|
+
const now = entry.timestampMs;
|
|
360
|
+
if (windowStartMs === null) {
|
|
361
|
+
windowStartMs = now;
|
|
362
|
+
}
|
|
363
|
+
else if (now - windowStartMs >= intervalMs) {
|
|
364
|
+
counts.clear();
|
|
365
|
+
windowStartMs = now;
|
|
366
|
+
}
|
|
367
|
+
const key = perChannel ? entry.channel : null;
|
|
368
|
+
const current = counts.get(key) ?? 0;
|
|
369
|
+
if (current >= maxPerInterval)
|
|
370
|
+
return;
|
|
371
|
+
counts.set(key, current + 1);
|
|
372
|
+
target(entry);
|
|
373
|
+
};
|
|
360
374
|
}
|
|
361
375
|
// Emit side. Emits a log entry at an explicit level. `channel` is a free categorization tag
|
|
362
376
|
// (e.g. 'batch', 'shader', 'user') for filtering captured output. Accepts a plain LogData value
|
|
363
377
|
// or a LogDataProvider thunk — the thunk is not called unless the entry passes the level gate,
|
|
364
378
|
// making suppressed verbose calls allocation-free. No-ops when no sinks are installed or when
|
|
365
379
|
// the global or per-channel level gate suppresses the entry.
|
|
366
|
-
export function log(level, data, channel = null) {
|
|
367
|
-
if (!_passesLevelGate(level, channel))
|
|
380
|
+
export function log(state, nowMs, level, data, channel = null) {
|
|
381
|
+
if (!_passesLevelGate(state, level, channel))
|
|
368
382
|
return;
|
|
369
|
-
|
|
370
|
-
const entry = { level, channel, data: _mergeSpanFields(resolved, channel) };
|
|
371
|
-
_emitToSinks(entry);
|
|
383
|
+
_emitToSinks(state, { level, channel, data: _mergeSpanFields(state, _resolveData(data)), timestampMs: nowMs });
|
|
372
384
|
}
|
|
373
385
|
// Emit side. Emits an Error-level entry only when condition is false. Never throws — sentinel
|
|
374
386
|
// behavior for assertion-style diagnostics.
|
|
375
|
-
export function logAssert(condition, data, channel = null) {
|
|
387
|
+
export function logAssert(state, nowMs, condition, data, channel = null) {
|
|
376
388
|
if (condition)
|
|
377
389
|
return;
|
|
378
|
-
|
|
379
|
-
return;
|
|
380
|
-
const resolved = typeof data === 'function' ? data() : data;
|
|
381
|
-
_emitToSinks({ level: LogLevel.Error, channel, data: _mergeSpanFields(resolved, channel) });
|
|
390
|
+
log(state, nowMs, LogLevel.Error, data, channel);
|
|
382
391
|
}
|
|
383
392
|
// Emit side. Severity-named convenience wrappers over log (mirrors console.debug/info/warn/error).
|
|
384
|
-
export function logDebug(data, channel = null) {
|
|
385
|
-
|
|
386
|
-
return;
|
|
387
|
-
const resolved = typeof data === 'function' ? data() : data;
|
|
388
|
-
_emitToSinks({ level: LogLevel.Debug, channel, data: _mergeSpanFields(resolved, channel) });
|
|
393
|
+
export function logDebug(state, nowMs, data, channel = null) {
|
|
394
|
+
log(state, nowMs, LogLevel.Debug, data, channel);
|
|
389
395
|
}
|
|
390
|
-
export function logDebugWith(context, data) {
|
|
391
|
-
|
|
392
|
-
if (!_passesLevelGate(LogLevel.Debug, channel))
|
|
393
|
-
return;
|
|
394
|
-
const resolved = typeof data === 'function' ? data() : data;
|
|
395
|
-
_emitToSinks({
|
|
396
|
-
level: LogLevel.Debug,
|
|
397
|
-
channel,
|
|
398
|
-
data: _mergeContextFields(context, _mergeSpanFields(resolved, channel)),
|
|
399
|
-
});
|
|
396
|
+
export function logDebugWith(state, nowMs, context, data) {
|
|
397
|
+
logWith(state, nowMs, context, LogLevel.Debug, data);
|
|
400
398
|
}
|
|
401
|
-
export function logError(data, channel = null) {
|
|
402
|
-
|
|
403
|
-
return;
|
|
404
|
-
const resolved = typeof data === 'function' ? data() : data;
|
|
405
|
-
_emitToSinks({ level: LogLevel.Error, channel, data: _mergeSpanFields(resolved, channel) });
|
|
399
|
+
export function logError(state, nowMs, data, channel = null) {
|
|
400
|
+
log(state, nowMs, LogLevel.Error, data, channel);
|
|
406
401
|
}
|
|
407
|
-
export function logErrorWith(context, data) {
|
|
408
|
-
|
|
409
|
-
if (!_passesLevelGate(LogLevel.Error, channel))
|
|
410
|
-
return;
|
|
411
|
-
const resolved = typeof data === 'function' ? data() : data;
|
|
412
|
-
_emitToSinks({
|
|
413
|
-
level: LogLevel.Error,
|
|
414
|
-
channel,
|
|
415
|
-
data: _mergeContextFields(context, _mergeSpanFields(resolved, channel)),
|
|
416
|
-
});
|
|
402
|
+
export function logErrorWith(state, nowMs, context, data) {
|
|
403
|
+
logWith(state, nowMs, context, LogLevel.Error, data);
|
|
417
404
|
}
|
|
418
|
-
export function logInfo(data, channel = null) {
|
|
419
|
-
|
|
420
|
-
return;
|
|
421
|
-
const resolved = typeof data === 'function' ? data() : data;
|
|
422
|
-
_emitToSinks({ level: LogLevel.Info, channel, data: _mergeSpanFields(resolved, channel) });
|
|
405
|
+
export function logInfo(state, nowMs, data, channel = null) {
|
|
406
|
+
log(state, nowMs, LogLevel.Info, data, channel);
|
|
423
407
|
}
|
|
424
|
-
export function logInfoWith(context, data) {
|
|
425
|
-
|
|
426
|
-
if (!_passesLevelGate(LogLevel.Info, channel))
|
|
427
|
-
return;
|
|
428
|
-
const resolved = typeof data === 'function' ? data() : data;
|
|
429
|
-
_emitToSinks({
|
|
430
|
-
level: LogLevel.Info,
|
|
431
|
-
channel,
|
|
432
|
-
data: _mergeContextFields(context, _mergeSpanFields(resolved, channel)),
|
|
433
|
-
});
|
|
408
|
+
export function logInfoWith(state, nowMs, context, data) {
|
|
409
|
+
logWith(state, nowMs, context, LogLevel.Info, data);
|
|
434
410
|
}
|
|
435
|
-
// Emit side. Emits a given key at most once per
|
|
436
|
-
//
|
|
437
|
-
export function logOnce(key, level, data, channel = null) {
|
|
438
|
-
if (
|
|
411
|
+
// Emit side. Emits a given key at most once per LogState (useful for warmup warnings and deprecation
|
|
412
|
+
// notices). Returns true if the entry was emitted, false if it was suppressed.
|
|
413
|
+
export function logOnce(state, nowMs, key, level, data, channel = null) {
|
|
414
|
+
if (state.onceKeys.has(key))
|
|
439
415
|
return false;
|
|
440
|
-
|
|
441
|
-
log(level, data, channel);
|
|
416
|
+
state.onceKeys.add(key);
|
|
417
|
+
log(state, nowMs, level, data, channel);
|
|
442
418
|
return true;
|
|
443
419
|
}
|
|
444
|
-
export function logVerbose(data, channel = null) {
|
|
445
|
-
|
|
446
|
-
return;
|
|
447
|
-
const resolved = typeof data === 'function' ? data() : data;
|
|
448
|
-
_emitToSinks({ level: LogLevel.Verbose, channel, data: _mergeSpanFields(resolved, channel) });
|
|
420
|
+
export function logVerbose(state, nowMs, data, channel = null) {
|
|
421
|
+
log(state, nowMs, LogLevel.Verbose, data, channel);
|
|
449
422
|
}
|
|
450
|
-
export function logVerboseWith(context, data) {
|
|
451
|
-
|
|
452
|
-
if (!_passesLevelGate(LogLevel.Verbose, channel))
|
|
453
|
-
return;
|
|
454
|
-
const resolved = typeof data === 'function' ? data() : data;
|
|
455
|
-
_emitToSinks({
|
|
456
|
-
level: LogLevel.Verbose,
|
|
457
|
-
channel,
|
|
458
|
-
data: _mergeContextFields(context, _mergeSpanFields(resolved, channel)),
|
|
459
|
-
});
|
|
423
|
+
export function logVerboseWith(state, nowMs, context, data) {
|
|
424
|
+
logWith(state, nowMs, context, LogLevel.Verbose, data);
|
|
460
425
|
}
|
|
461
|
-
export function logWarn(data, channel = null) {
|
|
462
|
-
|
|
463
|
-
return;
|
|
464
|
-
const resolved = typeof data === 'function' ? data() : data;
|
|
465
|
-
_emitToSinks({ level: LogLevel.Warn, channel, data: _mergeSpanFields(resolved, channel) });
|
|
426
|
+
export function logWarn(state, nowMs, data, channel = null) {
|
|
427
|
+
log(state, nowMs, LogLevel.Warn, data, channel);
|
|
466
428
|
}
|
|
467
|
-
export function logWarnWith(context, data) {
|
|
468
|
-
|
|
469
|
-
if (!_passesLevelGate(LogLevel.Warn, channel))
|
|
470
|
-
return;
|
|
471
|
-
const resolved = typeof data === 'function' ? data() : data;
|
|
472
|
-
_emitToSinks({
|
|
473
|
-
level: LogLevel.Warn,
|
|
474
|
-
channel,
|
|
475
|
-
data: _mergeContextFields(context, _mergeSpanFields(resolved, channel)),
|
|
476
|
-
});
|
|
429
|
+
export function logWarnWith(state, nowMs, context, data) {
|
|
430
|
+
logWith(state, nowMs, context, LogLevel.Warn, data);
|
|
477
431
|
}
|
|
478
432
|
// Emit side. Emits a log entry using the bound channel and merged fields from a LogContext. The
|
|
479
433
|
// context's fields are merged into the entry's data (string data becomes { msg, ...fields }).
|
|
480
|
-
export function logWith(context, level, data) {
|
|
434
|
+
export function logWith(state, nowMs, context, level, data) {
|
|
481
435
|
const { channel } = context;
|
|
482
|
-
if (!_passesLevelGate(level, channel))
|
|
436
|
+
if (!_passesLevelGate(state, level, channel))
|
|
483
437
|
return;
|
|
484
|
-
|
|
485
|
-
|
|
438
|
+
_emitToSinks(state, {
|
|
439
|
+
level,
|
|
440
|
+
channel,
|
|
441
|
+
data: _mergeContextFields(context, _mergeSpanFields(state, _resolveData(data))),
|
|
442
|
+
timestampMs: nowMs,
|
|
443
|
+
});
|
|
486
444
|
}
|
|
487
445
|
// Listener side. Parses a level name (case-insensitive) back to a LogLevel. Returns null for
|
|
488
446
|
// unknown names (sentinel, not a throw).
|
|
@@ -490,19 +448,19 @@ export function parseLogLevel(name) {
|
|
|
490
448
|
return _levelByName.get(name.toLowerCase()) ?? null;
|
|
491
449
|
}
|
|
492
450
|
// Registers a custom serializer for a named kind. When the JSON formatter encounters a field
|
|
493
|
-
// value whose `__kind` property matches `kind`, it calls `
|
|
451
|
+
// value whose `__kind` property matches `kind`, it calls `serializer` to convert the value to a plain
|
|
494
452
|
// record. Kind strings follow the `*Kind`-style string registry: last-write-wins; vendor-prefix
|
|
495
453
|
// custom kinds (e.g. 'acme.Foo') to avoid collisions with built-in kinds (bare names reserved).
|
|
496
|
-
export function registerLogSerializer(kind,
|
|
497
|
-
|
|
454
|
+
export function registerLogSerializer(state, kind, serializer) {
|
|
455
|
+
state.serializers.set(kind, serializer);
|
|
498
456
|
}
|
|
499
457
|
// Listener side. Removes a sink from the fan-out list. Returns false if not present (sentinel,
|
|
500
458
|
// not a throw).
|
|
501
|
-
export function removeLogSink(sink) {
|
|
502
|
-
const
|
|
503
|
-
if (
|
|
459
|
+
export function removeLogSink(state, sink) {
|
|
460
|
+
const index = state.sinks.indexOf(sink);
|
|
461
|
+
if (index < 0)
|
|
504
462
|
return false;
|
|
505
|
-
|
|
463
|
+
state.sinks.splice(index, 1);
|
|
506
464
|
return true;
|
|
507
465
|
}
|
|
508
466
|
// Extracts name/message/stack/cause from an Error (recursively) into a plain record suitable
|
|
@@ -522,50 +480,43 @@ export function serializeLogError(value) {
|
|
|
522
480
|
}
|
|
523
481
|
// Listener side. Sets a per-channel minimum emit level. Resolution order: channel level if set,
|
|
524
482
|
// else global level. Use getLogChannelLevel to read, clearLogChannelLevels to reset.
|
|
525
|
-
export function setLogChannelLevel(channel, level) {
|
|
526
|
-
|
|
483
|
+
export function setLogChannelLevel(state, channel, level) {
|
|
484
|
+
state.channelLevels.set(channel, level);
|
|
527
485
|
}
|
|
528
|
-
// Listener side. Sets the highest level printed as a human-readable console line
|
|
529
|
-
// silences those lines; the capture record still receives every level.
|
|
530
|
-
export function setLogConsoleLevel(level) {
|
|
531
|
-
|
|
486
|
+
// Listener side. Sets the highest level printed as a human-readable console line by the capture
|
|
487
|
+
// sink. LogLevel.None silences those lines; the capture record still receives every level.
|
|
488
|
+
export function setLogConsoleLevel(state, level) {
|
|
489
|
+
state.consoleLevel = level;
|
|
532
490
|
}
|
|
533
491
|
// Listener side. Sets the global minimum emit level. Entries below this level are suppressed
|
|
534
492
|
// before any sink work, making suppressed verbose logging genuinely free in hot paths.
|
|
535
|
-
export function setLogLevel(level) {
|
|
536
|
-
|
|
493
|
+
export function setLogLevel(state, level) {
|
|
494
|
+
state.level = level;
|
|
537
495
|
}
|
|
538
496
|
// Sets the redaction paths applied by the JSON formatter. Paths use dot notation to target
|
|
539
497
|
// nested fields (e.g. ['headers.authorization', 'user.token']). Matching field values are
|
|
540
498
|
// replaced with '[REDACTED]'. Pass an empty array to disable redaction.
|
|
541
|
-
export function setLogRedactionPaths(paths) {
|
|
542
|
-
|
|
499
|
+
export function setLogRedactionPaths(state, paths) {
|
|
500
|
+
state.redactionPaths.length = 0;
|
|
543
501
|
for (const p of paths)
|
|
544
|
-
|
|
502
|
+
state.redactionPaths.push(p);
|
|
545
503
|
}
|
|
546
504
|
// Installs (or clears, with null) the single sink — clears the list then adds the new sink if
|
|
547
|
-
// non-null.
|
|
548
|
-
export function setLogSink(sink) {
|
|
549
|
-
|
|
505
|
+
// non-null.
|
|
506
|
+
export function setLogSink(state, sink) {
|
|
507
|
+
state.sinks.length = 0;
|
|
550
508
|
if (sink !== null)
|
|
551
|
-
|
|
509
|
+
state.sinks.push(sink);
|
|
552
510
|
}
|
|
553
|
-
// Starts a named timer
|
|
554
|
-
// emit a structured Debug entry.
|
|
555
|
-
export function startLogTimer(label, channel
|
|
556
|
-
return { label, channel, startedAt:
|
|
511
|
+
// Starts a named timer at the caller's clock reading `nowMs`. Pass the returned LogTimer to
|
|
512
|
+
// endLogTimer to record elapsed time and emit a structured Debug entry.
|
|
513
|
+
export function startLogTimer(label, channel, nowMs) {
|
|
514
|
+
return { label, channel, startedAt: nowMs };
|
|
557
515
|
}
|
|
516
|
+
// Per-handle private sink state, keyed by the handle so it is collected with it. The handle's public
|
|
517
|
+
// shape stays `{ sink }`; nothing outside this module can reach the queue or the ring buffer.
|
|
558
518
|
const _bufferedSinkStates = new WeakMap();
|
|
559
|
-
const _fileLogSinkStates = new WeakMap();
|
|
560
519
|
const _memorySinkStates = new WeakMap();
|
|
561
|
-
const _consoleMethods = {
|
|
562
|
-
[LogLevel.None]: 'log',
|
|
563
|
-
[LogLevel.Error]: 'error',
|
|
564
|
-
[LogLevel.Warn]: 'warn',
|
|
565
|
-
[LogLevel.Info]: 'info',
|
|
566
|
-
[LogLevel.Debug]: 'debug',
|
|
567
|
-
[LogLevel.Verbose]: 'log',
|
|
568
|
-
};
|
|
569
520
|
const _levelNames = {
|
|
570
521
|
[LogLevel.None]: 'none',
|
|
571
522
|
[LogLevel.Error]: 'error',
|
|
@@ -582,46 +533,18 @@ const _levelByName = new Map([
|
|
|
582
533
|
['debug', LogLevel.Debug],
|
|
583
534
|
['verbose', LogLevel.Verbose],
|
|
584
535
|
]);
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
const
|
|
589
|
-
const
|
|
590
|
-
|
|
591
|
-
let _consoleLevel = LogLevel.Info;
|
|
592
|
-
let _groupDepth = 0;
|
|
593
|
-
let _level = LogLevel.Verbose;
|
|
594
|
-
let _logSignals = null;
|
|
595
|
-
async function _destroyFileLogSinkState(state) {
|
|
596
|
-
const transport = state.transport;
|
|
597
|
-
let flush;
|
|
598
|
-
let destroy;
|
|
599
|
-
try {
|
|
600
|
-
flush = await transport.flush();
|
|
601
|
-
}
|
|
602
|
-
catch (error) {
|
|
603
|
-
flush = { message: _operationFailureMessage(error), reason: 'operation-failed' };
|
|
604
|
-
}
|
|
605
|
-
try {
|
|
606
|
-
destroy = await transport.destroy();
|
|
607
|
-
}
|
|
608
|
-
catch (error) {
|
|
609
|
-
destroy = { message: _operationFailureMessage(error), reason: 'operation-failed' };
|
|
536
|
+
// Applies dot-notation redaction paths to a record, replacing matching values with '[REDACTED]'.
|
|
537
|
+
function _applyRedaction(state, data) {
|
|
538
|
+
// Shallow copy at root; only deep-copy when traversal requires it.
|
|
539
|
+
const result = { ...data };
|
|
540
|
+
for (const path of state.redactionPaths) {
|
|
541
|
+
_redactPath(result, path.split('.'), 0);
|
|
610
542
|
}
|
|
611
|
-
|
|
612
|
-
state.status = 'destroyed';
|
|
613
|
-
return {
|
|
614
|
-
destroy,
|
|
615
|
-
flush,
|
|
616
|
-
reason: flush.reason === 'operation-failed' || destroy.reason === 'operation-failed' ? 'operation-failed' : 'destroyed',
|
|
617
|
-
};
|
|
618
|
-
}
|
|
619
|
-
function _operationFailureMessage(error) {
|
|
620
|
-
return error instanceof Error ? error.message : String(error);
|
|
543
|
+
return result;
|
|
621
544
|
}
|
|
622
545
|
// Applies registered serializers to values whose `__kind` matches a registered kind.
|
|
623
|
-
function _applySerializers(data) {
|
|
624
|
-
if (
|
|
546
|
+
function _applySerializers(state, data) {
|
|
547
|
+
if (state.serializers.size === 0)
|
|
625
548
|
return data;
|
|
626
549
|
const result = {};
|
|
627
550
|
for (const [key, value] of Object.entries(data)) {
|
|
@@ -629,9 +552,8 @@ function _applySerializers(data) {
|
|
|
629
552
|
typeof value === 'object' &&
|
|
630
553
|
'__kind' in value &&
|
|
631
554
|
typeof value.__kind === 'string') {
|
|
632
|
-
const
|
|
633
|
-
|
|
634
|
-
result[key] = fn ? fn(value) : value;
|
|
555
|
+
const serializer = state.serializers.get(value.__kind);
|
|
556
|
+
result[key] = serializer ? serializer(value) : value;
|
|
635
557
|
}
|
|
636
558
|
else {
|
|
637
559
|
result[key] = value;
|
|
@@ -639,40 +561,28 @@ function _applySerializers(data) {
|
|
|
639
561
|
}
|
|
640
562
|
return result;
|
|
641
563
|
}
|
|
642
|
-
//
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
for (const path of _redactionPaths) {
|
|
647
|
-
const parts = path.split('.');
|
|
648
|
-
_redactPath(result, parts, 0);
|
|
649
|
-
}
|
|
650
|
-
return result;
|
|
651
|
-
}
|
|
652
|
-
function _redactPath(obj, parts, idx) {
|
|
653
|
-
const key = parts[idx];
|
|
654
|
-
if (!(key in obj))
|
|
655
|
-
return;
|
|
656
|
-
if (idx === parts.length - 1) {
|
|
657
|
-
obj[key] = '[REDACTED]';
|
|
658
|
-
return;
|
|
659
|
-
}
|
|
660
|
-
const next = obj[key];
|
|
661
|
-
if (next !== null && typeof next === 'object' && !Array.isArray(next)) {
|
|
662
|
-
// Deep-copy one level before mutating.
|
|
663
|
-
obj[key] = { ...next };
|
|
664
|
-
_redactPath(obj[key], parts, idx + 1);
|
|
665
|
-
}
|
|
564
|
+
// Sinks that hold entries past the call keep their own copy, so a caller reusing an entry object
|
|
565
|
+
// cannot rewrite what was captured.
|
|
566
|
+
function _copyLogEntry(entry) {
|
|
567
|
+
return { level: entry.level, channel: entry.channel, data: entry.data, timestampMs: entry.timestampMs };
|
|
666
568
|
}
|
|
667
|
-
function _emitToSinks(entry) {
|
|
668
|
-
for (const sink of
|
|
569
|
+
function _emitToSinks(state, entry) {
|
|
570
|
+
for (const sink of state.sinks)
|
|
669
571
|
sink(entry);
|
|
670
|
-
|
|
671
|
-
|
|
572
|
+
const signals = state.signals;
|
|
573
|
+
if (signals !== null) {
|
|
574
|
+
emitSignal(signals.onLogEntry, entry);
|
|
672
575
|
if (entry.level === LogLevel.Error)
|
|
673
|
-
emitSignal(
|
|
576
|
+
emitSignal(signals.onLogError, entry);
|
|
674
577
|
}
|
|
675
578
|
}
|
|
579
|
+
function _flushBufferedState(state) {
|
|
580
|
+
if (state.buf.length === 0)
|
|
581
|
+
return;
|
|
582
|
+
const batch = state.buf.splice(0);
|
|
583
|
+
for (const entry of batch)
|
|
584
|
+
state.target(entry);
|
|
585
|
+
}
|
|
676
586
|
function _mergeContextFields(context, data) {
|
|
677
587
|
const { fields } = context;
|
|
678
588
|
if (Object.keys(fields).length === 0)
|
|
@@ -683,12 +593,12 @@ function _mergeContextFields(context, data) {
|
|
|
683
593
|
}
|
|
684
594
|
// Merges active span fields into emitted data. Span fields have lower priority than data fields;
|
|
685
595
|
// later spans in the stack win over earlier ones on key collision within span fields.
|
|
686
|
-
function _mergeSpanFields(
|
|
687
|
-
if (
|
|
596
|
+
function _mergeSpanFields(state, data) {
|
|
597
|
+
if (state.spans.length === 0)
|
|
688
598
|
return data;
|
|
689
599
|
// Accumulate span fields oldest-first so newer spans overwrite older ones.
|
|
690
600
|
const spanFields = {};
|
|
691
|
-
for (const span of
|
|
601
|
+
for (const span of state.spans) {
|
|
692
602
|
Object.assign(spanFields, span.fields);
|
|
693
603
|
}
|
|
694
604
|
if (Object.keys(spanFields).length === 0)
|
|
@@ -698,52 +608,29 @@ function _mergeSpanFields(data, _channel) {
|
|
|
698
608
|
return { msg: data, ...spanFields };
|
|
699
609
|
return { ...spanFields, ...data };
|
|
700
610
|
}
|
|
701
|
-
function _passesLevelGate(level, channel) {
|
|
702
|
-
if (
|
|
611
|
+
function _passesLevelGate(state, level, channel) {
|
|
612
|
+
if (state.sinks.length === 0 && state.signals === null)
|
|
703
613
|
return false;
|
|
704
|
-
const
|
|
614
|
+
const channelLevel = channel !== null ? state.channelLevels.get(channel) : undefined;
|
|
615
|
+
const gate = channelLevel ?? state.level;
|
|
705
616
|
return level <= gate && level !== LogLevel.None;
|
|
706
617
|
}
|
|
707
|
-
function
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
return entity;
|
|
714
|
-
}
|
|
715
|
-
const _defaultJsonFormatter = (entry) => {
|
|
716
|
-
const { level, channel, data } = entry;
|
|
717
|
-
return JSON.stringify({
|
|
718
|
-
__flight: true,
|
|
719
|
-
t: _timestamp(),
|
|
720
|
-
level: _levelNames[level],
|
|
721
|
-
channel,
|
|
722
|
-
data: typeof data === 'string' ? { msg: data } : data,
|
|
723
|
-
});
|
|
724
|
-
};
|
|
725
|
-
/* eslint-disable no-console -- this is the console-capture sink; writing to the console is its job */
|
|
726
|
-
function _writeConsoleCaptureEntry(entry, envelopeFormatter) {
|
|
727
|
-
if (typeof console === 'undefined')
|
|
618
|
+
function _redactPath(obj, parts, index) {
|
|
619
|
+
const key = parts[index];
|
|
620
|
+
if (!(key in obj))
|
|
621
|
+
return;
|
|
622
|
+
if (index === parts.length - 1) {
|
|
623
|
+
obj[key] = '[REDACTED]';
|
|
728
624
|
return;
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
const prefix = channel !== null ? `[${channel}]` : '[flight]';
|
|
736
|
-
const { data } = entry;
|
|
737
|
-
if (typeof data === 'string')
|
|
738
|
-
console[method](`${prefix} ${data}`);
|
|
739
|
-
else
|
|
740
|
-
console[method](prefix, data);
|
|
625
|
+
}
|
|
626
|
+
const next = obj[key];
|
|
627
|
+
if (next !== null && typeof next === 'object' && !Array.isArray(next)) {
|
|
628
|
+
// Deep-copy one level before mutating.
|
|
629
|
+
obj[key] = { ...next };
|
|
630
|
+
_redactPath(obj[key], parts, index + 1);
|
|
741
631
|
}
|
|
742
632
|
}
|
|
743
|
-
function
|
|
744
|
-
|
|
745
|
-
return;
|
|
746
|
-
console[_consoleMethods[entry.level]](formatter(entry));
|
|
633
|
+
function _resolveData(data) {
|
|
634
|
+
return typeof data === 'function' ? data() : data;
|
|
747
635
|
}
|
|
748
|
-
/* eslint-enable no-console */
|
|
749
636
|
//# sourceMappingURL=log.js.map
|