Module flexi_logger::trc
source · [−]trc
only.Expand description
Use flexi_logger
functionality with tracing
.
tracing
is an alternative to
log
.
It has a similar base architecture, but is optimized for supporting async apps,
which adds complexity due to the need to manage contexts.
tracing-subscriber
facilitates contributing “backends”, and is used in the example below to plug
flexi_logger
-functionality into tracing
.
The content of this module is a first attempt to support such an integration. Every feedback is highly appreciated.
Example
The following example uses a FileLogWriter
as trace writer,
and flexi_logger
’s specfile handling to adapt tracing
dynamically,
while your program is running.
The code is a bit cumbersome, maybe there are (oor will be) easier ways to achieve the same.
Precondition: add these entries to your Cargo.toml
:
flexi_logger = {version = "0.19", features = ["trc"]}
tracing = "0.1"
tracing-subscriber = {version = "0.2.20", features = ["env-filter"]}
use flexi_logger::{
trc::{subscribe_to_specfile, BasicLogSpecSubscriber, LogSpecAsFilter},
writers::FileLogWriter,
Age, Cleanup, Criterion, FileSpec, LogSpecification, Naming, WriteMode,
};
use tracing::{debug, info, trace, warn};
use tracing_subscriber::FmtSubscriber;
// Prepare a `FileLogWriter` and a handle to it, and keep the handle alive
// until the program ends (it will flush and shutdown the `FileLogWriter` when dropped).
// For the `FileLogWriter`, use the settings that fit your needs
let (file_writer, _fw_handle) = FileLogWriter::builder(FileSpec::default())
.rotate(
// If the program runs long enough,
Criterion::Age(Age::Day), // - create a new file every day
Naming::Timestamps, // - let the rotated files have a timestamp in their name
Cleanup::KeepLogFiles(7), // - keep at most seven log files
)
.write_mode(WriteMode::Async)
.try_build_with_handle()
.unwrap();
// Set up subscriber that makes use of the file writer, with some hardcoded initial log spec
let initial_logspec = LogSpecification::info();
let subscriber_builder = FmtSubscriber::builder()
.with_writer(move || file_writer.clone())
.with_env_filter(LogSpecAsFilter(initial_logspec.clone()))
.with_filter_reloading();
// Set up specfile tracking and subscribe
let reload_handle = Box::new(subscriber_builder.reload_handle());
subscribe_to_specfile(
"trcspecfile.toml",
BasicLogSpecSubscriber::new(
Box::new(move |logspec| reload_handle.reload(LogSpecAsFilter(logspec)).unwrap()),
initial_logspec,
),
)
.unwrap();
// Get ready to trace
tracing::subscriber::set_global_default(subscriber_builder.finish())
.expect("setting default subscriber failed");
// now do what you really want to do...
Structs
Helper struct that can be registered in
subscribe_to_specfile
to get
informed about updates to the specfile,
and can be registered in tracing
to forward such updates.
Helper struct for using LogSpecification
as filter in tracing
.
Traits
Trait that allows to register for changes to the log specification.
Functions
Allows registering a LogSpecSubscriber
to a specfile.