How to setup simultaneous with features? #2300
-
Hello! I'm trying setup a simultaneous logging which can be adjusted with the feature-flags: mod journald;
pub fn setup_logging() {
let subscriber: Box<dyn Subscriber> = get_subscriber();
tracing::subscriber::set_global_default(subscriber)
.expect("Couldn't setup global subscriber (logger)");
}
fn get_subscriber() -> Box<dyn Subscriber> {
let subscriber: Box<dyn Subscriber> = Box::new(tracing_subscriber::registry());
let subscriber = subscriber.with(EnvFilter::from_default_env());
#[cfg(feature = "journald")]
let subscriber = journald::add_layer(subscriber);
#[cfg(feature = "file")]
// add file logging
subscriber
}
use tracing::Subscriber;
pub fn add_layer(subscriber: Box<dyn Subscriber>) -> Box<dyn Subscriber> {
(*subscriber).with(tracing_journald::layer().unwrap())
} I'm a little bit stuck at the moment because I'm getting a bunch of errors like: error[E0277]: `dyn tracing::Subscriber` cannot be sent between threads safely
--> leftwm/src/utils/log/mod.rs:8:45
|
8 | tracing::subscriber::set_global_default(subscriber)
| --------------------------------------- ^^^^^^^^^^ `dyn tracing::Subscriber` cannot be sent between threads safely
| |
| required by a bound introduced by this call
|
= help: the trait `Send` is not implemented for `dyn tracing::Subscriber`
= note: required because of the requirements on the impl of `Send` for `Unique<dyn tracing::Subscriber>`
= note: required because it appears within the type `Box<dyn tracing::Subscriber>`
note: required by a bound in `tracing::subscriber::set_global_default`
--> /home/tornax/.cargo/registry/src/github.com-1ecc6299db9ec823/tracing-0.1.36/src/subscriber.rs:41:21
|
41 | S: Subscriber + Send + Sync + 'static,
| ^^^^ required by this bound in `tracing::subscriber::set_global_default` |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Have you read https://docs.rs/tracing-subscriber/latest/tracing_subscriber/layer/index.html#runtime-configuration-with-layers, by any chance? I'd suggest dynamic configuration through that approach instead of using feature flags. That being said, something like this should work: use tracing::Subscriber;
use tracing_subscriber::{filter::EnvFilter, fmt, layer::SubscriberExt};
fn main() {
let subscriber: Box<dyn Subscriber + Send + Sync + 'static> = get_subscriber();
tracing::subscriber::set_global_default(subscriber)
.expect("Couldn't setup global subscriber (logger)");
}
fn get_subscriber() -> Box<dyn Subscriber + Send + Sync + 'static> {
let subscriber = tracing_subscriber::registry();
let subscriber = subscriber.with(EnvFilter::from_default_env());
Box::new(subscriber.with(fmt::Layer::default()))
} Notably, you can't declare the registry by writing |
Beta Was this translation helpful? Give feedback.
Have you read https://docs.rs/tracing-subscriber/latest/tracing_subscriber/layer/index.html#runtime-configuration-with-layers, by any chance? I'd suggest dynamic configuration through that approach instead of using feature flags.
That being said, something like this should work: