* `JSON.stringify` hardened for debug payloads: circular references collapse * to `"[Circular]"`, `Error` instances expand to `name`/`message`/`stack` * (they would otherwise stringify to `{}`), and `bigint` values become * strings (they would otherwise throw). Never throws — falls back to * `St
(value: unknown)
| 51 | * `String(value)` and, if even that coercion throws, a placeholder. |
| 52 | */ |
| 53 | function stringifyMetaSafely(value: unknown): string { |
| 54 | const seen = new WeakSet<object>() |
| 55 | try { |
| 56 | return JSON.stringify( |
| 57 | value, |
| 58 | (_key, entry: unknown) => { |
| 59 | if (typeof entry === 'bigint') return entry.toString() |
| 60 | if (entry instanceof Error) { |
| 61 | return { |
| 62 | name: entry.name, |
| 63 | message: entry.message, |
| 64 | stack: entry.stack, |
| 65 | } |
| 66 | } |
| 67 | if (typeof entry === 'object' && entry !== null) { |
| 68 | if (seen.has(entry)) return '[Circular]' |
| 69 | seen.add(entry) |
| 70 | } |
| 71 | return entry |
| 72 | }, |
| 73 | 2, |
| 74 | ) |
| 75 | } catch { |
| 76 | try { |
| 77 | return String(value) |
| 78 | } catch { |
| 79 | return '[Unserializable meta]' |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Default `Logger` implementation that routes each level to the matching |