-
use std::io;
use tracing::{debug, error, info, instrument, level_filters::LevelFilter, warn};
use tracing_subscriber::{Layer, layer::SubscriberExt, util::SubscriberInitExt};
#[instrument]
fn test(a: i32, b: &str) {
info!("Hello world!");
error!("An error occurs!");
debug!("Debug message");
warn!("A warning message");
}
fn main() {
let file_appender = tracing_appender::rolling::daily("log/", "output.log");
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
let stdout_layer = tracing_subscriber::fmt::layer().with_writer(io::stdout);
let mut file_layer = tracing_subscriber::fmt::layer()
.with_ansi(false) // NOT working?
.with_writer(non_blocking);
file_layer.set_ansi(false); // NOT working?
let layers = vec![
stdout_layer.with_filter(LevelFilter::INFO).boxed(),
file_layer.with_filter(LevelFilter::INFO).boxed(),
];
tracing_subscriber::registry().with(layers).init();
test(42, "test");
} The script still generate contents like
Running |
Beta Was this translation helpful? Give feedback.
Answered by
kaffarell
Mar 12, 2025
Replies: 1 comment
-
This is because the formatted fields are not stored properly and only one action (ansi=true or ansi=false) is recorded. This should be fixed with #3221. In the meantime you can put the stdout_layer above the file_layer in the layers vec, so: let layers = vec![
file_layer.with_filter(LevelFilter::INFO).boxed(),
stdout_layer.with_filter(LevelFilter::INFO).boxed(),
]; This will trigger the no-ansi formatted fields to be inserted first. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
failable
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is because the formatted fields are not stored properly and only one action (ansi=true or ansi=false) is recorded. This should be fixed with #3221. In the meantime you can put the stdout_layer above the file_layer in the layers vec, so:
This will trigger the no-ansi formatted fields to be inserted first.