@vielzeug/rune 1.1.1
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 +95 -0
- package/dist/_dev.cjs +2 -0
- package/dist/_dev.cjs.map +1 -0
- package/dist/_dev.d.ts +2 -0
- package/dist/_dev.d.ts.map +1 -0
- package/dist/_dev.js +9 -0
- package/dist/_dev.js.map +1 -0
- package/dist/_prototype.cjs +2 -0
- package/dist/_prototype.cjs.map +1 -0
- package/dist/_prototype.d.ts +2 -0
- package/dist/_prototype.d.ts.map +1 -0
- package/dist/_prototype.js +13 -0
- package/dist/_prototype.js.map +1 -0
- package/dist/console.cjs +2 -0
- package/dist/console.cjs.map +1 -0
- package/dist/console.d.ts +65 -0
- package/dist/console.d.ts.map +1 -0
- package/dist/console.js +132 -0
- package/dist/console.js.map +1 -0
- package/dist/errors.cjs +2 -0
- package/dist/errors.cjs.map +1 -0
- package/dist/errors.d.ts +15 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +17 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/lazy.cjs +2 -0
- package/dist/lazy.cjs.map +1 -0
- package/dist/lazy.d.ts +28 -0
- package/dist/lazy.d.ts.map +1 -0
- package/dist/lazy.js +21 -0
- package/dist/lazy.js.map +1 -0
- package/dist/logger.cjs +2 -0
- package/dist/logger.cjs.map +1 -0
- package/dist/logger.d.ts +15 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +158 -0
- package/dist/logger.js.map +1 -0
- package/dist/rune.cjs +3 -0
- package/dist/rune.cjs.map +1 -0
- package/dist/rune.iife.js +3 -0
- package/dist/rune.iife.js.map +1 -0
- package/dist/rune.js +3 -0
- package/dist/rune.js.map +1 -0
- package/dist/transports.cjs +3 -0
- package/dist/transports.cjs.map +1 -0
- package/dist/transports.d.ts +88 -0
- package/dist/transports.d.ts.map +1 -0
- package/dist/transports.js +111 -0
- package/dist/transports.js.map +1 -0
- package/dist/types.cjs +2 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.ts +274 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +16 -0
- package/dist/types.js.map +1 -0
- package/package.json +43 -0
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
export type LogType = 'debug' | 'error' | 'fatal' | 'info' | 'warn';
|
|
2
|
+
export type LogLevel = LogType | 'off';
|
|
3
|
+
/** Numeric priority for each level. Lower = more verbose. Exported for transport authors. */
|
|
4
|
+
export declare const PRIORITY: Record<LogLevel, number>;
|
|
5
|
+
/** Returns true if `level` passes the `threshold`. Returns false when `level` is 'off'. Exported for transport/middleware authors. */
|
|
6
|
+
export declare function isLevelEnabled(threshold: LogLevel, level: LogLevel): boolean;
|
|
7
|
+
export type Bindings = Record<string, unknown>;
|
|
8
|
+
/**
|
|
9
|
+
* The structured record produced by every log call and dispatched to all transports.
|
|
10
|
+
* `data` is the merged result of pinned bindings and per-call context — transports
|
|
11
|
+
* receive a single flat object and do not need to merge anything themselves.
|
|
12
|
+
* Any `Error` instances — whether from a pinned binding (`bindings`/`withBindings()`) or
|
|
13
|
+
* per-call context — are automatically serialized to `{ message, name, stack }`.
|
|
14
|
+
* **Shallow only** — an `Error` nested inside a plain object (e.g. `{ meta: { err } }`) is left as-is;
|
|
15
|
+
* only top-level fields of `data` are checked.
|
|
16
|
+
*/
|
|
17
|
+
export type LogEntry = {
|
|
18
|
+
/**
|
|
19
|
+
* Merged structured data: pinned bindings overlaid with per-call context.
|
|
20
|
+
* Already shallow-copied and immutable — do not mutate.
|
|
21
|
+
*/
|
|
22
|
+
data: Readonly<Bindings>;
|
|
23
|
+
level: LogType;
|
|
24
|
+
message?: string;
|
|
25
|
+
namespace: string;
|
|
26
|
+
/** Exact moment of the log call — shared across all transports for the same entry. */
|
|
27
|
+
timestamp: Date;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* A transport receives a log entry and is responsible for its own delivery and formatting.
|
|
31
|
+
* If a transport throws, the logger catches it, reports it via a dev-only warning (wrapped in
|
|
32
|
+
* `RuneTransportError`), and continues dispatching the entry to remaining transports — a single
|
|
33
|
+
* misbehaving transport can never crash the caller of `log.info()`/etc. or block its siblings.
|
|
34
|
+
*/
|
|
35
|
+
export type Transport = (entry: LogEntry) => void;
|
|
36
|
+
/**
|
|
37
|
+
* Middleware function that transforms or filters log entries before dispatch. Return null to drop the entry.
|
|
38
|
+
* If middleware throws, the logger catches it, reports it via a dev-only warning, and drops the entry
|
|
39
|
+
* (no transports run for it) rather than crashing the caller.
|
|
40
|
+
*/
|
|
41
|
+
export type LogMiddleware = (entry: LogEntry) => LogEntry | null;
|
|
42
|
+
export type RemoteLogData = {
|
|
43
|
+
data?: Bindings;
|
|
44
|
+
env: 'development' | 'production';
|
|
45
|
+
level: LogType;
|
|
46
|
+
message?: string;
|
|
47
|
+
namespace?: string;
|
|
48
|
+
timestamp: string;
|
|
49
|
+
};
|
|
50
|
+
export type RemoteTransportOptions = {
|
|
51
|
+
/** Override the detected runtime environment. Default: auto-detected. */
|
|
52
|
+
env?: 'development' | 'production';
|
|
53
|
+
/** Remote delivery handler — receives the log type and structured payload. */
|
|
54
|
+
handler: (type: LogType, data: RemoteLogData) => void;
|
|
55
|
+
/** Minimum level to forward. Default: 'debug'. */
|
|
56
|
+
level?: LogLevel;
|
|
57
|
+
/**
|
|
58
|
+
* Called when the handler throws or rejects.
|
|
59
|
+
* The async error path is separate from any synchronous errors in the emit call stack.
|
|
60
|
+
* Default: a dev-only `console.warn` (gated by `__RUNE_PROD__`). In production builds,
|
|
61
|
+
* unhandled remote transport errors are silently swallowed — pass an explicit `onError`
|
|
62
|
+
* if you need delivery-failure observability in production.
|
|
63
|
+
*/
|
|
64
|
+
onError?: (error: unknown, data: RemoteLogData) => void;
|
|
65
|
+
};
|
|
66
|
+
export type JsonTransportOptions = {
|
|
67
|
+
/**
|
|
68
|
+
* Custom output field names. Useful for adapting to aggregator conventions
|
|
69
|
+
* (Datadog, ELK, Loki, etc.).
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* jsonTransport({ fields: { level: 'severity', time: '@timestamp', msg: 'message' } })
|
|
73
|
+
*/
|
|
74
|
+
fields?: {
|
|
75
|
+
level?: string;
|
|
76
|
+
msg?: string;
|
|
77
|
+
ns?: string;
|
|
78
|
+
time?: string;
|
|
79
|
+
};
|
|
80
|
+
/** Minimum level to output. Default: 'debug'. */
|
|
81
|
+
level?: LogLevel;
|
|
82
|
+
/** Custom output function. Default: process.stdout.write. */
|
|
83
|
+
output?: (line: string) => void;
|
|
84
|
+
/**
|
|
85
|
+
* Replace circular references with `'[Circular]'` instead of throwing a TypeError.
|
|
86
|
+
* Useful in environments where log payloads may contain complex object graphs.
|
|
87
|
+
* Default: false.
|
|
88
|
+
*/
|
|
89
|
+
safe?: boolean;
|
|
90
|
+
};
|
|
91
|
+
/** Handle returned by `batchTransport()`. Pass `handle.transport` to `createLogger({ transports })`. */
|
|
92
|
+
export type BatchHandle = {
|
|
93
|
+
/** Delegates to `dispose()`. Enables `using` declarations. */
|
|
94
|
+
[Symbol.dispose]: () => void;
|
|
95
|
+
/** Stop the interval timer and flush remaining entries. Call on shutdown. Idempotent. */
|
|
96
|
+
dispose: () => void;
|
|
97
|
+
/** `true` after `dispose()` has been called. */
|
|
98
|
+
readonly disposed: boolean;
|
|
99
|
+
/** Immediately flush buffered entries to the downstream handler without stopping the timer. */
|
|
100
|
+
flush: () => void;
|
|
101
|
+
/** The transport function to pass to `createLogger({ transports: [handle.transport] })`. */
|
|
102
|
+
transport: Transport;
|
|
103
|
+
};
|
|
104
|
+
export type BatchTransportOptions = {
|
|
105
|
+
/** Flush interval in milliseconds. Default: 5000. */
|
|
106
|
+
interval?: number;
|
|
107
|
+
/** Minimum level to buffer. Default: 'debug'. */
|
|
108
|
+
level?: LogLevel;
|
|
109
|
+
/**
|
|
110
|
+
* Hard limit on the in-memory buffer size. When the buffer exceeds this value,
|
|
111
|
+
* the oldest entries are dropped to prevent unbounded memory growth.
|
|
112
|
+
* Unlike `maxSize`, this does NOT trigger a flush — it silently drops.
|
|
113
|
+
* Default: unbounded.
|
|
114
|
+
*/
|
|
115
|
+
maxBuffer?: number;
|
|
116
|
+
/** Maximum buffer size before an early flush. Default: 50. */
|
|
117
|
+
maxSize?: number;
|
|
118
|
+
/**
|
|
119
|
+
* Callback to receive flushed batches. May return a Promise — async rejections
|
|
120
|
+
* are forwarded to `onFlushError` in addition to synchronous throws.
|
|
121
|
+
*/
|
|
122
|
+
onFlush: (entries: LogEntry[]) => void | Promise<void>;
|
|
123
|
+
/**
|
|
124
|
+
* Called when onFlush throws synchronously or rejects asynchronously.
|
|
125
|
+
* Allows retry/dead-letter logic. Default: silent.
|
|
126
|
+
*/
|
|
127
|
+
onFlushError?: (entries: LogEntry[], error: unknown) => void;
|
|
128
|
+
};
|
|
129
|
+
export type PipeOptions = {
|
|
130
|
+
/**
|
|
131
|
+
* Called when one of the piped transports throws.
|
|
132
|
+
* Receives the thrown error and the log entry that triggered it.
|
|
133
|
+
* Default: silent (errors are swallowed to protect remaining transports).
|
|
134
|
+
*/
|
|
135
|
+
onError?: (error: unknown, entry: LogEntry) => void;
|
|
136
|
+
};
|
|
137
|
+
export type SampleTransportOptions = {
|
|
138
|
+
/** Minimum level to sample. Default: 'debug'. */
|
|
139
|
+
level?: LogLevel;
|
|
140
|
+
/** Fraction of entries to forward (0–1). */
|
|
141
|
+
rate: number;
|
|
142
|
+
/** Downstream transport to receive sampled entries. */
|
|
143
|
+
transport: Transport;
|
|
144
|
+
};
|
|
145
|
+
export type RedactTransportOptions = {
|
|
146
|
+
/**
|
|
147
|
+
* Field names to replace at any depth in `data`.
|
|
148
|
+
* Matched by exact field name — dot-path notation (e.g. `'user.password'`) is NOT supported.
|
|
149
|
+
* A key like `'password'` will redact every field named `'password'` at any nesting level.
|
|
150
|
+
*/
|
|
151
|
+
keys: string[];
|
|
152
|
+
/**
|
|
153
|
+
* Maximum object nesting depth to traverse during redaction.
|
|
154
|
+
* Objects deeper than this limit are returned as-is (not redacted).
|
|
155
|
+
* A dev-only warning is emitted when the cap is hit.
|
|
156
|
+
* Default: 20.
|
|
157
|
+
* @security In production builds, the depth warning is suppressed — deeply-nested sensitive
|
|
158
|
+
* fields beyond `maxDepth` will pass through unredacted without any indication. Ensure that
|
|
159
|
+
* sensitive payloads are not nested beyond this limit, or lower `maxDepth` as needed.
|
|
160
|
+
*/
|
|
161
|
+
maxDepth?: number;
|
|
162
|
+
/** Replacement value for redacted fields. Default: '[REDACTED]'. */
|
|
163
|
+
replacement?: string;
|
|
164
|
+
/** Downstream transport to receive the redacted entry. */
|
|
165
|
+
transport: Transport;
|
|
166
|
+
};
|
|
167
|
+
export type RuneOptions = {
|
|
168
|
+
/** Initial pinned bindings for this logger instance. `Error` values are auto-serialized, same as per-call context. */
|
|
169
|
+
bindings?: Bindings;
|
|
170
|
+
/** Minimum log level for this logger instance. Default: 'debug'. */
|
|
171
|
+
logLevel?: LogLevel;
|
|
172
|
+
/** Middleware pipeline applied to every entry before dispatch to transports. */
|
|
173
|
+
middleware?: LogMiddleware[];
|
|
174
|
+
/**
|
|
175
|
+
* Namespace for this logger. When passed to `child()`, it is automatically
|
|
176
|
+
* dot-joined to the parent namespace (e.g. parent `'api'` + child `'auth'` → `'api.auth'`).
|
|
177
|
+
*/
|
|
178
|
+
namespace?: string;
|
|
179
|
+
/**
|
|
180
|
+
* Transport pipeline. Each transport receives every entry that passes the level threshold.
|
|
181
|
+
* Default: [consoleTransport()].
|
|
182
|
+
*/
|
|
183
|
+
transports?: Transport[];
|
|
184
|
+
};
|
|
185
|
+
/**
|
|
186
|
+
* Signature shared by all five log-level methods.
|
|
187
|
+
*
|
|
188
|
+
* - `log.info('message')` — string-only, most common.
|
|
189
|
+
* - `log.info({ ...fields }, 'message')` — structured context + optional message.
|
|
190
|
+
* `Error` values in `fields` are automatically serialized to `{ message, name, stack }`.
|
|
191
|
+
* Serialization is shallow only — an `Error` nested inside a nested object is left as-is.
|
|
192
|
+
* - `log.error(err, { ...fields }, 'message')` — Error first, then optional context + message.
|
|
193
|
+
* Shorthand for the pattern where an Error is the primary subject of the log call.
|
|
194
|
+
* The context object may be omitted entirely: `log.error(err, 'message')`.
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* log.error({ err: new Error('timeout'), requestId }, 'request failed')
|
|
198
|
+
* log.error(new Error('timeout'), { requestId }, 'request failed')
|
|
199
|
+
*/
|
|
200
|
+
export type LogMethod = {
|
|
201
|
+
(message: string): void;
|
|
202
|
+
(error: Error, message?: string): void;
|
|
203
|
+
(error: Error, context: Bindings, message?: string): void;
|
|
204
|
+
(context: Bindings, message?: string): void;
|
|
205
|
+
};
|
|
206
|
+
export type Logger = {
|
|
207
|
+
/** Delegates to `dispose()`. Enables `using` declarations. */
|
|
208
|
+
[Symbol.dispose]: () => void;
|
|
209
|
+
/** Snapshot of currently pinned bindings. */
|
|
210
|
+
readonly bindings: Readonly<Bindings>;
|
|
211
|
+
/** Create a child logger with config overrides. Inherits all config and bindings by default. */
|
|
212
|
+
child: (overrides?: RuneOptions) => Logger;
|
|
213
|
+
debug: LogMethod;
|
|
214
|
+
/** `AbortSignal` aborted when `dispose()` is called. Use to tie external lifetimes to this logger. */
|
|
215
|
+
readonly disposalSignal: AbortSignal;
|
|
216
|
+
/**
|
|
217
|
+
* Marks the logger as disposed — all subsequent log calls become no-ops.
|
|
218
|
+
* Aborts `disposalSignal`. Does NOT auto-discover or dispose batch transports;
|
|
219
|
+
* hold a direct reference to `batchTransport` and call its `dispose()` on shutdown.
|
|
220
|
+
* Idempotent — safe to call multiple times.
|
|
221
|
+
*/
|
|
222
|
+
dispose: () => void;
|
|
223
|
+
/** `true` after `dispose()` has been called. */
|
|
224
|
+
readonly disposed: boolean;
|
|
225
|
+
/** Returns true if entries at this level will pass the configured threshold. */
|
|
226
|
+
enabled: (type: LogLevel) => boolean;
|
|
227
|
+
error: LogMethod;
|
|
228
|
+
fatal: LogMethod;
|
|
229
|
+
/**
|
|
230
|
+
* Wrap a callback in a console group, closing it even on throw/reject.
|
|
231
|
+
* Pass a `level` to gate the group header on the configured log threshold
|
|
232
|
+
* (e.g. `level: 'debug'` suppresses the group when `logLevel` is above `'debug'`).
|
|
233
|
+
* Default: always renders (unless `logLevel` is `'off'`).
|
|
234
|
+
*/
|
|
235
|
+
group: <T>(label: string, fn: () => T, level?: LogType) => T;
|
|
236
|
+
/**
|
|
237
|
+
* Same as `group`, using `console.groupCollapsed`.
|
|
238
|
+
* Pass a `level` to gate the group on the configured log threshold.
|
|
239
|
+
*/
|
|
240
|
+
groupCollapsed: <T>(label: string, fn: () => T, level?: LogType) => T;
|
|
241
|
+
info: LogMethod;
|
|
242
|
+
/** Active log level for this logger instance. */
|
|
243
|
+
readonly logLevel: LogLevel;
|
|
244
|
+
/** Middleware pipeline applied before dispatch. */
|
|
245
|
+
readonly middleware: readonly LogMiddleware[];
|
|
246
|
+
/** Namespace string for this logger instance. */
|
|
247
|
+
readonly namespace: string;
|
|
248
|
+
/**
|
|
249
|
+
* Measure execution time of `fn` and emit a structured log entry.
|
|
250
|
+
* The entry message is `label`; `data` contains `{ duration_ms }` (rounded to 2 dp).
|
|
251
|
+
* When `fn` throws or rejects, `data` also includes `{ err }` with the serialized error.
|
|
252
|
+
* @param label - Human-readable description of the operation.
|
|
253
|
+
* @param fn - Synchronous or async function to time.
|
|
254
|
+
* @param level - Log level for the timing entry. Default: `'debug'`.
|
|
255
|
+
*/
|
|
256
|
+
time: <T>(label: string, fn: () => T, level?: LogType) => T;
|
|
257
|
+
/** Transport pipeline for this logger instance. */
|
|
258
|
+
readonly transports: readonly Transport[];
|
|
259
|
+
/**
|
|
260
|
+
* Add a middleware function to the pipeline. Returns a **new** logger — the original is unchanged.
|
|
261
|
+
* Discarding the return value is a common mistake: always assign the result.
|
|
262
|
+
* @example
|
|
263
|
+
* const log = baseLog.use(tracingMiddleware); // ✓ keep the result
|
|
264
|
+
*/
|
|
265
|
+
use: (middleware: LogMiddleware) => Logger;
|
|
266
|
+
warn: LogMethod;
|
|
267
|
+
/**
|
|
268
|
+
* Derive a child logger with additional pinned bindings.
|
|
269
|
+
* The returned logger is fully independent — disposing it does not affect the parent,
|
|
270
|
+
* and disposing the parent does not affect child loggers.
|
|
271
|
+
*/
|
|
272
|
+
withBindings: (bindings: Bindings) => Logger;
|
|
273
|
+
};
|
|
274
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,CAAC;AACpE,MAAM,MAAM,QAAQ,GAAG,OAAO,GAAG,KAAK,CAAC;AAEvC,6FAA6F;AAC7F,eAAO,MAAM,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,MAAM,CAO7C,CAAC;AAEF,sIAAsI;AACtI,wBAAgB,cAAc,CAAC,SAAS,EAAE,QAAQ,EAAE,KAAK,EAAE,QAAQ,GAAG,OAAO,CAI5E;AAID,MAAM,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAI/C;;;;;;;;GAQG;AACH,MAAM,MAAM,QAAQ,GAAG;IACrB;;;OAGG;IACH,IAAI,EAAE,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACzB,KAAK,EAAE,OAAO,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,sFAAsF;IACtF,SAAS,EAAE,IAAI,CAAC;CACjB,CAAC;AAIF;;;;;GAKG;AACH,MAAM,MAAM,SAAS,GAAG,CAAC,KAAK,EAAE,QAAQ,KAAK,IAAI,CAAC;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
+
info: 1,
|
|
7
|
+
off: 5,
|
|
8
|
+
warn: 2
|
|
9
|
+
};
|
|
10
|
+
function t(t, n) {
|
|
11
|
+
return n === "off" ? !1 : e[t] <= e[n];
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
export { e as PRIORITY, t as isLevelEnabled };
|
|
15
|
+
|
|
16
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","names":[],"sources":["../src/types.ts"],"sourcesContent":["/* ─── Log levels ─── */\n\nexport type LogType = 'debug' | 'error' | 'fatal' | 'info' | 'warn';\nexport type LogLevel = LogType | 'off';\n\n/** Numeric priority for each level. Lower = more verbose. Exported for transport authors. */\nexport const PRIORITY: Record<LogLevel, number> = {\n debug: 0,\n error: 3,\n fatal: 4,\n info: 1,\n off: 5,\n warn: 2,\n};\n\n/** Returns true if `level` passes the `threshold`. Returns false when `level` is 'off'. Exported for transport/middleware authors. */\nexport function isLevelEnabled(threshold: LogLevel, level: LogLevel): boolean {\n if (level === 'off') return false;\n\n return PRIORITY[threshold] <= PRIORITY[level];\n}\n\n/* ─── Bindings ─── */\n\nexport type Bindings = Record<string, unknown>;\n\n/* ─── Log entry ─── */\n\n/**\n * The structured record produced by every log call and dispatched to all transports.\n * `data` is the merged result of pinned bindings and per-call context — transports\n * receive a single flat object and do not need to merge anything themselves.\n * Any `Error` instances — whether from a pinned binding (`bindings`/`withBindings()`) or\n * per-call context — are automatically serialized to `{ message, name, stack }`.\n * **Shallow only** — an `Error` nested inside a plain object (e.g. `{ meta: { err } }`) is left as-is;\n * only top-level fields of `data` are checked.\n */\nexport type LogEntry = {\n /**\n * Merged structured data: pinned bindings overlaid with per-call context.\n * Already shallow-copied and immutable — do not mutate.\n */\n data: Readonly<Bindings>;\n level: LogType;\n message?: string;\n namespace: string;\n /** Exact moment of the log call — shared across all transports for the same entry. */\n timestamp: Date;\n};\n\n/* ─── Transport ─── */\n\n/**\n * A transport receives a log entry and is responsible for its own delivery and formatting.\n * If a transport throws, the logger catches it, reports it via a dev-only warning (wrapped in\n * `RuneTransportError`), and continues dispatching the entry to remaining transports — a single\n * misbehaving transport can never crash the caller of `log.info()`/etc. or block its siblings.\n */\nexport type Transport = (entry: LogEntry) => void;\n\n/**\n * Middleware function that transforms or filters log entries before dispatch. Return null to drop the entry.\n * If middleware throws, the logger catches it, reports it via a dev-only warning, and drops the entry\n * (no transports run for it) rather than crashing the caller.\n */\nexport type LogMiddleware = (entry: LogEntry) => LogEntry | null;\n\n/* ─── Transport option types ─── */\n\nexport type RemoteLogData = {\n data?: Bindings;\n env: 'development' | 'production';\n level: LogType;\n message?: string;\n namespace?: string;\n timestamp: string;\n};\n\nexport type RemoteTransportOptions = {\n /** Override the detected runtime environment. Default: auto-detected. */\n env?: 'development' | 'production';\n /** Remote delivery handler — receives the log type and structured payload. */\n handler: (type: LogType, data: RemoteLogData) => void;\n /** Minimum level to forward. Default: 'debug'. */\n level?: LogLevel;\n /**\n * Called when the handler throws or rejects.\n * The async error path is separate from any synchronous errors in the emit call stack.\n * Default: a dev-only `console.warn` (gated by `__RUNE_PROD__`). In production builds,\n * unhandled remote transport errors are silently swallowed — pass an explicit `onError`\n * if you need delivery-failure observability in production.\n */\n onError?: (error: unknown, data: RemoteLogData) => void;\n};\n\nexport type JsonTransportOptions = {\n /**\n * Custom output field names. Useful for adapting to aggregator conventions\n * (Datadog, ELK, Loki, etc.).\n *\n * @example\n * jsonTransport({ fields: { level: 'severity', time: '@timestamp', msg: 'message' } })\n */\n fields?: {\n level?: string;\n msg?: string;\n ns?: string;\n time?: string;\n };\n /** Minimum level to output. Default: 'debug'. */\n level?: LogLevel;\n /** Custom output function. Default: process.stdout.write. */\n output?: (line: string) => void;\n /**\n * Replace circular references with `'[Circular]'` instead of throwing a TypeError.\n * Useful in environments where log payloads may contain complex object graphs.\n * Default: false.\n */\n safe?: boolean;\n};\n\n/** Handle returned by `batchTransport()`. Pass `handle.transport` to `createLogger({ transports })`. */\nexport type BatchHandle = {\n /** Delegates to `dispose()`. Enables `using` declarations. */\n [Symbol.dispose]: () => void;\n /** Stop the interval timer and flush remaining entries. Call on shutdown. Idempotent. */\n dispose: () => void;\n /** `true` after `dispose()` has been called. */\n readonly disposed: boolean;\n /** Immediately flush buffered entries to the downstream handler without stopping the timer. */\n flush: () => void;\n /** The transport function to pass to `createLogger({ transports: [handle.transport] })`. */\n transport: Transport;\n};\n\nexport type BatchTransportOptions = {\n /** Flush interval in milliseconds. Default: 5000. */\n interval?: number;\n /** Minimum level to buffer. Default: 'debug'. */\n level?: LogLevel;\n /**\n * Hard limit on the in-memory buffer size. When the buffer exceeds this value,\n * the oldest entries are dropped to prevent unbounded memory growth.\n * Unlike `maxSize`, this does NOT trigger a flush — it silently drops.\n * Default: unbounded.\n */\n maxBuffer?: number;\n /** Maximum buffer size before an early flush. Default: 50. */\n maxSize?: number;\n /**\n * Callback to receive flushed batches. May return a Promise — async rejections\n * are forwarded to `onFlushError` in addition to synchronous throws.\n */\n onFlush: (entries: LogEntry[]) => void | Promise<void>;\n /**\n * Called when onFlush throws synchronously or rejects asynchronously.\n * Allows retry/dead-letter logic. Default: silent.\n */\n onFlushError?: (entries: LogEntry[], error: unknown) => void;\n};\n\nexport type PipeOptions = {\n /**\n * Called when one of the piped transports throws.\n * Receives the thrown error and the log entry that triggered it.\n * Default: silent (errors are swallowed to protect remaining transports).\n */\n onError?: (error: unknown, entry: LogEntry) => void;\n};\n\nexport type SampleTransportOptions = {\n /** Minimum level to sample. Default: 'debug'. */\n level?: LogLevel;\n /** Fraction of entries to forward (0–1). */\n rate: number;\n /** Downstream transport to receive sampled entries. */\n transport: Transport;\n};\n\nexport type RedactTransportOptions = {\n /**\n * Field names to replace at any depth in `data`.\n * Matched by exact field name — dot-path notation (e.g. `'user.password'`) is NOT supported.\n * A key like `'password'` will redact every field named `'password'` at any nesting level.\n */\n keys: string[];\n /**\n * Maximum object nesting depth to traverse during redaction.\n * Objects deeper than this limit are returned as-is (not redacted).\n * A dev-only warning is emitted when the cap is hit.\n * Default: 20.\n * @security In production builds, the depth warning is suppressed — deeply-nested sensitive\n * fields beyond `maxDepth` will pass through unredacted without any indication. Ensure that\n * sensitive payloads are not nested beyond this limit, or lower `maxDepth` as needed.\n */\n maxDepth?: number;\n /** Replacement value for redacted fields. Default: '[REDACTED]'. */\n replacement?: string;\n /** Downstream transport to receive the redacted entry. */\n transport: Transport;\n};\n\n/* ─── Logger options ─── */\n\nexport type RuneOptions = {\n /** Initial pinned bindings for this logger instance. `Error` values are auto-serialized, same as per-call context. */\n bindings?: Bindings;\n /** Minimum log level for this logger instance. Default: 'debug'. */\n logLevel?: LogLevel;\n /** Middleware pipeline applied to every entry before dispatch to transports. */\n middleware?: LogMiddleware[];\n /**\n * Namespace for this logger. When passed to `child()`, it is automatically\n * dot-joined to the parent namespace (e.g. parent `'api'` + child `'auth'` → `'api.auth'`).\n */\n namespace?: string;\n /**\n * Transport pipeline. Each transport receives every entry that passes the level threshold.\n * Default: [consoleTransport()].\n */\n transports?: Transport[];\n};\n\n/* ─── Log method ─── */\n\n/**\n * Signature shared by all five log-level methods.\n *\n * - `log.info('message')` — string-only, most common.\n * - `log.info({ ...fields }, 'message')` — structured context + optional message.\n * `Error` values in `fields` are automatically serialized to `{ message, name, stack }`.\n * Serialization is shallow only — an `Error` nested inside a nested object is left as-is.\n * - `log.error(err, { ...fields }, 'message')` — Error first, then optional context + message.\n * Shorthand for the pattern where an Error is the primary subject of the log call.\n * The context object may be omitted entirely: `log.error(err, 'message')`.\n *\n * @example\n * log.error({ err: new Error('timeout'), requestId }, 'request failed')\n * log.error(new Error('timeout'), { requestId }, 'request failed')\n */\nexport type LogMethod = {\n (message: string): void;\n (error: Error, message?: string): void;\n (error: Error, context: Bindings, message?: string): void;\n (context: Bindings, message?: string): void;\n};\n\n/* ─── Logger interface ─── */\n\nexport type Logger = {\n /** Delegates to `dispose()`. Enables `using` declarations. */\n [Symbol.dispose]: () => void;\n /** Snapshot of currently pinned bindings. */\n readonly bindings: Readonly<Bindings>;\n /** Create a child logger with config overrides. Inherits all config and bindings by default. */\n child: (overrides?: RuneOptions) => Logger;\n debug: LogMethod;\n /** `AbortSignal` aborted when `dispose()` is called. Use to tie external lifetimes to this logger. */\n readonly disposalSignal: AbortSignal;\n /**\n * Marks the logger as disposed — all subsequent log calls become no-ops.\n * Aborts `disposalSignal`. Does NOT auto-discover or dispose batch transports;\n * hold a direct reference to `batchTransport` and call its `dispose()` on shutdown.\n * Idempotent — safe to call multiple times.\n */\n dispose: () => void;\n /** `true` after `dispose()` has been called. */\n readonly disposed: boolean;\n /** Returns true if entries at this level will pass the configured threshold. */\n enabled: (type: LogLevel) => boolean;\n error: LogMethod;\n fatal: LogMethod;\n /**\n * Wrap a callback in a console group, closing it even on throw/reject.\n * Pass a `level` to gate the group header on the configured log threshold\n * (e.g. `level: 'debug'` suppresses the group when `logLevel` is above `'debug'`).\n * Default: always renders (unless `logLevel` is `'off'`).\n */\n group: <T>(label: string, fn: () => T, level?: LogType) => T;\n /**\n * Same as `group`, using `console.groupCollapsed`.\n * Pass a `level` to gate the group on the configured log threshold.\n */\n groupCollapsed: <T>(label: string, fn: () => T, level?: LogType) => T;\n info: LogMethod;\n /** Active log level for this logger instance. */\n readonly logLevel: LogLevel;\n /** Middleware pipeline applied before dispatch. */\n readonly middleware: readonly LogMiddleware[];\n /** Namespace string for this logger instance. */\n readonly namespace: string;\n /**\n * Measure execution time of `fn` and emit a structured log entry.\n * The entry message is `label`; `data` contains `{ duration_ms }` (rounded to 2 dp).\n * When `fn` throws or rejects, `data` also includes `{ err }` with the serialized error.\n * @param label - Human-readable description of the operation.\n * @param fn - Synchronous or async function to time.\n * @param level - Log level for the timing entry. Default: `'debug'`.\n */\n time: <T>(label: string, fn: () => T, level?: LogType) => T;\n /** Transport pipeline for this logger instance. */\n readonly transports: readonly Transport[];\n /**\n * Add a middleware function to the pipeline. Returns a **new** logger — the original is unchanged.\n * Discarding the return value is a common mistake: always assign the result.\n * @example\n * const log = baseLog.use(tracingMiddleware); // ✓ keep the result\n */\n use: (middleware: LogMiddleware) => Logger;\n warn: LogMethod;\n /**\n * Derive a child logger with additional pinned bindings.\n * The returned logger is fully independent — disposing it does not affect the parent,\n * and disposing the parent does not affect child loggers.\n */\n withBindings: (bindings: Bindings) => Logger;\n};\n"],"mappings":";AAMA,IAAa,IAAqC;CAChD,OAAO;CACP,OAAO;CACP,OAAO;CACP,MAAM;CACN,KAAK;CACL,MAAM;AACR;AAGA,SAAgB,EAAe,GAAqB,GAA0B;CAG5E,OAFI,MAAU,QAAc,KAErB,EAAS,MAAc,EAAS;AACzC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vielzeug/rune",
|
|
3
|
+
"version": "1.1.1",
|
|
4
|
+
"description": "Structured logging — scoped loggers, pluggable transports, log levels, and remote log draining",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"main": "./dist/index.cjs",
|
|
10
|
+
"module": "./dist/index.js",
|
|
11
|
+
"types": "dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"source": "./src/index.ts",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js",
|
|
17
|
+
"require": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "vite build && pnpm run build:bundle && pnpm run build:types",
|
|
22
|
+
"build:types": "tsc -p tsconfig.declarations.json",
|
|
23
|
+
"fix": "eslint --fix src",
|
|
24
|
+
"lint": "eslint src",
|
|
25
|
+
"prepublishOnly": "pnpm run build",
|
|
26
|
+
"preview": "vite preview",
|
|
27
|
+
"test": "vitest",
|
|
28
|
+
"build:bundle": "vite build --config vite.bundle.config.ts"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public",
|
|
32
|
+
"registry": "https://registry.npmjs.org/"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=18"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/node": "^26.1.0",
|
|
39
|
+
"typescript": "~6.0.3",
|
|
40
|
+
"vite": "^8.1.3",
|
|
41
|
+
"vitest": "^4.1.9"
|
|
42
|
+
}
|
|
43
|
+
}
|