@yieldkit/logger 0.0.0-stage → 0.1.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.
Files changed (55) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +256 -2
  3. package/dist/callback-logger.d.ts +78 -0
  4. package/dist/callback-logger.js +104 -0
  5. package/dist/config.d.ts +29 -0
  6. package/dist/config.js +264 -0
  7. package/dist/context.d.ts +60 -0
  8. package/dist/context.js +100 -0
  9. package/dist/crash.d.ts +114 -0
  10. package/dist/crash.js +215 -0
  11. package/dist/credentials.d.ts +57 -0
  12. package/dist/credentials.js +267 -0
  13. package/dist/error.d.ts +27 -0
  14. package/dist/error.js +173 -0
  15. package/dist/formatters.d.ts +53 -0
  16. package/dist/formatters.js +137 -0
  17. package/dist/index.d.ts +16 -0
  18. package/dist/index.js +15 -0
  19. package/dist/layer.d.ts +31 -0
  20. package/dist/layer.js +78 -0
  21. package/dist/line-cap.d.ts +6 -0
  22. package/dist/line-cap.js +367 -0
  23. package/dist/log-fan-out.d.ts +24 -0
  24. package/dist/log-fan-out.js +83 -0
  25. package/dist/metrics.d.ts +19 -0
  26. package/dist/metrics.js +45 -0
  27. package/dist/omit-undefined.js +10 -0
  28. package/dist/otlp/flush.d.ts +8 -0
  29. package/dist/otlp/flush.js +11 -0
  30. package/dist/otlp/index.d.ts +4 -0
  31. package/dist/otlp/index.js +4 -0
  32. package/dist/otlp/telemetry.d.ts +86 -0
  33. package/dist/otlp/telemetry.js +230 -0
  34. package/dist/otlp/tracing.d.ts +11 -0
  35. package/dist/otlp/tracing.js +13 -0
  36. package/dist/redact-cause.js +92 -0
  37. package/dist/redact-walk.d.ts +19 -0
  38. package/dist/redact-walk.js +290 -0
  39. package/dist/redact.d.ts +104 -0
  40. package/dist/redact.js +271 -0
  41. package/dist/request-id.d.ts +21 -0
  42. package/dist/request-id.js +28 -0
  43. package/dist/testing/index.d.ts +55 -0
  44. package/dist/testing/index.js +42 -0
  45. package/dist/types.d.ts +212 -0
  46. package/docs/callback-logger.md +38 -0
  47. package/docs/configuration.md +56 -0
  48. package/docs/crash-and-startup.md +57 -0
  49. package/docs/durations-and-request-ids.md +30 -0
  50. package/docs/formats.md +78 -0
  51. package/docs/logging.md +53 -0
  52. package/docs/redaction.md +87 -0
  53. package/docs/telemetry.md +78 -0
  54. package/docs/testing.md +60 -0
  55. package/package.json +74 -3
