@optique/logtape 0.8.0-dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,615 @@
1
+ import { ValueParser } from "@optique/core/valueparser";
2
+ import { Message } from "@optique/core/message";
3
+ import { Config, LogLevel, LogLevel as LogLevel$1, LogRecord, Sink, Sink as Sink$1 } from "@logtape/logtape";
4
+ import { Parser } from "@optique/core/parser";
5
+
6
+ //#region src/loglevel.d.ts
7
+ /**
8
+ * All valid log levels in order from lowest to highest severity.
9
+ */
10
+ declare const LOG_LEVELS: readonly LogLevel$1[];
11
+ /**
12
+ * Options for creating a log level value parser.
13
+ * @since 0.8.0
14
+ */
15
+ interface LogLevelOptions {
16
+ /**
17
+ * The metavariable name for this parser. This is used in help messages to
18
+ * indicate what kind of value this parser expects.
19
+ * @default `"LEVEL"`
20
+ */
21
+ readonly metavar?: string;
22
+ /**
23
+ * Custom error messages for log level parsing failures.
24
+ */
25
+ readonly errors?: {
26
+ /**
27
+ * Custom error message when input is not a valid log level.
28
+ * Can be a static message or a function that receives the input string.
29
+ */
30
+ invalidLevel?: Message | ((input: string) => Message);
31
+ };
32
+ }
33
+ /**
34
+ * Creates a {@link ValueParser} for LogTape log levels.
35
+ *
36
+ * This parser validates that the input is one of the valid LogTape severity
37
+ * levels: `"trace"`, `"debug"`, `"info"`, `"warning"`, `"error"`, or `"fatal"`.
38
+ * The parsing is case-insensitive.
39
+ *
40
+ * @param options Configuration options for the log level parser.
41
+ * @returns A {@link ValueParser} that converts string input to {@link LogLevel}.
42
+ *
43
+ * @example Basic usage
44
+ * ```typescript
45
+ * import { logLevel } from "@optique/logtape";
46
+ * import { option, withDefault } from "@optique/core";
47
+ *
48
+ * const parser = object({
49
+ * level: withDefault(
50
+ * option("--log-level", "-l", logLevel()),
51
+ * "info"
52
+ * ),
53
+ * });
54
+ * ```
55
+ *
56
+ * @example Custom metavar
57
+ * ```typescript
58
+ * import { logLevel } from "@optique/logtape";
59
+ *
60
+ * const parser = logLevel({ metavar: "LOG_LEVEL" });
61
+ * ```
62
+ *
63
+ * @since 0.8.0
64
+ */
65
+ declare function logLevel(options?: LogLevelOptions): ValueParser<LogLevel$1>;
66
+ //#endregion
67
+ //#region src/verbosity.d.ts
68
+ /**
69
+ * Options for creating a verbosity parser.
70
+ * @since 0.8.0
71
+ */
72
+ interface VerbosityOptions {
73
+ /**
74
+ * Short option name for the verbosity flag.
75
+ * @default `"-v"`
76
+ */
77
+ readonly short?: string;
78
+ /**
79
+ * Long option name for the verbosity flag.
80
+ * @default `"--verbose"`
81
+ */
82
+ readonly long?: string;
83
+ /**
84
+ * The base log level when no verbosity flags are provided.
85
+ * @default `"warning"`
86
+ */
87
+ readonly baseLevel?: LogLevel$1;
88
+ /**
89
+ * Description to show in help text.
90
+ */
91
+ readonly description?: Message;
92
+ }
93
+ /**
94
+ * Creates a parser for verbosity flags (`-v`, `-vv`, `-vvv`, etc.).
95
+ *
96
+ * This parser accumulates `-v` flags to determine the log level.
97
+ * Each additional `-v` flag increases the verbosity (decreases the log level
98
+ * severity).
99
+ *
100
+ * Default level mapping with `baseLevel: "warning"`:
101
+ * - No flags: `"warning"`
102
+ * - `-v`: `"info"`
103
+ * - `-vv`: `"debug"`
104
+ * - `-vvv` or more: `"trace"`
105
+ *
106
+ * @param options Configuration options for the verbosity parser.
107
+ * @returns A {@link Parser} that produces a {@link LogLevel}.
108
+ *
109
+ * @example Basic usage
110
+ * ```typescript
111
+ * import { verbosity } from "@optique/logtape";
112
+ * import { object } from "@optique/core/constructs";
113
+ *
114
+ * const parser = object({
115
+ * logLevel: verbosity(),
116
+ * });
117
+ *
118
+ * // No flags -> "warning"
119
+ * // -v -> "info"
120
+ * // -vv -> "debug"
121
+ * // -vvv -> "trace"
122
+ * ```
123
+ *
124
+ * @example Custom base level
125
+ * ```typescript
126
+ * import { verbosity } from "@optique/logtape";
127
+ *
128
+ * const parser = verbosity({ baseLevel: "error" });
129
+ * // No flags -> "error"
130
+ * // -v -> "warning"
131
+ * // -vv -> "info"
132
+ * // -vvv -> "debug"
133
+ * // -vvvv -> "trace"
134
+ * ```
135
+ *
136
+ * @since 0.8.0
137
+ */
138
+ declare function verbosity(options?: VerbosityOptions): Parser<LogLevel$1, unknown>;
139
+ //#endregion
140
+ //#region src/debug.d.ts
141
+ /**
142
+ * Options for creating a debug flag parser.
143
+ * @since 0.8.0
144
+ */
145
+ interface DebugOptions {
146
+ /**
147
+ * Short option name for the debug flag.
148
+ * @default `"-d"`
149
+ */
150
+ readonly short?: string;
151
+ /**
152
+ * Long option name for the debug flag.
153
+ * @default `"--debug"`
154
+ */
155
+ readonly long?: string;
156
+ /**
157
+ * The log level to use when the debug flag is present.
158
+ * @default `"debug"`
159
+ */
160
+ readonly debugLevel?: LogLevel$1;
161
+ /**
162
+ * The log level to use when the debug flag is not present.
163
+ * @default `"info"`
164
+ */
165
+ readonly normalLevel?: LogLevel$1;
166
+ /**
167
+ * Description to show in help text.
168
+ */
169
+ readonly description?: Message;
170
+ }
171
+ /**
172
+ * Creates a parser for a debug flag (`-d`, `--debug`).
173
+ *
174
+ * This parser provides a simple boolean toggle for enabling debug-level
175
+ * logging. When the flag is present, it returns the debug level; otherwise,
176
+ * it returns the normal level.
177
+ *
178
+ * @param options Configuration options for the debug flag parser.
179
+ * @returns A {@link Parser} that produces a {@link LogLevel}.
180
+ *
181
+ * @example Basic usage
182
+ * ```typescript
183
+ * import { debug } from "@optique/logtape";
184
+ * import { object } from "@optique/core/constructs";
185
+ *
186
+ * const parser = object({
187
+ * logLevel: debug(),
188
+ * });
189
+ *
190
+ * // No flag -> "info"
191
+ * // --debug or -d -> "debug"
192
+ * ```
193
+ *
194
+ * @example Custom levels
195
+ * ```typescript
196
+ * import { debug } from "@optique/logtape";
197
+ *
198
+ * const parser = debug({
199
+ * debugLevel: "trace",
200
+ * normalLevel: "warning",
201
+ * });
202
+ * // No flag -> "warning"
203
+ * // --debug -> "trace"
204
+ * ```
205
+ *
206
+ * @since 0.8.0
207
+ */
208
+ declare function debug(options?: DebugOptions): Parser<LogLevel$1, unknown>;
209
+ //#endregion
210
+ //#region src/output.d.ts
211
+ /**
212
+ * Represents a log output destination.
213
+ *
214
+ * This is a discriminated union type that represents either console output
215
+ * or file output.
216
+ * @since 0.8.0
217
+ */
218
+ type LogOutput = {
219
+ readonly type: "console";
220
+ } | {
221
+ readonly type: "file";
222
+ readonly path: string;
223
+ };
224
+ /**
225
+ * Options for configuring console sink creation.
226
+ * @since 0.8.0
227
+ */
228
+ interface ConsoleSinkOptions {
229
+ /**
230
+ * The stream to write to. Either `"stdout"` or `"stderr"`.
231
+ * @default `"stderr"`
232
+ */
233
+ readonly stream?: "stdout" | "stderr";
234
+ /**
235
+ * A function that determines which stream to use based on the log level.
236
+ * If provided, this takes precedence over the `stream` option.
237
+ *
238
+ * @example
239
+ * ```typescript
240
+ * // Write warnings and above to stderr, info and below to stdout
241
+ * streamResolver: (level) =>
242
+ * level === "warning" || level === "error" || level === "fatal"
243
+ * ? "stderr"
244
+ * : "stdout"
245
+ * ```
246
+ */
247
+ readonly streamResolver?: (level: LogLevel$1) => "stdout" | "stderr";
248
+ }
249
+ /**
250
+ * Options for creating a log output parser.
251
+ * @since 0.8.0
252
+ */
253
+ interface LogOutputOptions {
254
+ /**
255
+ * Long option name for the log output option.
256
+ * @default `"--log-output"`
257
+ */
258
+ readonly long?: string;
259
+ /**
260
+ * Short option name for the log output option.
261
+ */
262
+ readonly short?: string;
263
+ /**
264
+ * The metavariable name shown in help text.
265
+ * @default `"FILE"`
266
+ */
267
+ readonly metavar?: string;
268
+ /**
269
+ * Description to show in help text.
270
+ */
271
+ readonly description?: Message;
272
+ /**
273
+ * Custom error messages.
274
+ */
275
+ readonly errors?: {
276
+ /**
277
+ * Error message when the output path is empty.
278
+ */
279
+ emptyPath?: Message | ((input: string) => Message);
280
+ };
281
+ }
282
+ /**
283
+ * Creates a parser for log output destination (`--log-output`).
284
+ *
285
+ * This parser accepts either `-` for console output (following CLI convention)
286
+ * or a file path for file output.
287
+ *
288
+ * @param options Configuration options for the log output parser.
289
+ * @returns A {@link Parser} that produces a {@link LogOutput} or `undefined`.
290
+ *
291
+ * @example Basic usage
292
+ * ```typescript
293
+ * import { logOutput } from "@optique/logtape";
294
+ * import { object } from "@optique/core/constructs";
295
+ *
296
+ * const parser = object({
297
+ * output: logOutput(),
298
+ * });
299
+ *
300
+ * // --log-output=- -> console output
301
+ * // --log-output=/var/log/app.log -> file output
302
+ * ```
303
+ *
304
+ * @since 0.8.0
305
+ */
306
+ declare function logOutput(options?: LogOutputOptions): Parser<LogOutput | undefined, unknown>;
307
+ /**
308
+ * Creates a console sink with configurable stream selection.
309
+ *
310
+ * This function creates a LogTape sink that writes to the console. The target
311
+ * stream (stdout or stderr) can be configured statically or dynamically per
312
+ * log record.
313
+ *
314
+ * @param options Configuration options for the console sink.
315
+ * @returns A {@link Sink} function.
316
+ *
317
+ * @example Static stream selection
318
+ * ```typescript
319
+ * import { createConsoleSink } from "@optique/logtape";
320
+ *
321
+ * const sink = createConsoleSink({ stream: "stderr" });
322
+ * ```
323
+ *
324
+ * @example Dynamic stream selection based on level
325
+ * ```typescript
326
+ * import { createConsoleSink } from "@optique/logtape";
327
+ *
328
+ * const sink = createConsoleSink({
329
+ * streamResolver: (level) =>
330
+ * level === "error" || level === "fatal" ? "stderr" : "stdout"
331
+ * });
332
+ * ```
333
+ *
334
+ * @since 0.8.0
335
+ */
336
+ declare function createConsoleSink(options?: ConsoleSinkOptions): Sink$1;
337
+ /**
338
+ * Creates a sink from a {@link LogOutput} destination.
339
+ *
340
+ * For console output, this creates a console sink. For file output, this
341
+ * dynamically imports `@logtape/file` and creates a file sink.
342
+ *
343
+ * @param output The log output destination.
344
+ * @param consoleSinkOptions Options for console sink (only used when output is console).
345
+ * @returns A promise that resolves to a {@link Sink}.
346
+ * @throws {Error} If file output is requested but `@logtape/file` is not installed.
347
+ *
348
+ * @example Console output
349
+ * ```typescript
350
+ * import { createSink } from "@optique/logtape";
351
+ *
352
+ * const sink = await createSink({ type: "console" }, { stream: "stderr" });
353
+ * ```
354
+ *
355
+ * @example File output
356
+ * ```typescript
357
+ * import { createSink } from "@optique/logtape";
358
+ *
359
+ * const sink = await createSink({ type: "file", path: "/var/log/app.log" });
360
+ * ```
361
+ *
362
+ * @since 0.8.0
363
+ */
364
+ declare function createSink(output: LogOutput, consoleSinkOptions?: ConsoleSinkOptions): Promise<Sink$1>;
365
+ //#endregion
366
+ //#region src/preset.d.ts
367
+ /**
368
+ * Result type for the logging options parser.
369
+ * @since 0.8.0
370
+ */
371
+ interface LoggingOptionsResult {
372
+ /**
373
+ * The configured log level.
374
+ */
375
+ readonly logLevel: LogLevel$1;
376
+ /**
377
+ * The configured log output destination.
378
+ */
379
+ readonly logOutput: LogOutput;
380
+ }
381
+ /**
382
+ * Configuration for log output option in presets.
383
+ * @since 0.8.0
384
+ */
385
+ interface LogOutputConfig {
386
+ /**
387
+ * Whether to enable the log output option.
388
+ * @default `true`
389
+ */
390
+ readonly enabled?: boolean;
391
+ /**
392
+ * Long option name for the log output option.
393
+ * @default `"--log-output"`
394
+ */
395
+ readonly long?: string;
396
+ }
397
+ /**
398
+ * Configuration for logging options preset using `--log-level` option.
399
+ * @since 0.8.0
400
+ */
401
+ interface LoggingOptionsWithLevel {
402
+ /**
403
+ * Use explicit log level option.
404
+ */
405
+ readonly level: "option";
406
+ /**
407
+ * Long option name for the log level option.
408
+ * @default `"--log-level"`
409
+ */
410
+ readonly long?: string;
411
+ /**
412
+ * Short option name for the log level option.
413
+ * @default `"-l"`
414
+ */
415
+ readonly short?: string;
416
+ /**
417
+ * Default log level when not specified.
418
+ * @default `"info"`
419
+ */
420
+ readonly default?: LogLevel$1;
421
+ /**
422
+ * Configuration for log output option.
423
+ */
424
+ readonly output?: LogOutputConfig;
425
+ /**
426
+ * Label for the option group in help text.
427
+ * @default `"Logging options"`
428
+ */
429
+ readonly groupLabel?: string;
430
+ }
431
+ /**
432
+ * Configuration for logging options preset using `-v`/`-vv`/`-vvv` flags.
433
+ * @since 0.8.0
434
+ */
435
+ interface LoggingOptionsWithVerbosity {
436
+ /**
437
+ * Use verbosity flags.
438
+ */
439
+ readonly level: "verbosity";
440
+ /**
441
+ * Short option name for the verbosity flag.
442
+ * @default `"-v"`
443
+ */
444
+ readonly short?: string;
445
+ /**
446
+ * Long option name for the verbosity flag.
447
+ * @default `"--verbose"`
448
+ */
449
+ readonly long?: string;
450
+ /**
451
+ * Base log level when no verbosity flags are provided.
452
+ * @default `"warning"`
453
+ */
454
+ readonly baseLevel?: LogLevel$1;
455
+ /**
456
+ * Configuration for log output option.
457
+ */
458
+ readonly output?: LogOutputConfig;
459
+ /**
460
+ * Label for the option group in help text.
461
+ * @default `"Logging options"`
462
+ */
463
+ readonly groupLabel?: string;
464
+ }
465
+ /**
466
+ * Configuration for logging options preset using `--debug` flag.
467
+ * @since 0.8.0
468
+ */
469
+ interface LoggingOptionsWithDebug {
470
+ /**
471
+ * Use debug flag.
472
+ */
473
+ readonly level: "debug";
474
+ /**
475
+ * Long option name for the debug flag.
476
+ * @default `"--debug"`
477
+ */
478
+ readonly long?: string;
479
+ /**
480
+ * Short option name for the debug flag.
481
+ * @default `"-d"`
482
+ */
483
+ readonly short?: string;
484
+ /**
485
+ * Log level when debug flag is present.
486
+ * @default `"debug"`
487
+ */
488
+ readonly debugLevel?: LogLevel$1;
489
+ /**
490
+ * Log level when debug flag is not present.
491
+ * @default `"info"`
492
+ */
493
+ readonly normalLevel?: LogLevel$1;
494
+ /**
495
+ * Configuration for log output option.
496
+ */
497
+ readonly output?: LogOutputConfig;
498
+ /**
499
+ * Label for the option group in help text.
500
+ * @default `"Logging options"`
501
+ */
502
+ readonly groupLabel?: string;
503
+ }
504
+ /**
505
+ * Configuration for logging options preset.
506
+ *
507
+ * This is a discriminated union type that determines how log level is
508
+ * configured. Only one method can be used at a time:
509
+ *
510
+ * - `level: "option"`: Use `--log-level=LEVEL` option
511
+ * - `level: "verbosity"`: Use `-v`/`-vv`/`-vvv` flags
512
+ * - `level: "debug"`: Use `--debug` flag
513
+ *
514
+ * @since 0.8.0
515
+ */
516
+ type LoggingOptionsConfig = LoggingOptionsWithLevel | LoggingOptionsWithVerbosity | LoggingOptionsWithDebug;
517
+ /**
518
+ * Creates a logging options parser preset.
519
+ *
520
+ * This function creates a parser that combines log level and log output
521
+ * options into a single group. The log level can be configured using one of
522
+ * three methods (mutually exclusive):
523
+ *
524
+ * - `"option"`: Explicit `--log-level=LEVEL` option
525
+ * - `"verbosity"`: `-v`/`-vv`/`-vvv` flags for increasing verbosity
526
+ * - `"debug"`: Simple `--debug` flag toggle
527
+ *
528
+ * @param config Configuration for the logging options.
529
+ * @returns A {@link Parser} that produces a {@link LoggingOptionsResult}.
530
+ *
531
+ * @example Using log level option
532
+ * ```typescript
533
+ * import { loggingOptions } from "@optique/logtape";
534
+ * import { object } from "@optique/core/constructs";
535
+ *
536
+ * const parser = object({
537
+ * logging: loggingOptions({ level: "option" }),
538
+ * });
539
+ * // --log-level=debug --log-output=/var/log/app.log
540
+ * ```
541
+ *
542
+ * @example Using verbosity flags
543
+ * ```typescript
544
+ * import { loggingOptions } from "@optique/logtape";
545
+ * import { object } from "@optique/core/constructs";
546
+ *
547
+ * const parser = object({
548
+ * logging: loggingOptions({ level: "verbosity" }),
549
+ * });
550
+ * // -vv --log-output=-
551
+ * ```
552
+ *
553
+ * @example Using debug flag
554
+ * ```typescript
555
+ * import { loggingOptions } from "@optique/logtape";
556
+ * import { object } from "@optique/core/constructs";
557
+ *
558
+ * const parser = object({
559
+ * logging: loggingOptions({ level: "debug" }),
560
+ * });
561
+ * // --debug
562
+ * ```
563
+ *
564
+ * @since 0.8.0
565
+ */
566
+ declare function loggingOptions(config: LoggingOptionsConfig): Parser<LoggingOptionsResult, unknown>;
567
+ /**
568
+ * Creates a LogTape configuration from parsed logging options.
569
+ *
570
+ * This helper function converts the result of {@link loggingOptions} parser
571
+ * into a configuration object that can be passed to LogTape's `configure()`
572
+ * function.
573
+ *
574
+ * @param options The parsed logging options.
575
+ * @param consoleSinkOptions Options for console sink (only used when output is console).
576
+ * @param additionalConfig Additional LogTape configuration to merge.
577
+ * @returns A promise that resolves to a LogTape {@link Config}.
578
+ *
579
+ * @example Basic usage
580
+ * ```typescript
581
+ * import { loggingOptions, createLoggingConfig } from "@optique/logtape";
582
+ * import { configure } from "@logtape/logtape";
583
+ * import { object, parse } from "@optique/core";
584
+ *
585
+ * const parser = object({
586
+ * logging: loggingOptions({ level: "option" }),
587
+ * });
588
+ *
589
+ * const result = parse(parser, ["--log-level=debug"]);
590
+ * if (result.success) {
591
+ * const config = await createLoggingConfig(result.value.logging);
592
+ * await configure(config);
593
+ * }
594
+ * ```
595
+ *
596
+ * @example With additional configuration
597
+ * ```typescript
598
+ * import { loggingOptions, createLoggingConfig } from "@optique/logtape";
599
+ * import { configure } from "@logtape/logtape";
600
+ *
601
+ * const config = await createLoggingConfig(result.value.logging, {
602
+ * stream: "stderr",
603
+ * }, {
604
+ * loggers: [
605
+ * { category: ["my-app", "database"], lowestLevel: "debug", sinks: ["default"] },
606
+ * ],
607
+ * });
608
+ * await configure(config);
609
+ * ```
610
+ *
611
+ * @since 0.8.0
612
+ */
613
+ declare function createLoggingConfig(options: LoggingOptionsResult, consoleSinkOptions?: ConsoleSinkOptions, additionalConfig?: Partial<Config<string, string>>): Promise<Config<string, string>>;
614
+ //#endregion
615
+ export { type ConsoleSinkOptions, type DebugOptions, LOG_LEVELS, type LogLevel, type LogLevelOptions, type LogOutput, type LogOutputConfig, type LogOutputOptions, type LogRecord, type LoggingOptionsConfig, type LoggingOptionsResult, type LoggingOptionsWithDebug, type LoggingOptionsWithLevel, type LoggingOptionsWithVerbosity, type Sink, type VerbosityOptions, createConsoleSink, createLoggingConfig, createSink, debug, logLevel, logOutput, loggingOptions, verbosity };