Logger class final

A logger for command-line tools, shaped like mason_logger's Logger.

info, detail and success write to stdout; warn, err and alert write to stderr. Prompts use dart_tui's prompt helpers (promptInput, promptConfirm, promptSelect, promptMultiSelect): a TUI on a terminal, and plain line questions in pipes, CI and with ACCESSIBLE=1 (see withPlainMode). Colours follow detectColorProfile, so NO_COLOR and pipes turn them off.

Unlike mason_logger, prompts return a Future. Each one has a blocking twin (promptSync, promptAnySync, confirmSync, chooseOneSync, chooseAnySync) for code that is already synchronous.

Sync prompts work in any isolate, but their stdin guard only checks the calling isolate. Do not let another isolate read stdin or change terminal mode at the same time (see promptSelectSync).

Cancelling a prompt (Esc or Ctrl+C in the TUI) exits the process with code 130, as mason_logger does on Ctrl+C. When input ends in plain mode, the prompt returns its default instead.

stdout, stderr, stdin and environment replace the real ones, for tests. A custom stdin is listened to once per prompt, so pass a broadcast stream to answer several prompts. exit replaces dart:io's exit for the cancel case.

Constructors

Logger({Level level = Level.info, ProgressOptions progressOptions = const ProgressOptions(), IOSink? stdout, IOSink? stderr, Stream<List<int>>? stdin, Map<String, String>? environment, Never exit(int code)?})

Properties

hashCode → int
The hash code for this object.
no setterinherited
level ↔ Level
The lowest level that is written.
getter/setter pair
progressOptions ↔ ProgressOptions
The options progress uses when it gets none.
getter/setter pair
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

alert(String? message, {String? style(String? message)?}) → void
Writes message to stderr in bold white on red.
chooseAny<T extends Object?>(String? message, {required List<T> choices, List<T>? defaultValues, String display(T choice)?}) → Future<List<T>>
Asks for any number of choices, shown with display. The ones in defaultValues start checked.
chooseAnySync<T extends Object?>(String? message, {required List<T> choices, List<T>? defaultValues, String display(T choice)?}) → List<T>
Like chooseAny, but blocks and returns the answer.
chooseOne<T extends Object?>(String? message, {required List<T> choices, T? defaultValue, String display(T choice)?}) → Future<T>
Asks for one of choices, shown with display (default toString). The cursor starts on defaultValue, or the first choice.
chooseOneSync<T extends Object?>(String? message, {required List<T> choices, T? defaultValue, String display(T choice)?}) → T
Like chooseOne, but blocks and returns the answer.
confirm(String? message, {bool defaultValue = false}) → Future<bool>
Asks a yes / no question. Enter returns defaultValue.
confirmSync(String? message, {bool defaultValue = false}) → bool
Like confirm, but blocks and returns the answer.
delayed(String? message) → void
Keeps message until flush.
detail(String? message, {String? style(String? message)?}) → void
Writes message to stdout in dark grey, at Level.debug.
err(String? message, {String? style(String? message)?}) → void
Writes message to stderr in red.
flush([void print(String?)?]) → void
Writes the delayed messages with print (default info).
info(String? message, {String? style(String? message)?}) → void
Writes message to stdout.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
progress(String message, {ProgressOptions? options}) → Progress
Starts a spinner line with message; end it with Progress.complete, Progress.fail or Progress.cancel.
prompt(String? message, {Object? defaultValue, bool hidden = false}) → Future<String>
Asks for a line of text. Empty input returns defaultValue (or an empty string). With hidden, the answer is not shown.
promptAny(String? message, {String separator = ','}) → Future<List<String>>
Asks for a line of text and splits it on separator. Items are trimmed, and empty ones dropped.
promptAnySync(String? message, {String separator = ','}) → List<String>
Like promptAny, but blocks and returns the answer.
promptSync(String? message, {Object? defaultValue, bool hidden = false}) → String
Like prompt, but blocks and returns the answer, as mason_logger's prompt does. For code that is already synchronous; see promptInputSync for what sync prompts can't do.
success(String? message, {String? style(String? message)?}) → void
Writes message to stdout in green.
tasks(List<Task> tasks, {int concurrency = 1, bool exitOnError = true}) → Future<void>
Runs tasks as a live list: a spinner line per task, the last line of each running task's output under it, and logs above it. Nested steps come from TaskContext.tasks. At the end it prints one summary line.
toString() → String
A string representation of this object.
inherited
warn(String? message, {String tag = 'WARN', String? style(String? message)?}) → void
Writes [tag] message to stderr in bold yellow. An empty tag writes the message alone.
write(String? message) → void
Writes message to stdout as it is, with no newline.

Operators

operator ==(Object other) → bool
The equality operator.
inherited