@vielzeug/rune 1.1.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -38
- package/dist/_dev.cjs +1 -1
- package/dist/_dev.cjs.map +1 -1
- package/dist/_dev.js +2 -5
- package/dist/_dev.js.map +1 -1
- package/dist/console.cjs +1 -1
- package/dist/console.cjs.map +1 -1
- package/dist/console.js +2 -2
- package/dist/console.js.map +1 -1
- package/dist/errors.cjs +1 -1
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.ts +0 -9
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +1 -5
- package/dist/errors.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -7
- package/dist/logger.cjs +1 -1
- package/dist/logger.cjs.map +1 -1
- package/dist/logger.d.ts.map +1 -1
- package/dist/logger.js +76 -77
- package/dist/logger.js.map +1 -1
- package/dist/rune.cjs +2 -2
- package/dist/rune.cjs.map +1 -1
- package/dist/rune.iife.js +2 -2
- package/dist/rune.iife.js.map +1 -1
- package/dist/rune.js +2 -2
- package/dist/rune.js.map +1 -1
- package/dist/transports.cjs +1 -1
- package/dist/transports.cjs.map +1 -1
- package/dist/transports.d.ts +4 -4
- package/dist/transports.d.ts.map +1 -1
- package/dist/transports.js +60 -28
- package/dist/transports.js.map +1 -1
- package/dist/types.cjs +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.ts +19 -22
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +3 -4
package/dist/transports.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transports.js","names":[],"sources":["../src/transports.ts"],"sourcesContent":["import type {\n BatchHandle,\n BatchTransportOptions,\n Bindings,\n JsonTransportOptions,\n LogEntry,\n PipeOptions,\n RedactTransportOptions,\n RemoteLogData,\n RemoteTransportOptions,\n SampleTransportOptions,\n Transport,\n} from './types';\n\nimport { warn } from './_dev';\nimport { isUnsafeObjectKey } from './_prototype';\nimport { isLevelEnabled } from './types';\n\nexport type { RemoteLogData };\n\n/* ─── Environment detection ─── */\n\nfunction detectEnv(): 'development' | 'production' {\n if (typeof window === 'undefined') {\n return (globalThis as Record<string, unknown> & { process?: { env?: { NODE_ENV?: string } } }).process?.env\n ?.NODE_ENV === 'production'\n ? 'production'\n : 'development';\n }\n\n return (import.meta as ImportMeta & { env?: { PROD?: boolean } }).env?.PROD ? 'production' : 'development';\n}\n\n/* ─── remoteTransport ─── */\n\n/**\n * Forwards log entries asynchronously to a remote handler.\n * The handler is fire-and-forget. Use onError to observe delivery failures.\n * Console and remote thresholds are fully independent.\n *\n * **Security note:** serialized `Error` objects include the full `stack` trace,\n * which may expose internal file paths. Use `redactTransport` or a middleware\n * to strip `err.stack` before forwarding in production if this is a concern.\n *\n * @example\n * remoteTransport({\n * handler: async (type, data) => {\n * await fetch('/api/logs', { body: JSON.stringify(data), method: 'POST' });\n * },\n * level: 'error',\n * })\n */\nexport function remoteTransport(options: RemoteTransportOptions): Transport {\n const { handler } = options;\n const level = options.level ?? 'debug';\n const env = options.env ?? detectEnv();\n const onError = options.onError ?? ((err: unknown) => warn(`remote transport error: ${String(err)}`));\n\n return (entry: LogEntry): void => {\n if (!isLevelEnabled(level, entry.level)) return;\n\n const hasData = Object.keys(entry.data).length > 0;\n const payload: RemoteLogData = {\n data: hasData ? entry.data : undefined,\n env,\n level: entry.level,\n message: entry.message,\n namespace: entry.namespace || undefined,\n timestamp: entry.timestamp.toISOString(),\n };\n\n Promise.resolve()\n .then(() => handler(entry.level, payload))\n .catch((err: unknown) => onError(err, payload));\n };\n}\n\n/* ─── jsonTransport ─── */\n\nfunction makeCircularReplacer(): (_key: string, value: unknown) => unknown {\n const seen = new WeakSet();\n\n return (_key: string, value: unknown): unknown => {\n if (typeof value === 'object' && value !== null) {\n if (seen.has(value)) return '[Circular]';\n\n seen.add(value);\n }\n\n return value;\n };\n}\n\n/**\n * Writes newline-delimited JSON (NDJSON) to stdout or a custom output function.\n * Useful for structured log aggregation pipelines in Node.js (ELK, Datadog, etc.).\n *\n * @example\n * jsonTransport({ level: 'info' })\n * jsonTransport({ safe: true }) // handles circular references gracefully\n * jsonTransport({ output: (line) => fs.appendFileSync('app.log', line + '\\n') })\n */\nexport function jsonTransport(options: JsonTransportOptions = {}): Transport {\n const level = options.level ?? 'debug';\n const f = options.fields ?? {};\n const fLevel = f.level ?? 'level';\n const fTime = f.time ?? 'time';\n const fNs = f.ns ?? 'ns';\n const fMsg = f.msg ?? 'msg';\n const safe = options.safe ?? false;\n const output =\n options.output ??\n ((line: string) => {\n (\n globalThis as Record<string, unknown> & { process?: { stdout?: { write: (s: string) => void } } }\n ).process?.stdout?.write(line + '\\n');\n });\n\n return (entry: LogEntry): void => {\n if (!isLevelEnabled(level, entry.level)) return;\n\n const record: Record<string, unknown> = {\n ...entry.data,\n [fLevel]: entry.level,\n [fTime]: entry.timestamp.toISOString(),\n ...(entry.namespace && { [fNs]: entry.namespace }),\n ...(entry.message !== undefined && { [fMsg]: entry.message }),\n };\n\n output(JSON.stringify(record, safe ? makeCircularReplacer() : undefined));\n };\n}\n\n/* ─── batchTransport ─── */\n\n/**\n * Buffers log entries and flushes them in batches, reducing I/O overhead.\n * Flushes when the buffer reaches maxSize or after the interval elapses.\n *\n * Returns a `BatchHandle` with `.transport`, `.flush()`, and `.dispose()` methods:\n * - `.transport` — pass to `createLogger({ transports: [handle.transport] })`\n * - `.flush()` — immediately send buffered entries without stopping the timer\n * - `.dispose()` — stop the interval and flush remaining entries (call on shutdown)\n *\n * Use `onFlushError` to observe or retry failed flushes (e.g. dead-letter queue).\n *\n * @example\n * const batch = batchTransport({\n * onFlush: (entries) => sendToCollector(entries),\n * onFlushError: (entries, err) => deadLetter.push(entries),\n * interval: 10_000,\n * maxSize: 100,\n * });\n * createLogger({ transports: [batch.transport] });\n * batch.dispose(); // call on app shutdown\n */\nexport function batchTransport(options: BatchTransportOptions): BatchHandle {\n const level = options.level ?? 'debug';\n const maxSize = options.maxSize ?? 50;\n const maxBuffer = options.maxBuffer;\n const interval = options.interval ?? 5000;\n\n let buffer: LogEntry[] = [];\n let timer: ReturnType<typeof setInterval> | undefined;\n let batchDisposed = false;\n\n function flush(): void {\n if (buffer.length === 0) return;\n\n const entries = buffer;\n\n buffer = [];\n\n Promise.resolve()\n .then(() => options.onFlush(entries))\n .catch((err: unknown) => options.onFlushError?.(entries, err));\n }\n\n const transportFn: Transport = (entry: LogEntry): void => {\n if (batchDisposed) return;\n\n if (!isLevelEnabled(level, entry.level)) return;\n\n if (!timer) timer = setInterval(flush, interval);\n\n buffer.push(entry);\n\n if (maxBuffer !== undefined && buffer.length > maxBuffer) {\n buffer = buffer.slice(buffer.length - maxBuffer);\n }\n\n if (buffer.length >= maxSize) flush();\n };\n\n const handle: BatchHandle = {\n dispose(): void {\n if (batchDisposed) return;\n\n batchDisposed = true;\n\n if (timer) {\n clearInterval(timer);\n timer = undefined;\n }\n\n flush();\n },\n get disposed(): boolean {\n return batchDisposed;\n },\n flush,\n [Symbol.dispose](): void {\n handle.dispose();\n },\n transport: transportFn,\n };\n\n return handle;\n}\n\n/* ─── sampleTransport ─── */\n\n/**\n * Probabilistically forwards entries to a downstream transport.\n * Useful for reducing volume of high-frequency debug logs in production.\n *\n * @example\n * sampleTransport({ rate: 0.1, transport: remoteTransport({ handler }) })\n */\nexport function sampleTransport(options: SampleTransportOptions): Transport {\n const { rate, transport } = options;\n const level = options.level ?? 'debug';\n\n return (entry: LogEntry): void => {\n if (!isLevelEnabled(level, entry.level)) return;\n\n if (Math.random() < rate) transport(entry);\n };\n}\n\n/* ─── redactTransport ─── */\n\n/**\n * Strips sensitive fields from `data` before forwarding to a downstream transport.\n * Redaction is applied recursively at any depth, including inside arrays.\n *\n * @example\n * redactTransport({\n * keys: ['password', 'token', 'ssn'],\n * replacement: '[REDACTED]',\n * transport: remoteTransport({ handler }),\n * })\n */\nexport function redactTransport(options: RedactTransportOptions): Transport {\n const { keys, maxDepth = 20, replacement = '[REDACTED]', transport } = options;\n\n for (const key of keys) {\n if (key.includes('.')) {\n warn(\n `redactTransport: key \"${key}\" contains a dot. Dot-path notation is not supported — use the plain field name (e.g. 'password') to redact at any depth.`,\n );\n }\n }\n\n const keySet = new Set(keys);\n\n function redactValue(v: unknown, depth = 0): unknown {\n if (depth > maxDepth) {\n warn(\n `redactTransport: object nesting depth exceeded ${maxDepth} — redaction truncated at this level. Sensitive fields below depth ${maxDepth} may not be redacted.`,\n );\n\n return v;\n }\n\n if (Array.isArray(v)) return v.map((item) => redactValue(item, depth + 1));\n\n if (typeof v === 'object' && v !== null) return redactObject(v as Bindings, depth + 1);\n\n return v;\n }\n\n function redactObject(obj: Bindings, depth = 0): Bindings {\n const result: Bindings = {};\n\n for (const [k, v] of Object.entries(obj)) {\n // Guard against a `__proto__`/`constructor`/`prototype` field name hijacking result's own\n // prototype via the bracket-assignment accessor — see _prototype.ts.\n if (isUnsafeObjectKey(k)) continue;\n\n result[k] = keySet.has(k) ? replacement : redactValue(v, depth);\n }\n\n return result;\n }\n\n return (entry: LogEntry): void => {\n transport({ ...entry, data: redactObject(entry.data as Bindings) });\n };\n}\n\n/* ─── pipe — fault-tolerant fan-out ─── */\n\n/**\n * Fan-out: dispatches each entry to all provided transports independently.\n * A throw in one transport does not prevent others from receiving the entry.\n * Pass `onError` to observe individual transport failures; without it, errors are silently swallowed.\n *\n * @example\n * createLogger({\n * transports: [pipe(consoleTransport(), remoteTransport({ handler }))],\n * })\n *\n * // With error observer:\n * pipe({ onError: (err) => console.error('[pipe]', err) }, consoleTransport(), remoteTransport({ handler }))\n */\nexport function pipe(...transports: Transport[]): Transport;\nexport function pipe(options: PipeOptions, ...transports: Transport[]): Transport;\nexport function pipe(optionsOrTransport: PipeOptions | Transport, ...rest: Transport[]): Transport {\n let opts: PipeOptions;\n let transports: Transport[];\n\n if (typeof optionsOrTransport === 'function') {\n opts = {};\n transports = [optionsOrTransport, ...rest];\n } else {\n opts = optionsOrTransport;\n transports = rest;\n }\n\n return (entry: LogEntry): void => {\n for (const t of transports) {\n try {\n t(entry);\n } catch (err) {\n opts.onError?.(err, entry);\n }\n }\n };\n}\n"],"mappings":";;;;AAsBA,SAAS,IAA0C;CAQjD,OAPI,OAAO,SAAW,MACZ,WAAuF,SAAS,KACpG,aAAa,eACb,eACA,gBAGwE;AAChF;AAqBA,SAAgB,EAAgB,GAA4C;CAC1E,IAAM,EAAE,eAAY,GACd,IAAQ,EAAQ,SAAS,SACzB,IAAM,EAAQ,OAAO,EAAU,GAC/B,IAAU,EAAQ,aAAa,MAAiB,EAAK,2BAA2B,OAAO,CAAG,GAAG;CAEnG,QAAQ,MAA0B;EAChC,IAAI,CAAC,EAAe,GAAO,EAAM,KAAK,GAAG;EAGzC,IAAM,IAAyB;GAC7B,MAFc,OAAO,KAAK,EAAM,IAAI,CAAC,CAAC,SAAS,IAE/B,EAAM,OAAO,KAAA;GAC7B;GACA,OAAO,EAAM;GACb,SAAS,EAAM;GACf,WAAW,EAAM,aAAa,KAAA;GAC9B,WAAW,EAAM,UAAU,YAAY;EACzC;EAEA,QAAQ,QAAQ,CAAC,CACd,WAAW,EAAQ,EAAM,OAAO,CAAO,CAAC,CAAC,CACzC,OAAO,MAAiB,EAAQ,GAAK,CAAO,CAAC;CAClD;AACF;AAIA,SAAS,IAAkE;CACzE,IAAM,oBAAO,IAAI,QAAQ;CAEzB,QAAQ,GAAc,MAA4B;EAChD,IAAI,OAAO,KAAU,YAAY,GAAgB;GAC/C,IAAI,EAAK,IAAI,CAAK,GAAG,OAAO;GAE5B,EAAK,IAAI,CAAK;EAChB;EAEA,OAAO;CACT;AACF;AAWA,SAAgB,EAAc,IAAgC,CAAC,GAAc;CAC3E,IAAM,IAAQ,EAAQ,SAAS,SACzB,IAAI,EAAQ,UAAU,CAAC,GACvB,IAAS,EAAE,SA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AuB,cAChC,IAAO,CAAC,GACR,IAAa,CAAC,GAAoB,GAAG,CAAI,MAEzC,IAAO,GACP,IAAa,KAGP,MAA0B;EAChC,KAAK,IAAM,KAAK,GACd,IAAI;GACF,EAAE,CAAK;EACT,SAAS,GAAK;GACZ,EAAK,UAAU,GAAK,CAAK;EAC3B;CAEJ;AACF"}
|
|
1
|
+
{"version":3,"file":"transports.js","names":[],"sources":["../src/transports.ts"],"sourcesContent":["import type {\n BatchHandle,\n BatchTransportOptions,\n Bindings,\n JsonTransportOptions,\n LogEntry,\n PipeOptions,\n RedactTransportOptions,\n RemoteLogData,\n RemoteTransportOptions,\n SampleTransportOptions,\n Transport,\n} from './types';\n\nimport { warn } from './_dev';\nimport { isUnsafeObjectKey } from './_prototype';\nimport { isLevelEnabled } from './types';\n\nexport type { RemoteLogData };\n\n/* ─── Environment detection ─── */\n\nfunction detectEnv(): 'development' | 'production' {\n if (typeof window === 'undefined') {\n return (globalThis as Record<string, unknown> & { process?: { env?: { NODE_ENV?: string } } }).process?.env\n ?.NODE_ENV === 'production'\n ? 'production'\n : 'development';\n }\n\n return (import.meta as ImportMeta & { env?: { PROD?: boolean } }).env?.PROD ? 'production' : 'development';\n}\n\n/* ─── remoteTransport ─── */\n\n/**\n * Forwards log entries asynchronously to a remote handler.\n * The handler is fire-and-forget. Use onError to observe delivery failures.\n * Console and remote thresholds are fully independent.\n *\n * **Security note:** serialized `Error` objects include the full `stack` trace,\n * which may expose internal file paths. Use `redactTransport` or a middleware\n * to strip `err.stack` before forwarding in production if this is a concern.\n *\n * @example\n * remoteTransport({\n * handler: async (type, data) => {\n * await fetch('/api/logs', { body: JSON.stringify(data), method: 'POST' });\n * },\n * level: 'error',\n * })\n */\nexport function remoteTransport(options: RemoteTransportOptions): Transport {\n const { handler } = options;\n const level = options.level ?? 'debug';\n const env = options.env ?? detectEnv();\n const onError = options.onError ?? ((err: unknown) => warn(`remote transport error: ${String(err)}`));\n\n return (entry: LogEntry): void => {\n if (!isLevelEnabled(level, entry.level)) return;\n\n const hasData = Object.keys(entry.data).length > 0;\n const payload: RemoteLogData = {\n data: hasData ? entry.data : undefined,\n env,\n level: entry.level,\n message: entry.message,\n namespace: entry.namespace || undefined,\n timestamp: entry.timestamp.toISOString(),\n };\n\n Promise.resolve()\n .then(() => handler(entry.level, payload))\n .catch((err: unknown) => onError(err, payload));\n };\n}\n\n/* ─── jsonTransport ─── */\n\nfunction makeCircularReplacer(): (_key: string, value: unknown) => unknown {\n const seen = new WeakSet();\n\n return (_key: string, value: unknown): unknown => {\n if (typeof value === 'object' && value !== null) {\n if (seen.has(value)) return '[Circular]';\n\n seen.add(value);\n }\n\n return value;\n };\n}\n\n/**\n * Writes newline-delimited JSON (NDJSON) to stdout or a custom output function.\n * Useful for structured log aggregation pipelines in Node.js (ELK, Datadog, etc.).\n *\n * @example\n * jsonTransport({ level: 'info' })\n * jsonTransport({ safe: true }) // handles circular references gracefully\n * jsonTransport({ output: (line) => fs.appendFileSync('app.log', line + '\\n') })\n */\nexport function jsonTransport(options: JsonTransportOptions = {}): Transport {\n const level = options.level ?? 'debug';\n const f = options.fields ?? {};\n const fLevel = f.level ?? 'level';\n const fTime = f.time ?? 'time';\n const fNs = f.ns ?? 'ns';\n const fMsg = f.msg ?? 'msg';\n const safe = options.safe ?? false;\n const output =\n options.output ??\n ((line: string) => {\n (\n globalThis as Record<string, unknown> & { process?: { stdout?: { write: (s: string) => void } } }\n ).process?.stdout?.write(line + '\\n');\n });\n\n return (entry: LogEntry): void => {\n if (!isLevelEnabled(level, entry.level)) return;\n\n const record: Record<string, unknown> = {\n ...entry.data,\n [fLevel]: entry.level,\n [fTime]: entry.timestamp.toISOString(),\n ...(entry.namespace && { [fNs]: entry.namespace }),\n ...(entry.message !== undefined && { [fMsg]: entry.message }),\n };\n\n output(JSON.stringify(record, safe ? makeCircularReplacer() : undefined));\n };\n}\n\n/* ─── Transport option validation ─── */\n\nfunction assertFiniteNumber(value: number, name: string): void {\n if (!Number.isFinite(value)) throw new RangeError(`${name} must be a finite number`);\n}\n\nfunction assertNonNegativeInteger(value: number, name: string): void {\n assertFiniteNumber(value, name);\n\n if (!Number.isInteger(value) || value < 0) throw new RangeError(`${name} must be a non-negative integer`);\n}\n\nfunction assertPositiveInteger(value: number, name: string): void {\n assertFiniteNumber(value, name);\n\n if (!Number.isInteger(value) || value <= 0) throw new RangeError(`${name} must be a positive integer`);\n}\n\n/* ─── batchTransport ─── */\n\n/**\n * Buffers log entries and flushes them in batches, reducing I/O overhead.\n * Flushes when the buffer reaches maxSize or after the interval elapses.\n *\n * Returns a `BatchHandle` with `.transport`, `.flush()`, and `.dispose()` methods:\n * - `.transport` — pass to `createLogger({ transports: [handle.transport] })`\n * - `.flush()` — immediately send buffered entries and wait for delivery without stopping the timer\n * - `.dispose()` — stop the interval, flush remaining entries, and wait for delivery\n *\n * Use `onFlushError` to observe failed flushes (e.g. dead-letter queue).\n *\n * @example\n * const batch = batchTransport({\n * onFlush: (entries) => sendToCollector(entries),\n * onFlushError: (entries, err) => deadLetter.push(entries),\n * interval: 10_000,\n * maxSize: 100,\n * });\n * createLogger({ transports: [batch.transport] });\n * await batch.dispose(); // call during graceful shutdown\n */\nexport function batchTransport(options: BatchTransportOptions): BatchHandle {\n const level = options.level ?? 'debug';\n const maxSize = options.maxSize ?? 50;\n const maxBuffer = options.maxBuffer;\n const interval = options.interval ?? 5000;\n\n assertFiniteNumber(interval, 'batchTransport interval');\n\n if (interval <= 0) throw new RangeError('batchTransport interval must be greater than zero');\n\n assertPositiveInteger(maxSize, 'batchTransport maxSize');\n\n if (maxBuffer !== undefined) assertNonNegativeInteger(maxBuffer, 'batchTransport maxBuffer');\n\n let buffer: LogEntry[] = [];\n let timer: ReturnType<typeof setInterval> | undefined;\n let automaticFailure: { error: unknown } | undefined;\n let batchDisposed = false;\n let deliveryTail: Promise<void> = Promise.resolve();\n let disposePromise: Promise<void> | undefined;\n\n const deliver = async (entries: LogEntry[]): Promise<void> => {\n try {\n await options.onFlush(entries);\n } catch (err) {\n try {\n options.onFlushError?.(entries, err);\n } catch (observerErr) {\n warn(`batch transport error observer threw: ${String(observerErr)}`);\n }\n\n throw err;\n }\n };\n\n const enqueue = (entries: LogEntry[]): Promise<void> => {\n const delivery = deliveryTail.then(() => deliver(entries));\n\n deliveryTail = delivery.catch(() => undefined);\n\n return delivery;\n };\n\n const flush = (): Promise<void> => {\n if (buffer.length === 0) return deliveryTail;\n\n const entries = buffer;\n\n buffer = [];\n\n return enqueue(entries);\n };\n\n const flushAutomatically = (): void => {\n void flush().catch((error: unknown) => {\n automaticFailure ??= { error };\n });\n };\n\n const transportFn: Transport = (entry: LogEntry): void => {\n if (batchDisposed) return;\n\n if (!isLevelEnabled(level, entry.level)) return;\n\n if (!timer) timer = setInterval(flushAutomatically, interval);\n\n buffer.push(entry);\n\n if (maxBuffer !== undefined && buffer.length > maxBuffer) {\n buffer = buffer.slice(buffer.length - maxBuffer);\n }\n\n if (buffer.length >= maxSize) flushAutomatically();\n };\n\n const handle: BatchHandle = {\n dispose(): Promise<void> {\n if (disposePromise) return disposePromise;\n\n batchDisposed = true;\n\n if (timer) {\n clearInterval(timer);\n timer = undefined;\n }\n\n disposePromise = flush().then(() => {\n if (automaticFailure) throw automaticFailure.error;\n });\n\n return disposePromise;\n },\n get disposed(): boolean {\n return batchDisposed;\n },\n flush,\n [Symbol.asyncDispose](): Promise<void> {\n return handle.dispose();\n },\n transport: transportFn,\n };\n\n return handle;\n}\n\n/* ─── sampleTransport ─── */\n\n/**\n * Probabilistically forwards entries to a downstream transport.\n * Useful for reducing volume of high-frequency debug logs in production.\n *\n * @example\n * sampleTransport({ rate: 0.1, transport: remoteTransport({ handler }) })\n */\nexport function sampleTransport(options: SampleTransportOptions): Transport {\n const { rate, transport } = options;\n const level = options.level ?? 'debug';\n\n assertFiniteNumber(rate, 'sampleTransport rate');\n\n if (rate < 0 || rate > 1) throw new RangeError('sampleTransport rate must be between zero and one');\n\n return (entry: LogEntry): void => {\n if (!isLevelEnabled(level, entry.level)) return;\n\n if (Math.random() < rate) transport(entry);\n };\n}\n\n/* ─── redactTransport ─── */\n\n/**\n * Strips sensitive fields from `data` before forwarding to a downstream transport.\n * Redaction is applied recursively at any depth, including inside arrays.\n *\n * @example\n * redactTransport({\n * keys: ['password', 'token', 'ssn'],\n * replacement: '[REDACTED]',\n * transport: remoteTransport({ handler }),\n * })\n */\nexport function redactTransport(options: RedactTransportOptions): Transport {\n const { keys, maxDepth = 20, replacement = '[REDACTED]', transport } = options;\n\n assertNonNegativeInteger(maxDepth, 'redactTransport maxDepth');\n\n for (const key of keys) {\n if (key.includes('.')) {\n warn(\n `redactTransport: key \"${key}\" contains a dot. Dot-path notation is not supported — use the plain field name (e.g. 'password') to redact at any depth.`,\n );\n }\n }\n\n const keySet = new Set(keys);\n\n function redactValue(v: unknown, depth = 0): unknown {\n if (depth > maxDepth) {\n warn(\n `redactTransport: object nesting depth exceeded ${maxDepth} — redaction truncated at this level. Sensitive fields below depth ${maxDepth} may not be redacted.`,\n );\n\n return v;\n }\n\n if (Array.isArray(v)) return v.map((item) => redactValue(item, depth + 1));\n\n if (typeof v === 'object' && v !== null) return redactObject(v as Bindings, depth + 1);\n\n return v;\n }\n\n function redactObject(obj: Bindings, depth = 0): Bindings {\n const result: Bindings = {};\n\n for (const [k, v] of Object.entries(obj)) {\n // Guard against a `__proto__`/`constructor`/`prototype` field name hijacking result's own\n // prototype via the bracket-assignment accessor — see _prototype.ts.\n if (isUnsafeObjectKey(k)) continue;\n\n result[k] = keySet.has(k) ? replacement : redactValue(v, depth);\n }\n\n return result;\n }\n\n return (entry: LogEntry): void => {\n transport({ ...entry, data: redactObject(entry.data as Bindings) });\n };\n}\n\n/* ─── pipe — fault-tolerant fan-out ─── */\n\n/**\n * Fan-out: dispatches each entry to all provided transports independently.\n * A throw in one transport does not prevent others from receiving the entry.\n * Pass `onError` to observe individual transport failures; without it, errors are silently swallowed.\n *\n * @example\n * createLogger({\n * transports: [pipe(consoleTransport(), remoteTransport({ handler }))],\n * })\n *\n * // With error observer:\n * pipe({ onError: (err) => console.error('[pipe]', err) }, consoleTransport(), remoteTransport({ handler }))\n */\nexport function pipe(...transports: Transport[]): Transport;\nexport function pipe(options: PipeOptions, ...transports: Transport[]): Transport;\nexport function pipe(optionsOrTransport: PipeOptions | Transport, ...rest: Transport[]): Transport {\n let opts: PipeOptions;\n let transports: Transport[];\n\n if (typeof optionsOrTransport === 'function') {\n opts = {};\n transports = [optionsOrTransport, ...rest];\n } else {\n opts = optionsOrTransport;\n transports = rest;\n }\n\n return (entry: LogEntry): void => {\n for (const t of transports) {\n try {\n t(entry);\n } catch (err) {\n opts.onError?.(err, entry);\n }\n }\n };\n}\n"],"mappings":";;;;AAsBA,SAAS,IAA0C;CAQjD,OAPI,OAAO,SAAW,MACZ,WAAuF,SAAS,KACpG,aAAa,eACb,eACA,gBAGwE;AAChF;AAqBA,SAAgB,EAAgB,GAA4C;CAC1E,IAAM,EAAE,eAAY,GACd,IAAQ,EAAQ,SAAS,SACzB,IAAM,EAAQ,OAAO,EAAU,GAC/B,IAAU,EAAQ,aAAa,MAAiB,kBAAK,2BAA2B,OAAO,CAAG,GAAG;CAEnG,QAAQ,MAA0B;EAChC,IAAI,CAAC,EAAe,GAAO,EAAM,KAAK,GAAG;EAGzC,IAAM,IAAyB;GAC7B,MAFc,OAAO,KAAK,EAAM,IAAI,CAAC,CAAC,SAAS,IAE/B,EAAM,OAAO,KAAA;GAC7B;GACA,OAAO,EAAM;GACb,SAAS,EAAM;GACf,WAAW,EAAM,aAAa,KAAA;GAC9B,WAAW,EAAM,UAAU,YAAY;EACzC;EAEA,QAAQ,QAAQ,CAAC,CACd,WAAW,EAAQ,EAAM,OAAO,CAAO,CAAC,CAAC,CACzC,OAAO,MAAiB,EAAQ,GAAK,CAAO,CAAC;CAClD;AACF;AAIA,SAAS,IAAkE;CACzE,IAAM,oBAAO,IAAI,QAAQ;CAEzB,QAAQ,GAAc,MAA4B;EAChD,IAAI,OAAO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,GAAG;GAAO,MAAM,EAAa,EAAM,IAAgB;EAAE,CAAC;CACpE;AACF;AAmBA,SAAgB,EAAK,GAA6C,GAAG,GAA8B;CACjG,IAAI,GACA;CAUJ,OARI,OAAO,KAAuB,cAChC,IAAO,CAAC,GACR,IAAa,CAAC,GAAoB,GAAG,CAAI,MAEzC,IAAO,GACP,IAAa,KAGP,MAA0B;EAChC,KAAK,IAAM,KAAK,GACd,IAAI;GACF,EAAE,CAAK;EACT,SAAS,GAAK;GACZ,EAAK,UAAU,GAAK,CAAK;EAC3B;CAEJ;AACF"}
|
package/dist/types.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var e={debug:0,error:3,fatal:4,info:1,off:5,warn:2};function t(t,n){return n
|
|
1
|
+
var e={debug:0,error:3,fatal:4,info:1,off:5,warn:2};function t(t,n){return n!==`off`&&e[t]<=e[n]}exports.PRIORITY=e,exports.isLevelEnabled=t;
|
|
2
2
|
//# sourceMappingURL=types.cjs.map
|
package/dist/types.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.cjs","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,EAAqC,CAChD,MAAO,EACP,MAAO,EACP,MAAO,EACP,KAAM,EACN,IAAK,EACL,KAAM,CACR,EAGA,SAAgB,EAAe,EAAqB,EAA0B,CAG5E,OAFI,IAAU,MAAc,GAErB,EAAS,IAAc,EAAS,EACzC"}
|
|
1
|
+
{"version":3,"file":"types.cjs","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, and continues\n * dispatching the entry to remaining transports — a single misbehaving transport can never crash\n * 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 `await using` declarations. */\n [Symbol.asyncDispose]: () => Promise<void>;\n /** Stop the interval timer, flush remaining entries, and wait for all accepted batches. Idempotent. */\n dispose: () => Promise<void>;\n /** `true` after `dispose()` has been called. */\n readonly disposed: boolean;\n /** Immediately flush buffered entries and wait for their downstream delivery without stopping the timer. */\n flush: () => Promise<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. Must be finite and greater than zero. 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. Must be a finite non-negative integer.\n * When the buffer exceeds this value, the oldest entries are dropped to prevent\n * unbounded memory growth. Unlike `maxSize`, this does NOT trigger a flush.\n * Default: unbounded.\n */\n maxBuffer?: number;\n /** Maximum buffer size before an early flush. Must be a finite positive integer. Default: 50. */\n maxSize?: number;\n /** Callback to receive flushed batches. May return a Promise. */\n onFlush: (entries: LogEntry[]) => void | Promise<void>;\n /**\n * Called when onFlush throws synchronously or rejects asynchronously. Observes the\n * failure; the corresponding `flush()` or `dispose()` promise still rejects.\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 /** Finite 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 * Finite non-negative integer 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,EAAqC,CAChD,MAAO,EACP,MAAO,EACP,MAAO,EACP,KAAM,EACN,IAAK,EACL,KAAM,CACR,EAGA,SAAgB,EAAe,EAAqB,EAA0B,CAG5E,OAFI,IAAU,OAEP,EAAS,IAAc,EAAS,EACzC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -28,9 +28,9 @@ export type LogEntry = {
|
|
|
28
28
|
};
|
|
29
29
|
/**
|
|
30
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
|
|
32
|
-
*
|
|
33
|
-
*
|
|
31
|
+
* If a transport throws, the logger catches it, reports it via a dev-only warning, and continues
|
|
32
|
+
* dispatching the entry to remaining transports — a single misbehaving transport can never crash
|
|
33
|
+
* the caller of `log.info()`/etc. or block its siblings.
|
|
34
34
|
*/
|
|
35
35
|
export type Transport = (entry: LogEntry) => void;
|
|
36
36
|
/**
|
|
@@ -90,39 +90,36 @@ export type JsonTransportOptions = {
|
|
|
90
90
|
};
|
|
91
91
|
/** Handle returned by `batchTransport()`. Pass `handle.transport` to `createLogger({ transports })`. */
|
|
92
92
|
export type BatchHandle = {
|
|
93
|
-
/** Delegates to `dispose()`. Enables `using` declarations. */
|
|
94
|
-
[Symbol.
|
|
95
|
-
/** Stop the interval timer
|
|
96
|
-
dispose: () => void
|
|
93
|
+
/** Delegates to `dispose()`. Enables `await using` declarations. */
|
|
94
|
+
[Symbol.asyncDispose]: () => Promise<void>;
|
|
95
|
+
/** Stop the interval timer, flush remaining entries, and wait for all accepted batches. Idempotent. */
|
|
96
|
+
dispose: () => Promise<void>;
|
|
97
97
|
/** `true` after `dispose()` has been called. */
|
|
98
98
|
readonly disposed: boolean;
|
|
99
|
-
/** Immediately flush buffered entries
|
|
100
|
-
flush: () => void
|
|
99
|
+
/** Immediately flush buffered entries and wait for their downstream delivery without stopping the timer. */
|
|
100
|
+
flush: () => Promise<void>;
|
|
101
101
|
/** The transport function to pass to `createLogger({ transports: [handle.transport] })`. */
|
|
102
102
|
transport: Transport;
|
|
103
103
|
};
|
|
104
104
|
export type BatchTransportOptions = {
|
|
105
|
-
/** Flush interval in milliseconds. Default: 5000. */
|
|
105
|
+
/** Flush interval in milliseconds. Must be finite and greater than zero. Default: 5000. */
|
|
106
106
|
interval?: number;
|
|
107
107
|
/** Minimum level to buffer. Default: 'debug'. */
|
|
108
108
|
level?: LogLevel;
|
|
109
109
|
/**
|
|
110
|
-
* Hard limit on the in-memory buffer size.
|
|
111
|
-
* the oldest entries are dropped to prevent
|
|
112
|
-
* Unlike `maxSize`, this does NOT trigger a flush
|
|
110
|
+
* Hard limit on the in-memory buffer size. Must be a finite non-negative integer.
|
|
111
|
+
* When the buffer exceeds this value, the oldest entries are dropped to prevent
|
|
112
|
+
* unbounded memory growth. Unlike `maxSize`, this does NOT trigger a flush.
|
|
113
113
|
* Default: unbounded.
|
|
114
114
|
*/
|
|
115
115
|
maxBuffer?: number;
|
|
116
|
-
/** Maximum buffer size before an early flush. Default: 50. */
|
|
116
|
+
/** Maximum buffer size before an early flush. Must be a finite positive integer. Default: 50. */
|
|
117
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
|
-
*/
|
|
118
|
+
/** Callback to receive flushed batches. May return a Promise. */
|
|
122
119
|
onFlush: (entries: LogEntry[]) => void | Promise<void>;
|
|
123
120
|
/**
|
|
124
|
-
* Called when onFlush throws synchronously or rejects asynchronously.
|
|
125
|
-
*
|
|
121
|
+
* Called when onFlush throws synchronously or rejects asynchronously. Observes the
|
|
122
|
+
* failure; the corresponding `flush()` or `dispose()` promise still rejects.
|
|
126
123
|
*/
|
|
127
124
|
onFlushError?: (entries: LogEntry[], error: unknown) => void;
|
|
128
125
|
};
|
|
@@ -137,7 +134,7 @@ export type PipeOptions = {
|
|
|
137
134
|
export type SampleTransportOptions = {
|
|
138
135
|
/** Minimum level to sample. Default: 'debug'. */
|
|
139
136
|
level?: LogLevel;
|
|
140
|
-
/**
|
|
137
|
+
/** Finite fraction of entries to forward (0–1). */
|
|
141
138
|
rate: number;
|
|
142
139
|
/** Downstream transport to receive sampled entries. */
|
|
143
140
|
transport: Transport;
|
|
@@ -150,7 +147,7 @@ export type RedactTransportOptions = {
|
|
|
150
147
|
*/
|
|
151
148
|
keys: string[];
|
|
152
149
|
/**
|
|
153
|
-
*
|
|
150
|
+
* Finite non-negative integer maximum object nesting depth to traverse during redaction.
|
|
154
151
|
* Objects deeper than this limit are returned as-is (not redacted).
|
|
155
152
|
* A dev-only warning is emitted when the cap is hit.
|
|
156
153
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EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,CAAC,EAAE,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC,CAAC;IAC5D,mDAAmD;IACnD,QAAQ,CAAC,UAAU,EAAE,SAAS,SAAS,EAAE,CAAC;IAC1C;;;;;OAKG;IACH,GAAG,EAAE,CAAC,UAAU,EAAE,aAAa,KAAK,MAAM,CAAC;IAC3C,IAAI,EAAE,SAAS,CAAC;IAChB;;;;OAIG;IACH,YAAY,EAAE,CAAC,QAAQ,EAAE,QAAQ,KAAK,MAAM,CAAC;CAC9C,CAAC"}
|
package/dist/types.js
CHANGED
package/dist/types.js.map
CHANGED
|
@@ -1 +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"}
|
|
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, and continues\n * dispatching the entry to remaining transports — a single misbehaving transport can never crash\n * 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 `await using` declarations. */\n [Symbol.asyncDispose]: () => Promise<void>;\n /** Stop the interval timer, flush remaining entries, and wait for all accepted batches. Idempotent. */\n dispose: () => Promise<void>;\n /** `true` after `dispose()` has been called. */\n readonly disposed: boolean;\n /** Immediately flush buffered entries and wait for their downstream delivery without stopping the timer. */\n flush: () => Promise<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. Must be finite and greater than zero. 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. Must be a finite non-negative integer.\n * When the buffer exceeds this value, the oldest entries are dropped to prevent\n * unbounded memory growth. Unlike `maxSize`, this does NOT trigger a flush.\n * Default: unbounded.\n */\n maxBuffer?: number;\n /** Maximum buffer size before an early flush. Must be a finite positive integer. Default: 50. */\n maxSize?: number;\n /** Callback to receive flushed batches. May return a Promise. */\n onFlush: (entries: LogEntry[]) => void | Promise<void>;\n /**\n * Called when onFlush throws synchronously or rejects asynchronously. Observes the\n * failure; the corresponding `flush()` or `dispose()` promise still rejects.\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 /** Finite 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 * Finite non-negative integer 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,SAEP,EAAS,MAAc,EAAS;AACzC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vielzeug/rune",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Structured logging — scoped loggers, pluggable transports, log levels, and remote log draining",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -11,7 +11,6 @@
|
|
|
11
11
|
"types": "dist/index.d.ts",
|
|
12
12
|
"exports": {
|
|
13
13
|
".": {
|
|
14
|
-
"source": "./src/index.ts",
|
|
15
14
|
"types": "./dist/index.d.ts",
|
|
16
15
|
"import": "./dist/index.js",
|
|
17
16
|
"require": "./dist/index.cjs"
|
|
@@ -35,9 +34,9 @@
|
|
|
35
34
|
"node": ">=22"
|
|
36
35
|
},
|
|
37
36
|
"devDependencies": {
|
|
38
|
-
"@types/node": "^26.1.
|
|
37
|
+
"@types/node": "^26.1.2",
|
|
39
38
|
"typescript": "^6.0.3",
|
|
40
|
-
"vite": "^8.
|
|
39
|
+
"vite": "^8.2.0",
|
|
41
40
|
"vitest": "^4.1.10"
|
|
42
41
|
}
|
|
43
42
|
}
|