Name Description Size Coverage
interest_cache.rs 17381 -
lib.rs Adapters for connecting unstructured log records from the `log` crate into the `tracing` ecosystem. # Overview [`tracing`] is a framework for instrumenting Rust programs with context-aware, structured, event-based diagnostic information. This crate provides compatibility layers for using `tracing` alongside the logging facade provided by the [`log`] crate. This crate provides: - [`AsTrace`] and [`AsLog`] traits for converting between `tracing` and `log` types. - [`LogTracer`], a [`log::Log`] implementation that consumes [`log::Record`]s and outputs them as [`tracing::Event`]. *Compiler support: [requires `rustc` 1.56+][msrv]* [msrv]: #supported-rust-versions # Usage ## Convert log records to tracing `Event`s To convert [`log::Record`]s as [`tracing::Event`]s, set `LogTracer` as the default logger by calling its [`init`] or [`init_with_filter`] methods. ```rust # use std::error::Error; use tracing_log::LogTracer; use log; # fn main() -> Result<(), Box<Error>> { LogTracer::init()?; // will be available for Subscribers as a tracing Event log::trace!("an example trace log"); # Ok(()) # } ``` This conversion does not convert unstructured data in log records (such as values passed as format arguments to the `log!` macro) to structured `tracing` fields. However, it *does* attach these new events to to the span that was currently executing when the record was logged. This is the primary use-case for this library: making it possible to locate the log records emitted by dependencies which use `log` within the context of a trace. ## Convert tracing `Event`s to logs Enabling the ["log" and "log-always" feature flags][flags] on the `tracing` crate will cause all `tracing` spans and events to emit `log::Record`s as they occur. ## Caution: Mixing both conversions Note that `log::Logger` implementations that convert log records to trace events should not be used with `Subscriber`s that convert trace events _back_ into `log` records, as doing so will result in the event recursing between the subscriber and the logger forever (or, in real life, probably overflowing the call stack). If the logging of trace events generated from log records produced by the `log` crate is desired, either the `log` crate should not be used to implement this logging, or an additional layer of filtering will be required to avoid infinitely converting between `Event` and `log::Record`. ## Feature Flags * `std`: enables features that require the Rust standard library (on by default) * `log-tracer`: enables the `LogTracer` type (on by default) * `interest-cache`: makes it possible to configure an interest cache for logs emitted through the `log` crate (see [`Builder::with_interest_cache`]); requires `std` ## Supported Rust Versions Tracing is built against the latest stable release. The minimum supported version is 1.56. The current Tracing version is not guaranteed to build on Rust versions earlier than the minimum supported version. Tracing follows the same compiler support policies as the rest of the Tokio project. The current stable Rust compiler and the three most recent minor versions before it will always be supported. For example, if the current stable compiler version is 1.69, the minimum supported version will not be increased past 1.66, three minor versions prior. Increasing the minimum supported compiler version is not considered a semver breaking change as long as doing so complies with this policy. [`init`]: LogTracer::init [`init_with_filter`]: LogTracer::init_with_filter [`tracing`]: https://crates.io/crates/tracing [`tracing::Subscriber`]: https://docs.rs/tracing/latest/tracing/trait.Subscriber.html [`Subscriber`]: https://docs.rs/tracing/latest/tracing/trait.Subscriber.html [`tracing::Event`]: https://docs.rs/tracing/latest/tracing/struct.Event.html [flags]: https://docs.rs/tracing/latest/tracing/#crate-feature-flags [`Builder::with_interest_cache`]: log_tracer::Builder::with_interest_cache 19947 -
log_tracer.rs An adapter for converting [`log`] records into `tracing` `Event`s. This module provides the [`LogTracer`] type which implements `log`'s [logger interface] by recording log records as `tracing` `Event`s. This is intended for use in conjunction with a `tracing` `Subscriber` to consume events from dependencies that emit [`log`] records within a trace context. # Usage To create and initialize a `LogTracer` with the default configurations, use: * [`init`] if you want to convert all logs, regardless of log level, allowing the tracing `Subscriber` to perform any filtering * [`init_with_filter`] to convert all logs up to a specified log level In addition, a [builder] is available for cases where more advanced configuration is required. In particular, the builder can be used to [ignore log records][ignore] emitted by particular crates. This is useful in cases such as when a crate emits both `tracing` diagnostics _and_ log records by default. [logger interface]: log::Log [`init`]: LogTracer.html#method.init [`init_with_filter`]: LogTracer.html#method.init_with_filter [builder]: LogTracer::builder() [ignore]: Builder::ignore_crate() 10820 -