Simultaneous logging #2299
-
Hello! I've got a question: Is it possible to log to multiple sources at the "same" time? For example: I'd like to log to a file but also to journald as well, is that possible with |
Beta Was this translation helpful? Give feedback.
Answered by
davidbarsky
Sep 8, 2022
Replies: 1 comment 1 reply
-
Yes, take a look at this example. To adopt it to your use-case, replace the use std::io;
use tracing_subscriber::{fmt, layer::SubscriberExt, EnvFilter};
fn main() {
let file_appender = tracing_appender::rolling::hourly(dir, "example.log");
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
let subscriber = tracing_subscriber::registry()
.with(EnvFilter::from_default_env().add_directive(tracing::Level::TRACE.into()))
.with(tracing_journald::Layer::new())
.with(fmt::Layer::new().with_writer(non_blocking));
tracing::subscriber::set_global_default(subscriber).expect("Unable to set a global subscriber");
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
TornaxO7
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, take a look at this example. To adopt it to your use-case, replace the
fmt::Layer()
on line 18 withtracing_journald::Layer
::Layer. The code will look something like this: