base-console
Automatic terminal and JSON diagnostics for Rust applications using tracing.
Console chooses coloured, human-readable output when stderr is a terminal and newline-delimited JSON when it is redirected. Both formats preserve event fields, nested span context, and typed errors with their causes. Libraries continue using tracing directly; the application chooses its subscriber.
Usage
Add Console and tracing to an application:
init reads RUST_LOG, defaulting to info, and panics if setup fails. Use base_console::try_init()? to handle invalid filters or an already installed global subscriber. Both leave the log logger unchanged; applications may explicitly install tracing_log::LogTracer to include dependencies using log.
Try the demonstration:
Failures
Ordinary error fields preserve their cause chains when recorded as &dyn Error:
let error = other;
error!;
Use Failure to capture diagnostics at the point an error is wrapped, then retain or return it for later reporting:
let failure = capture;
failure!;
The macro preserves the caller's target and source location. The wrapper captures a logical span trace when Console's layer is active, and an OS backtrace when RUST_BACKTRACE or RUST_LIB_BACKTRACE enables it. Failure::with_backtrace accepts a previously captured backtrace. Capture before leaving the failing operation: wrapping an error later cannot reconstruct its earlier stack. Logging the wrapper directly as &dyn Error preserves its causes; use failure! to include its captured traces as well.
Composition
Configure a Console and compose its layer with other subscribers, or use it for scoped output:
use ;
use ;
let console = default
.with_format
.with_ansi
.with_filter;
let subscriber = registry.with;
with_default;
See the API documentation for writers, filters, asynchronous scopes, and the JSON output contract.
Releasing
Prepare a release with cargo bake cargo:version:patch (or minor, major, or bump --version X.Y.Z), commit it with a Release: vVERSION trailer, then run cargo bake cargo:release and open a pull request. After review and merge, GitHub Actions publishes the release when the configured crates-io environment approves it, then creates or updates the matching GitHub Release from releases.md. Follow the shared Releasing skill for the standard process.
Releases
See releases.md for the full release history.
v0.1.1
- Record integers wider than 64 bits as decimal strings by default, and add an optional
arbitrary-precisionfeature that records them as JSON numbers. - Stop enabling
serde_json'sarbitrary_precisionby default, because it changes number handling for everyserde_jsonuser in a build.
v0.1.0
- Publish as
base-consolein thebase-rsorganization, with package versions starting at 0.1.0. - Choose terminal text or newline-delimited JSON automatically for
tracingevents. - Preserve typed fields, span context, and error causes through composable or scoped subscribers.
- Capture failures with optional OS backtraces and logical span traces for later reporting.
- Follow Rust logging conventions with panicking
initand fallibletry_initfunctions and builder methods.
See Also
tracingprovides structured events and spans.tracing-subscriberprovides subscriber composition and filtering.
Contributing
Please open an issue or pull request on GitHub.