package/dist/config.js ADDED
@@ -0,0 +1,264 @@
1
+ import { Config, Option, Schema } from "effect";
2
+ // src/config.ts
3
+ /**
4
+ * Accepted `LOG_LEVEL` spellings, including `warning` and `silent`.
5
+ */
6
+ const LOG_LEVEL_ALIASES = {
7
+ /**
8
+ * Enables every severity.
9
+ */
10
+ all: "All",
11
+ /**
12
+ * Enables trace and above.
13
+ */
14
+ trace: "Trace",
15
+ /**
16
+ * Enables debug and above.
17
+ */
18
+ debug: "Debug",
19
+ /**
20
+ * Enables info and above. This is the default.
21
+ */
22
+ info: "Info",
23
+ /**
24
+ * Enables warnings and above.
25
+ */
26
+ warn: "Warn",
27
+ /**
28
+ * Alias of `warn`.
29
+ */
30
+ warning: "Warn",
31
+ /**
32
+ * Enables errors and fatal lines only.
33
+ */
34
+ error: "Error",
35
+ /**
36
+ * Enables fatal lines only.
37
+ */
38
+ fatal: "Fatal",
39
+ /**
40
+ * Drops every line.
41
+ */
42
+ none: "None",
43
+ /**
44
+ * Alias of `none`.
45
+ */
46
+ silent: "None"
47
+ };
48
+ /**
49
+ * Accepted `LOG_FORMAT` spellings.
50
+ */
51
+ const LOG_FORMAT_ALIASES = {
52
+ /**
53
+ * JSON in production, pretty otherwise.
54
+ */
55
+ auto: "auto",
56
+ /**
57
+ * One JSON object per line.
58
+ */
59
+ json: "json",
60
+ /**
61
+ * One structured object per line, not serialized.
62
+ */
63
+ structured: "structured",
64
+ /**
65
+ * Effect's colored console format.
66
+ */
67
+ pretty: "pretty",
68
+ /**
69
+ * One logfmt line.
70
+ */
71
+ logfmt: "logfmt"
72
+ };
73
+ /**
74
+ * Accepted `LOG_COLORS` spellings.
75
+ */
76
+ const LOG_COLOR_ALIASES = {
77
+ /**
78
+ * Follows the terminal.
79
+ */
80
+ auto: "auto",
81
+ /**
82
+ * Forces ANSI color.
83
+ */
84
+ true: true,
85
+ /**
86
+ * Alias of `true`.
87
+ */
88
+ "1": true,
89
+ /**
90
+ * Alias of `true`.
91
+ */
92
+ yes: true,
93
+ /**
94
+ * Alias of `true`.
95
+ */
96
+ on: true,
97
+ /**
98
+ * Alias of `true`.
99
+ */
100
+ y: true,
101
+ /**
102
+ * Disables ANSI color.
103
+ */
104
+ false: false,
105
+ /**
106
+ * Alias of `false`.
107
+ */
108
+ "0": false,
109
+ /**
110
+ * Alias of `false`.
111
+ */
112
+ no: false,
113
+ /**
114
+ * Alias of `false`.
115
+ */
116
+ off: false,
117
+ /**
118
+ * Alias of `false`.
119
+ */
120
+ n: false
121
+ };
122
+ /**
123
+ * Accepted `LOG_STDERR` spellings: the boolean spellings Effect's `Config.Boolean` accepts, plus
124
+ * `leveled`.
125
+ */
126
+ const LOG_STDERR_ALIASES = {
127
+ /**
128
+ * Warn, Error and Fatal go to stderr, everything else to stdout. This is the default.
129
+ */
130
+ leveled: "leveled",
131
+ /**
132
+ * Every line goes to stderr.
133
+ */
134
+ true: true,
135
+ /**
136
+ * Alias of `true`.
137
+ */
138
+ yes: true,
139
+ /**
140
+ * Alias of `true`.
141
+ */
142
+ on: true,
143
+ /**
144
+ * Alias of `true`.
145
+ */
146
+ "1": true,
147
+ /**
148
+ * Alias of `true`.
149
+ */
150
+ y: true,
151
+ /**
152
+ * Every line goes to stdout.
153
+ */
154
+ false: false,
155
+ /**
156
+ * Alias of `false`.
157
+ */
158
+ no: false,
159
+ /**
160
+ * Alias of `false`.
161
+ */
162
+ off: false,
163
+ /**
164
+ * Alias of `false`.
165
+ */
166
+ "0": false,
167
+ /**
168
+ * Alias of `false`.
169
+ */
170
+ n: false
171
+ };
172
+ /**
173
+ * Looks up the trimmed, lowercased `raw` among the table's own spellings, so an inherited name such
174
+ * as `constructor` or `__proto__` is unknown rather than a value from `Object.prototype`.
175
+ */
176
+ const lookupAlias = (table, raw) => {
177
+ const spelling = raw.trim().toLowerCase();
178
+ return Object.hasOwn(table, spelling) ? table[spelling] : void 0;
179
+ };
180
+ /**
181
+ * Parses a case-insensitive `LOG_LEVEL` value. An unknown spelling returns `undefined`.
182
+ */
183
+ const parseLogLevel = (raw) => lookupAlias(LOG_LEVEL_ALIASES, raw);
184
+ /**
185
+ * Parses a case-insensitive `LOG_FORMAT` value. An unknown spelling returns `undefined`.
186
+ */
187
+ const parseLogFormat = (raw) => lookupAlias(LOG_FORMAT_ALIASES, raw);
188
+ /**
189
+ * Parses a case-insensitive `LOG_STDERR` value: a boolean spelling or `leveled`. An unknown
190
+ * spelling returns `undefined`.
191
+ */
192
+ const parseLogStderr = (raw) => lookupAlias(LOG_STDERR_ALIASES, raw);
193
+ /**
194
+ * Parses a case-insensitive `LOG_COLORS` value. An unknown spelling returns `undefined`.
195
+ */
196
+ const parseLogColors = (raw) => lookupAlias(LOG_COLOR_ALIASES, raw);
197
+ /**
198
+ * Resolves `auto` to `json` when `env` is `production` or `prod`, otherwise to `pretty`. Any other
199
+ * format is returned unchanged.
200
+ */
201
+ const resolveLogFormat = (format, env) => {
202
+ if (format !== "auto") return format;
203
+ const normalizedEnv = env?.trim().toLowerCase();
204
+ return normalizedEnv === "production" || normalizedEnv === "prod" ? "json" : "pretty";
205
+ };
206
+ /**
207
+ * `LOG_LEVEL` text that `parseLogLevel` accepts.
208
+ */
209
+ const LogLevelInputSchema = Schema.String.check(Schema.makeFilter((raw) => parseLogLevel(raw) !== void 0, { message: "Expected LOG_LEVEL to be one of all, trace, debug, info, warn, warning, error, fatal, none, silent" }));
210
+ /**
211
+ * `LOG_FORMAT` text that `parseLogFormat` accepts.
212
+ */
213
+ const LogFormatInputSchema = Schema.String.check(Schema.makeFilter((raw) => parseLogFormat(raw) !== void 0, { message: "Expected LOG_FORMAT to be one of auto, json, structured, pretty, logfmt" }));
214
+ /**
215
+ * `LOG_COLORS` text that `parseLogColors` accepts.
216
+ */
217
+ const LogColorsInputSchema = Schema.String.check(Schema.makeFilter((raw) => parseLogColors(raw) !== void 0, { message: "Expected LOG_COLORS to be one of auto, true, yes, on, 1, y, false, no, off, 0, n" }));
218
+ /**
219
+ * `LOG_STDERR` text that `parseLogStderr` accepts.
220
+ */
221
+ const LogStderrInputSchema = Schema.String.check(Schema.makeFilter((raw) => parseLogStderr(raw) !== void 0, { message: "Expected LOG_STDERR to be one of true, yes, on, 1, y, false, no, off, 0, n, leveled" }));
222
+ /**
223
+ * Optional `LOG_LEVEL` config. A missing variable is left for the caller default.
224
+ */
225
+ const LogLevelConfig = Config.schema(LogLevelInputSchema, "LOG_LEVEL").pipe(Config.map((raw) => parseLogLevel(raw) ?? "Info"));
226
+ /**
227
+ * Optional `LOG_FORMAT` config. A missing variable is left for the caller default.
228
+ */
229
+ const LogFormatConfig = Config.schema(LogFormatInputSchema, "LOG_FORMAT").pipe(Config.map((raw) => parseLogFormat(raw) ?? "auto"));
230
+ /**
231
+ * Optional `LOG_COLORS` config. A missing variable is left for the caller default.
232
+ */
233
+ const LogColorsConfig = Config.schema(LogColorsInputSchema, "LOG_COLORS").pipe(Config.map((raw) => parseLogColors(raw) ?? "auto"));
234
+ /**
235
+ * Optional `LOG_STDERR` config. A missing variable is left for the caller default.
236
+ */
237
+ const LogStderrConfig = Config.schema(LogStderrInputSchema, "LOG_STDERR").pipe(Config.map((raw) => parseLogStderr(raw) ?? "leveled"));
238
+ /**
239
+ * Reads logger settings from the environment and fills gaps from `defaults`. An invalid
240
+ * `LOG_LEVEL`, `LOG_FORMAT`, `LOG_STDERR`, or `LOG_COLORS` fails with `ConfigError`.
241
+ */
242
+ const readLoggerConfig = (defaults = {}) => Config.all({
243
+ minLevel: Config.option(LogLevelConfig),
244
+ format: Config.option(LogFormatConfig),
245
+ stderr: Config.option(LogStderrConfig),
246
+ colors: Config.option(LogColorsConfig),
247
+ nodeEnv: Config.option(Config.NonEmptyString("NODE_ENV"))
248
+ }).pipe(Config.map(({ minLevel, format, stderr, colors, nodeEnv }) => {
249
+ const env = Option.getOrElse(nodeEnv, () => defaults.env ?? "development");
250
+ const configuredFormat = Option.getOrElse(format, () => defaults.format ?? "auto");
251
+ return {
252
+ service: defaults.service,
253
+ env,
254
+ version: defaults.version,
255
+ format: resolveLogFormat(configuredFormat, env),
256
+ minLevel: Option.getOrElse(minLevel, () => defaults.minLevel ?? "Info"),
257
+ stderr: Option.getOrElse(stderr, () => defaults.stderr ?? "leveled"),
258
+ colors: Option.getOrElse(colors, () => defaults.colors ?? "auto"),
259
+ includeTracerLogger: defaults.includeTracerLogger ?? true,
260
+ maxLineBytes: defaults.maxLineBytes,
261
+ redaction: defaults.redaction
262
+ };
263
+ }));
264
+ export { parseLogColors, parseLogFormat, parseLogLevel, parseLogStderr, readLoggerConfig, resolveLogFormat };
@@ -0,0 +1,60 @@
1
+ import { LogFields } from "./types.js";
2
+ import { Cause, Effect, LogLevel, Scope } from "effect";
3
+ /**
4
+ * Returns a copy of `fields` with `undefined` values removed. An empty input returns an empty
5
+ * object and does not annotate the effect. Every key, `__proto__` included, is copied as an own
6
+ * data property. A `Redactable` record (Effect's `Headers`) is copied from its redacted form,
7
+ * resolved in the current fiber's context. A property whose getter throws becomes
8
+ * `[Unserializable]`, and an object whose keys cannot be listed gives `{}`, so this never throws.
9
+ */
10
+ export declare const compactLogFields: (fields: LogFields) => LogFields;
11
+ /**
12
+ * Annotates `effect` with `fields`. A set of only `undefined` values is a no-op. A `Redactable`
13
+ * record (Effect's `Headers`) is resolved when the effect runs, so the redacted names in the
14
+ * running fiber's context apply.
15
+ */
16
+ export declare const withLogContext: (fields: LogFields) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
17
+ /**
18
+ * Annotates the current scope with `fields`. A set of only `undefined` values is a no-op. A
19
+ * `Redactable` record (Effect's `Headers`) is resolved when the effect runs.
20
+ */
21
+ export declare const withLogContextScoped: (fields: LogFields) => Effect.Effect<void, never, Scope.Scope>;
22
+ /**
23
+ * Writes one line at `level`. Optional fields are annotations. An optional cause is attached.
24
+ */
25
+ export declare const logWithFields: (level: LogLevel.Severity, message: string, fields?: LogFields, cause?: Cause.Cause<unknown>) => Effect.Effect<void>;
26
+ /**
27
+ * Writes a trace line. Hidden unless the minimum level is `Trace` or `All`.
28
+ */
29
+ export declare const logTrace: (message: string, fields?: LogFields) => Effect.Effect<void>;
30
+ /**
31
+ * Writes a debug line. Hidden at the default `Info` minimum.
32
+ */
33
+ export declare const logDebug: (message: string, fields?: LogFields) => Effect.Effect<void>;
34
+ /**
35
+ * Writes an info line. This is the default severity for operational events.
36
+ */
37
+ export declare const logInfo: (message: string, fields?: LogFields) => Effect.Effect<void>;
38
+ /**
39
+ * Writes a warning line. Use it for a degraded path that still completed.
40
+ */
41
+ export declare const logWarning: (message: string, fields?: LogFields) => Effect.Effect<void>;
42
+ /**
43
+ * Writes an error line without a cause. Prefer `logFailure` when an error value exists.
44
+ */
45
+ export declare const logError: (message: string, fields?: LogFields) => Effect.Effect<void>;
46
+ /**
47
+ * Writes an error line and attaches `cause` so the formatter can extract a summary.
48
+ */
49
+ export declare const logErrorCause: (message: string, cause: Cause.Cause<unknown>, fields?: LogFields) => Effect.Effect<void>;
50
+ /**
51
+ * Writes an error line, attaches `error` as a fail cause, and adds `errorMessage`, `errorTag`,
52
+ * `errorName`, and `errorStatus` when those can be read, from the redacted form of a `Redactable`
53
+ * error. The fields are read when the effect runs, not when it is built, and reading them never
54
+ * throws.
55
+ */
56
+ export declare const logFailure: (message: string, error: unknown, fields?: LogFields) => Effect.Effect<void>;
57
+ /**
58
+ * Writes a fatal line. An optional cause is attached and is not wrapped again.
59
+ */
60
+ export declare const logFatal: (message: string, cause?: Cause.Cause<unknown>, fields?: LogFields) => Effect.Effect<void>;
@@ -0,0 +1,100 @@
1
+ import { extractErrorFields } from "./error.js";
2
+ import { ownEntries, withVisible } from "./redact-walk.js";
3
+ import { Cause, Effect, Predicate, Redactable } from "effect";
4
+ // src/context.ts
5
+ /**
6
+ * Reads the own entries that become the fields: those of the redacted form of a `Redactable` record
7
+ * (such as Effect's `Headers`, which hides the names in `Headers.CurrentRedactedNames`), otherwise
8
+ * those of `fields` itself. Each property is read on its own, so a getter that throws gives
9
+ * `[Unserializable]` for that property only. A redacted form that is not an object, or an object
10
+ * whose keys cannot be listed, gives no entries.
11
+ */
12
+ const visibleEntries = (fields) => {
13
+ try {
14
+ return withVisible(fields, (visible) => Predicate.isObject(visible) ? ownEntries(visible) : []);
15
+ } catch {
16
+ return [];
17
+ }
18
+ };
19
+ /**
20
+ * Returns a copy of `fields` with `undefined` values removed. An empty input returns an empty
21
+ * object and does not annotate the effect. Every key, `__proto__` included, is copied as an own
22
+ * data property. A `Redactable` record (Effect's `Headers`) is copied from its redacted form,
23
+ * resolved in the current fiber's context. A property whose getter throws becomes
24
+ * `[Unserializable]`, and an object whose keys cannot be listed gives `{}`, so this never throws.
25
+ */
26
+ const compactLogFields = (fields) => Object.fromEntries(visibleEntries(fields).filter(([, value]) => value !== void 0));
27
+ /**
28
+ * Annotates `effect` with the compacted `fields`.
29
+ */
30
+ const annotateWith = (effect, fields) => {
31
+ const compacted = compactLogFields(fields);
32
+ if (Object.keys(compacted).length === 0) return effect;
33
+ return effect.pipe(Effect.annotateLogs(compacted));
34
+ };
35
+ /**
36
+ * Annotates `effect` with `fields`. A set of only `undefined` values is a no-op. A `Redactable`
37
+ * record (Effect's `Headers`) is resolved when the effect runs, so the redacted names in the
38
+ * running fiber's context apply.
39
+ */
40
+ const withLogContext = (fields) => (effect) => Redactable.isRedactable(fields) ? Effect.suspend(() => annotateWith(effect, fields)) : annotateWith(effect, fields);
41
+ /**
42
+ * Annotates the current scope with `fields`. A set of only `undefined` values is a no-op. A
43
+ * `Redactable` record (Effect's `Headers`) is resolved when the effect runs.
44
+ */
45
+ const withLogContextScoped = (fields) => Redactable.isRedactable(fields) ? Effect.suspend(() => annotateScopedWith(fields)) : annotateScopedWith(fields);
46
+ /**
47
+ * Annotates the current scope with the compacted `fields`.
48
+ */
49
+ const annotateScopedWith = (fields) => {
50
+ const compacted = compactLogFields(fields);
51
+ if (Object.keys(compacted).length === 0) return Effect.void;
52
+ return Effect.annotateLogsScoped(compacted);
53
+ };
54
+ /**
55
+ * Writes one line at `level`. Optional fields are annotations. An optional cause is attached.
56
+ */
57
+ const logWithFields = (level, message, fields, cause) => {
58
+ const baseLog = cause !== void 0 ? Effect.logWithLevel(level)(message, cause) : Effect.logWithLevel(level)(message);
59
+ if (fields === void 0) return baseLog;
60
+ return baseLog.pipe(withLogContext(fields));
61
+ };
62
+ /**
63
+ * Writes a trace line. Hidden unless the minimum level is `Trace` or `All`.
64
+ */
65
+ const logTrace = (message, fields) => logWithFields("Trace", message, fields);
66
+ /**
67
+ * Writes a debug line. Hidden at the default `Info` minimum.
68
+ */
69
+ const logDebug = (message, fields) => logWithFields("Debug", message, fields);
70
+ /**
71
+ * Writes an info line. This is the default severity for operational events.
72
+ */
73
+ const logInfo = (message, fields) => logWithFields("Info", message, fields);
74
+ /**
75
+ * Writes a warning line. Use it for a degraded path that still completed.
76
+ */
77
+ const logWarning = (message, fields) => logWithFields("Warn", message, fields);
78
+ /**
79
+ * Writes an error line without a cause. Prefer `logFailure` when an error value exists.
80
+ */
81
+ const logError = (message, fields) => logWithFields("Error", message, fields);
82
+ /**
83
+ * Writes an error line and attaches `cause` so the formatter can extract a summary.
84
+ */
85
+ const logErrorCause = (message, cause, fields) => logWithFields("Error", message, fields, cause);
86
+ /**
87
+ * Writes an error line, attaches `error` as a fail cause, and adds `errorMessage`, `errorTag`,
88
+ * `errorName`, and `errorStatus` when those can be read, from the redacted form of a `Redactable`
89
+ * error. The fields are read when the effect runs, not when it is built, and reading them never
90
+ * throws.
91
+ */
92
+ const logFailure = (message, error, fields) => Effect.suspend(() => logWithFields("Error", message, {
93
+ ...extractErrorFields(error),
94
+ ...compactLogFields(fields ?? {})
95
+ }, Cause.fail(error)));
96
+ /**
97
+ * Writes a fatal line. An optional cause is attached and is not wrapped again.
98
+ */
99
+ const logFatal = (message, cause, fields) => logWithFields("Fatal", message, fields, cause);
100
+ export { compactLogFields, logDebug, logError, logErrorCause, logFailure, logFatal, logInfo, logTrace, logWarning, logWithFields, withLogContext, withLogContextScoped };
@@ -0,0 +1,114 @@
1
+ import { RedactionOptions } from "./credentials.js";
2
+ import { Cause, Duration, Effect, Layer, Runtime } from "effect";
3
+ /**
4
+ * Process event that carries a crash.
5
+ */
6
+ type CrashEvent = 'uncaughtException' | 'unhandledRejection';
7
+ /**
8
+ * Listener registered for a crash event. It receives the thrown or rejected value.
9
+ */
10
+ type CrashListener = (error: unknown) => void;
11
+ /**
12
+ * Where crash listeners are registered. Node's and Bun's `process` fits, and so does an
13
+ * `EventEmitter`.
14
+ */
15
+ export type CrashTarget = {
16
+ /**
17
+ * Adds a listener for a crash event.
18
+ */
19
+ readonly on: (event: CrashEvent, listener: CrashListener) => void;
20
+ /**
21
+ * Removes a listener added with `on`.
22
+ */
23
+ readonly off: (event: CrashEvent, listener: CrashListener) => void;
24
+ };
25
+ /**
26
+ * Options for `makeCrashHandlersLayer`.
27
+ */
28
+ export type CrashHandlerOptions = {
29
+ /**
30
+ * Called once with exit code 1 when the crash was logged and flushed, or when `flushTimeout`
31
+ * passed first, even if the flush is still running. Defaults to `process.exit`.
32
+ */
33
+ readonly exit?: ((code: number) => void) | undefined;
34
+ /**
35
+ * Object the `uncaughtException` and `unhandledRejection` listeners are added to and removed
36
+ * from. Defaults to `process`.
37
+ */
38
+ readonly target?: CrashTarget | undefined;
39
+ /**
40
+ * Runs after the Fatal line and before the exit, in the context the layer was built in. Use it to
41
+ * flush buffered exporters, such as `flushOtlp` from `@yieldkit/logger/otlp`. Defaults to doing
42
+ * nothing.
43
+ */
44
+ readonly flush?: Effect.Effect<void> | undefined;
45
+ /**
46
+ * Longest time to wait for the Fatal line and the flush before exiting. Defaults to five seconds.
47
+ * A value that is not a finite, positive duration (`NaN`, zero, negative or infinite included)
48
+ * makes the layer die when it is built, so a bad setting shows up at startup rather than at the
49
+ * first crash.
50
+ */
51
+ readonly flushTimeout?: Duration.Input | undefined;
52
+ };
53
+ /**
54
+ * Registers `uncaughtException` and `unhandledRejection` listeners while the layer is up and
55
+ * removes them when its scope closes.
56
+ *
57
+ * Each crash is logged at Fatal as `process.crashed` with a die cause and a `crashKind` annotation,
58
+ * through the loggers and annotations that were in the context when the layer was built, and then
59
+ * `options.flush` runs in that same context. When a logger throws on the crash value, a Fatal line
60
+ * with `crashValue: 'unloggable'` is written instead; the loggers `makeLoggerLayer` installs never
61
+ * throw, and write `process.crashed` without the cause and annotations. The flush runs either way,
62
+ * bounded together with the log line by `flushTimeout`, and `exit(1)` runs once, when reporting
63
+ * ends or when `flushTimeout` has passed, whichever comes first. The deadline runs on its own
64
+ * fiber, so a log line or flush that cannot be interrupted does not delay the exit; only code that
65
+ * blocks the thread synchronously (a busy loop in a logger or flusher) can, since no timer fires
66
+ * until it returns. A second crash while the first is being handled does nothing, so the first
67
+ * handler still exits.
68
+ *
69
+ * Registering a listener replaces Node's own crash, which is why the handler exits itself. Build
70
+ * this layer with the logger layers provided to it (`Layer.provide`), because a sibling in
71
+ * `Layer.mergeAll` does not see the loggers its sibling installs.
72
+ */
73
+ export declare const makeCrashHandlersLayer: (options?: CrashHandlerOptions) => Layer.Layer<never>;
74
+ /**
75
+ * `makeCrashHandlersLayer()` with the default options: the global `process`, `process.exit` and a
76
+ * five second flush timeout.
77
+ */
78
+ export declare const CrashHandlersLive: Layer.Layer<never>;
79
+ /**
80
+ * Options for {@link makeStartupFailureTeardown}.
81
+ */
82
+ export type StartupFailureOptions = {
83
+ /**
84
+ * Redaction options (extra sensitive and allowed names, `scrubText`) used when the failure is
85
+ * described. Pass the same value as the logger layer's `redaction`, so a key masked in log lines
86
+ * is masked on the terminal too.
87
+ */
88
+ readonly redaction?: RedactionOptions | undefined;
89
+ };
90
+ /**
91
+ * Renders a failure to build the app (for example a bad `LOG_LEVEL`) as console text, for use where
92
+ * the loggers are not available. When every reason of the cause is a `ConfigError` failure, the
93
+ * text is `invalid configuration: ` plus their redacted messages. Any other cause, including a
94
+ * `ConfigError` next to a defect, becomes `failed to start: ` plus the sanitized pretty cause, so
95
+ * no reason is hidden and a secret in an error message or span name never reaches the terminal.
96
+ * Redacts with the built-in sensitive names plus `redaction`.
97
+ */
98
+ export declare const describeStartupFailure: (cause: Cause.Cause<unknown>, redaction?: RedactionOptions) => string;
99
+ /**
100
+ * Builds a `Runtime.Teardown` for `runMain` that reports a failed program with
101
+ * {@link describeStartupFailure}, redacting with the built-in sensitive names plus
102
+ * `options.redaction`. A success or an interrupt-only exit (Ctrl-C, `SIGTERM`) goes to
103
+ * `Runtime.defaultTeardown`, which keeps its exit code. Any other failure is passed to `write`
104
+ * once, as one string, and exits with code 1, even when the failure cannot be described or `write`
105
+ * throws. Pair it with `disableErrorReporting: true`, or `runMain` also prints the raw, unredacted
106
+ * cause.
107
+ */
108
+ export declare const makeStartupFailureTeardown: (write: (line: string) => void, options?: StartupFailureOptions) => Runtime.Teardown;
109
+ /**
110
+ * {@link makeStartupFailureTeardown} writing to `console.error`, for `BunRuntime.runMain({
111
+ * disableErrorReporting: true, teardown: startupFailureTeardown })`.
112
+ */
113
+ export declare const startupFailureTeardown: Runtime.Teardown;
114
+ export {};